Note that there are some explanatory texts on larger screens.

plurals
  1. POneed Help in RSA ENCRYPTION(doFinal)
    primarykey
    data
    text
    <p>i am trying to encrypt and decrypt a string using RSA algorithm. Here the encryption is working good but problem is in decryption. the code gets terminated when it reaches the doFinal in DECRYPT method. Am i taking the input wrong or is there any problem with public and private keys? please give me suggestions regarding this. Thank u.</p> <pre><code>public class rsa { private KeyPair keypair; public rsa() throws NoSuchAlgorithmException, NoSuchProviderException { KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); keygenerator.initialize(1024, random); keypair = keygenerator.generateKeyPair(); } public String ENCRYPT(String Algorithm, String Data ) throws Exception { String alg = Algorithm; String data=Data; byte[] encrypted=new byte[2048]; if(alg.equals("RSA")) { PublicKey publicKey = keypair.getPublic(); Cipher cipher; cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); encrypted = cipher.doFinal(data.getBytes()); System.out.println("Encrypted String[RSA] -&gt; " + encrypted); } return encrypted.toString(); } public String DECRYPT(String Algorithm, String Data ) throws Exception { String alg = Algorithm; byte[] Decrypted=Data.getBytes(); if(alg.equals("RSA")) { PrivateKey privateKey = keypair.getPrivate(); Cipher cipher; cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] dec = cipher.doFinal(Decrypted); System.out.println("Decrypted String[RSA] -&gt; " + dec.toString()); } return Decrypted.toString(); } public static void main(String[] args) throws Exception { rsa RSA=new rsa(); RSA.ENCRYPT("RSA", "avinash"); RSA.DECRYPT("RSA","[B@cb7e2c"); } </code></pre> <p>}</p> <pre><code> got exception as Exception in thread "main" javax.crypto.BadPaddingException: Data must start with zero at sun.security.rsa.RSAPadding.unpadV15(Unknown Source) at sun.security.rsa.RSAPadding.unpad(Unknown Source) at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356) at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382) at javax.crypto.Cipher.doFinal(Cipher.java:2086) at EncryptionProvider.rsa.DECRYPT(rsa.java:56) at EncryptionProvider.rsa.main(rsa.java:68) </code></pre> <p>Encrypted String[RSA] -> [B@4a96a</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload