java - IllegalBlockSize Exception When Doing RSA on bytes of Strings -


i trying write rsa encryption , decryption classes in java server client passing strings , forth. have following code classes:

public class rsaencryption { public static final string keygenalgorithm = "rsa"; public static final string algorithm = "rsa/ecb/pkcs1padding";  public static keypaircontainer generatekey() {       keypairgenerator keygen;       keypair key;     try {         keygen = keypairgenerator.getinstance(keygenalgorithm);         keygen.initialize(1024);         key = keygen.generatekeypair();         return new keypaircontainer(key.getpublic(), key.getprivate());     } catch (nosuchalgorithmexception e) {         e.printstacktrace();         system.out.println("error: no such algorithm");     }     return null;  }  public static string pubkeytostring(publickey key){     byte[] array = key.getencoded();     base64encoder encoder = new base64encoder();     string tempstring = encoder.encode(array);     return tempstring; }  public static publickey stringtopubkey(string string){     base64decoder decoder = new base64decoder();     try {         byte[] array = decoder.decodebuffer(string);         x509encodedkeyspec x509keyspec = new x509encodedkeyspec(array);         keyfactory keyfact = keyfactory.getinstance(keygenalgorithm);         publickey pubkey = keyfact.generatepublic(x509keyspec);         return pubkey;     } catch (ioexception | nosuchalgorithmexception | invalidkeyspecexception e) {         // todo auto-generated catch block         e.printstacktrace();     } return null; }  public static byte[] rsaencrypt(byte[] plaintext, string keystring) {     cipher cipher;     try {         publickey key = stringtopubkey(keystring);         cipher = cipher.getinstance(algorithm);         cipher.init(cipher.encrypt_mode, key);         byte[] ciphertext = cipher.dofinal(plaintext);         //string ciphertext = new string(ciphertextbytes);         return ciphertext;     } catch (nosuchalgorithmexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (nosuchpaddingexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (invalidkeyexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (illegalblocksizeexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (badpaddingexception e) {         // todo auto-generated catch block         e.printstacktrace();     }     return null;  }  public static byte[] rsadecrypt(byte[] ciphertext, privatekey key){     cipher cipher;     try {         //byte[] ciphertext = ciphertextstring.getbytes();         cipher = cipher.getinstance(algorithm);         cipher.init(cipher.decrypt_mode, key);         byte[] decryptedtext = cipher.dofinal(ciphertext);         return decryptedtext;     } catch (nosuchalgorithmexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (nosuchpaddingexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (invalidkeyexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (illegalblocksizeexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (badpaddingexception e) {         // todo auto-generated catch block         e.printstacktrace();     }     return null; } } 

i have following code test class:

public class rsaencryptionkeydemo { public static void main(string[] args){     keypaircontainer keypair = rsaencryption.generatekey();     publickey pubkey = keypair.getpublickey();     string pubkeytext = rsaencryption.pubkeytostring(pubkey);     system.out.println(pubkeytext);      string plaintext = "hello world!";     byte[] ciphertext = rsaencryption.rsaencrypt(plaintext.getbytes(), pubkeytext);     string ciphertextstring = new string(ciphertext);     system.out.println(ciphertextstring);      privatekey privkey = keypair.getprivatekey();     byte[] decryptedtext = rsaencryption.rsadecrypt(ciphertextstring.getbytes(), privkey);      string decryptedtextstring = new string(decryptedtext);      system.out.println(decryptedtextstring);   } } 

however, when try run test class, key generation , encryption work fine, thrown error when try decrypt. error javax.crypto.illegalblocksizeexception: data must not longer 128 bytes, however, data less 128 bytes.

i can confirm transforming public key string , returns same public key. have tried testing code using bytes, , works fine. error apparently in trying decrypt bytes taken string.

is there way decrypt bytes taken string without throwing error? in advance, appreciated.

edit: think might have isolated issue. trying turn encrypted bytearray string, , extract bytes it, encrypted bytearray doesn't translate string anyway when bytes doesn't extract original encrypted bytearray was. correct, , if so, how turn bytearray string can exchange it?

if this

byte[] ciphertext = rsaencryption.rsaencrypt(plaintext.getbytes(), pubkeytext); //string ciphertextstring = new string(ciphertext); //system.out.println(ciphertextstring);  privatekey privkey = keypair.getprivatekey(); byte[] decryptedtext = rsaencryption.rsadecrypt(ciphertext, privkey); 

the code runs fine. cannot convert byte array string since bytes converted characters , bytes (using getbytes()) - depending on default charset. new string(ciphertext) strips away unprintable characters changes ciphertext , hence makes plaintext unrecoverable. (thanks artjom b. pointing out.)

simply use base64 or binary transport ciphertext, e.g.:

byte[] ciphertext = rsaencryption.rsaencrypt(plaintext.getbytes(), pubkeytext); string ciphertextstring = base64.tobase64string(ciphertext); system.out.println(ciphertextstring);  privatekey privkey = keypair.getprivatekey(); byte[] decryptedtext = rsaencryption.rsadecrypt(base64.decode(ciphertextstring), privkey); 

(i using bouncycastle base64 encoder here.)


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -