80,491
社区成员
发帖
与我相关
我的任务
分享
RSA加密使用RSA-OAEP算法,引入随机数,算法Hash使用SHA512,Label为空
SHA512是不是要用下面这个?
MessageDigest md = MessageDigest.getInstance("SHA-512");
private void getRSAPublidKeyBybase64(String base64s) {
Cipher cipher;
try {
//cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");//Android上使用
byte[] publicBytes = Base64.decode(base64s.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(keySpec);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String plaintext = "test";
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
String cipherText = Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
Log.d(TAG, "encrypted (cipherText) = " + cipherText);
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
InvalidKeyException | BadPaddingException | IllegalBlockSizeException |
InvalidKeySpecException e) {
e.printStackTrace();
}
}