java - javax.crypto.BadPaddingException : Decryption error -


i trying implement rsa based encryption communication between client , server. generated rsa public , private keys using openssl in following way :

openssl genrsa -out private.pem 2048  openssl rsa -in private.pem -outform pem -pubout -out public.pem --generate modulus , exponent openssl rsa -pubin -in public_key.pem -modulus -noout  openssl rsa -pubin -in public_key.pem -text -noout 

using above files, encryption done on android side follows :

   public static byte[] encrypt(biginteger modulus, biginteger exp, byte[] data) {         try {             keyfactory kf = keyfactory.getinstance("rsa");             rsapublickeyspec keyspec = new rsapublickeyspec(modulus, exp);             publickey publickey = kf.generatepublic(keyspec);             final cipher cipher = cipher.getinstance("rsa");             cipher.init(cipher.encrypt_mode, publickey);             return cipher.dofinal(data);         } catch (exception e) {             filelog.e("module" , e);         }         return null;     } 

this works fine , encrypt data. on server side, used following code decrypt. private key stored pkcs8 format key , read java.

public static byte[] decrypt(byte[] data) throws ioexception, nosuchalgorithmexception, invalidkeyspecexception, nosuchpaddingexception, invalidkeyexception, illegalblocksizeexception, badpaddingexception{     string filename = "location/of/private_key/file";     file f = new file(filename);     fileinputstream fis = new fileinputstream(f);     datainputstream dis = new datainputstream(fis);     byte[] keybytes = new byte[(int)f.length()];     dis.readfully(keybytes);     dis.close();     pkcs8encodedkeyspec spec = new pkcs8encodedkeyspec(keybytes);     keyfactory kf = keyfactory.getinstance("rsa");     privatekey rsaprivate = kf.generateprivate(spec);     cipher cipher = cipher.getinstance("rsa");     cipher.init(cipher.decrypt_mode, rsaprivate);     return cipher.dofinal(data);     } 

on running using eclipse on server, badpaddingexception: decryption error problem. length of data 256 bytes , hence length should not issue.

any helpful.

thanks

android using different padding algorithm. need use other algorithm below:

cipher checkcipher = cipher.getinstance("rsa/ecb/pkcs1padding"); 

there quit number of posts out there.

you can refer

rsa badpaddingexception in java - encrypt in android decrypt in jre

rsa encryption: difference between java , android


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -