Java android非对称加密代码错误
当我将字符串转换为字节时,它会生成一个错误 这是Java android非对称加密代码错误,java,android,encryption,rsa,Java,Android,Encryption,Rsa,当我将字符串转换为字节时,它会生成一个错误 这是 String f= "engineer hussein mawzi hello world"; KeyPairGenerator kpg; KeyPair kp; PublicKey publicKey; PrivateKey privateKey; byte [] encryptedBytes1,decryptedBytes1; Cipher cipher,cipher1;
String f= "engineer hussein mawzi hello world";
KeyPairGenerator kpg;
KeyPair kp;
PublicKey publicKey;
PrivateKey privateKey;
byte [] encryptedBytes1,decryptedBytes1;
Cipher cipher,cipher1;
String encrypted1,decrypted1;
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);
kp = kpg.genKeyPair();
publicKey = kp.getPublic();
privateKey = kp.getPrivate();
System.out.println("th"+publicKey);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes1= cipher.doFinal(f.getBytes());
encrypted1 = new String(encryptedBytes1);
// here is the message that i wnt to encrypte and send !!!!
System.out.println("here is mu test app"+encrypted1);
System.out.println("EEncrypted?????"+encrypted1.length());
cipher1 = Cipher.getInstance("RSA");
cipher1.init(Cipher.DECRYPT_MODE, privateKey);
// here i want to recover the byte array of the message i extract it and decrypte it
byte[] by = encrypted1.getBytes();
System.out.println(by.length);
decryptedBytes1 = cipher1.doFinal(by);
decrypted1 = new String(decryptedBytes1);
System.out.println("DDecrypted?????" + decrypted1);
System.out.println("DDecrypted?????" + decrypted1.length());
}
您的问题可能是从
字节[]
到字符串的转换。并非所有字节都映射到字符,不映射的字节将被静默删除。仅使用byte[]
或执行例如base 64编码/解码。它给出了一个错误,加密的1字符串长度为58,在get bytes操作后恢复的字符串是双倍的,因此以下是解密方法的输出。有什么帮助吗?请提供64 base编码的代码给我?