java - Encrypt and Decrypt a String with RSA -
i new bee java security , crypto. below code not return me correct decryption.
also please let me know recommendation using algorithm make strong key.
below code has 2 methods 1 encryption string , other decryption.
public class testsecuritydiscussions { public static byte[] encryptdata(keypair keys){ string rawdata = "hi how you>?"; cipher cipher = cipher.getinstance("rsa"); try { cipher.init(cipher.encrypt_mode, keys.getpublic()); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } byte[] encrypted = cipher.dofinal(rawdata.getbytes()); return encrypted; } public static string decryptdata(byte[] encrypted,keypair keys) { cipher cipher = cipher.getinstance("rsa"); try { cipher.init(cipher.decrypt_mode, keys.getprivate()); } catch (exception e) { e.printstacktrace(); } byte[] deycrypted = cipher.dofinal(encrypted); return deycrypted.tostring(); } public static void main(string[] args) { keypair keys = keypairgenerator.getinstance("rsa").generatekeypair(); byte[] keydata = encryptdata(keys); system.out.println("======>"+decryptdata(keydata,keys)); } }
i think problem line:
return decrypted.tostring();
byte strings give memory location via tostring()
. should this:
return new string(decrypted);
Comments
Post a Comment