我创建了上述类来使用 AES 执行文件加密和解密,下面是代码。我已将文件转换为字节数组并对其执行加密并再次使用文件对其进行解密并从函数中返回字节。
class AESEncryptDecrypt private constructor() {
private val secretKey: SecretKey
private val iV: AlgorithmParameterSpec
init {
this.secretKey = createAESKey(randomString()) as SecretKey
this.iV = IvParameterSpec(AES_IV.toByteArray())
}
/**
* Encrypt File
* @param file : File to Encrypt
*/
fun encryptFile(file: File): ByteArray {
var cipher: Cipher? = null
try {
cipher = Cipher.getInstance(AES_TRANSFORMATION)
cipher!!.init(Cipher.ENCRYPT_MODE, secretKey, iV)
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: NoSuchPaddingException) {
e.printStackTrace()
} catch (e: InvalidAlgorithmParameterException) {
e.printStackTrace()
} catch (e: InvalidKeyException) {
e.printStackTrace()
}
return cipher!!.doFinal(file.readBytes())
}
/**
* Encrypt Bytes
* @param byteArray : Plain Bytes to Encrypted Bytes
*/
fun encryptBytes(byteArray: ByteArray): ByteArray {
var cipher: Cipher? = null
try {
cipher = Cipher.getInstance(AES_TRANSFORMATION)
cipher!!.init(Cipher.ENCRYPT_MODE, secretKey, iV)
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: NoSuchPaddingException) {
e.printStackTrace()
} catch (e: InvalidAlgorithmParameterException) {
e.printStackTrace()
} catch (e: InvalidKeyException) {
e.printStackTrace()
}
return cipher!!.doFinal(byteArray)
}
/**
* Decrypt File
* @param file : File to Decrypt
*/
fun decryptFile(file: File): ByteArray {
var cipher: Cipher? = null
try {
cipher = Cipher.getInstance(AES_TRANSFORMATION)
cipher!!.init(Cipher.DECRYPT_MODE, secretKey, iV)
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: NoSuchPaddingException) {
e.printStackTrace()
} catch (e: InvalidAlgorithmParameterException) {
e.printStackTrace()
} catch (e: InvalidKeyException) {
e.printStackTrace()
}
return cipher!!.doFinal(file.readBytes())
}
companion object {
private var INSTANCE: AESEncryptDecrypt? = null
fun getInstance() =
INSTANCE ?: synchronized(AESEncryptDecrypt::class.java) {
INSTANCE ?: AESEncryptDecrypt()
.also { INSTANCE = it }
}
private fun createAESKey(keyValue: String): Key {
if (keyValue.length != AES_RANDOM_STRING_LENGTH)
try {
throw Exception("Key must be exactly 16 characters")
} catch (e: Exception) {
e.printStackTrace()
}
return SecretKeySpec(keyValue.toByteArray(), AES_TRANSFORMATION)
}
private fun randomString(): String {
return "ABCDE12345ABCDE1"
}
}
}
常量值如下所述
const val AES_IV = "abcaqwerabcaqwer"
const val AES_TRANSFORMATION = "AES/CBC/PKCS7Padding"
const val AES_RANDOM_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$"
const val AES_RANDOM_STRING_LENGTH = 16
我已经用于图像文件的加密和解密。该文件首先以未加密的方式存储,然后我使用了该功能
对于加密: FileUtils.writeByteArrayToFile(file, AESEncryptDecrypt.getInstance().encryptBytes(byteArray))
AESEncryptDecrypt.getInstance().encryptBytes(byteArray) 使我的文件第二次加密 FileUtils.writeByteArrayToFile是我覆盖我的文件并使其加密的函数。
对于解密:
val file: File? = getFile()
FileUtils.writeByteArrayToFile(
file, AESEncryptDecrypt.getInstance().decryptFile(file)
)
注意: FileUtils.writeByteArrayToFile 是从下面提到的依赖中使用的:(其次,这个函数是从依赖中提到的。)
implementation 'org.apache.commons:commons-lang3:3.7'