62,041
社区成员
发帖
与我相关
我的任务
分享
public static byte[] encryptLarger(byte[] data, Key key) throws Exception {
SecureRandom random = new SecureRandom();
byte[] secretKey = new byte[16];
random.nextBytes(secretKey);
javax.crypto.Cipher rsa = javax.crypto.Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsa.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
byte[] ciphedKey = rsa.doFinal(secretKey);
javax.crypto.Cipher aes = javax.crypto.Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec sks = new SecretKeySpec(secretKey, "AES");
aes.init(Cipher.ENCRYPT_MODE, sks);
byte[] ciphedData = aes.doFinal(data);
byte[] result = new byte[ciphedKey.length + ciphedData.length];
System.arraycopy(ciphedKey, 0, result, 0, ciphedKey.length);
System.arraycopy(ciphedData, 0, result, ciphedKey.length, ciphedData.length);
return result;
}