获取key
public byte[] keyGen() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(192);
return keyGenerator.generateKey().getEncoded();
}
加密文件
public byte[] encriptFile(byte[] dataToEncrypt, byte[] key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
//I'm using AES encription
Cipher c = Cipher.getInstance("AES");
SecretKeySpec k = new SecretKeySpec(key, "AES");
c.init(Cipher.ENCRYPT_MODE, k);
return c.doFinal(dataToEncrypt);
}
解密文件
public byte[] decriptFile(byte[] encryptedData, byte[] key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher c = Cipher.getInstance("AES");
SecretKeySpec k = new SecretKeySpec(key, "AES");
c.init(Cipher.DECRYPT_MODE, k);
return c.doFinal(encryptedData);
}
对于文件的加密如果文件太大肯定会影响性能的,所以针对于小型的文件