写出android中进行rsa加密的代码
时间: 2023-03-13 21:19:52 浏览: 172
以下是实现RSA加密的Android代码:// Generate an RSA keyKeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();// Get the public and private keysPublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();// Encrypt a messageCipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal("Hello world".getBytes());// Decrypt the messagecipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
String decryptedString = new String(decryptedBytes);
相关问题
写出android中进行rsa加密与解密的代码
下面是Android中用于RSA加密和解密的代码:// 加密
PrivateKey privatekey = keyPair.getPrivate();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, privatekey);
byte[] cipherData = cipher.doFinal(plainData);// 解密
PublicKey publickey = keyPair.getPublic();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, publickey);
byte[] plainData = cipher.doFinal(cipherData);
阅读全文