新增 打包时简单加密

This commit is contained in:
hyb1996
2018-12-01 12:53:40 +08:00
parent 25ca819fa7
commit 708cde848c
10 changed files with 185 additions and 13 deletions

View File

@@ -0,0 +1,42 @@
package com.stardust.util
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
class AdvancedEncryptionStandard(private val key: ByteArray, private val initVector: String) {
/**
* Encrypts the given plain text
*
* @param plainText The plain text to encrypt
*/
@Throws(Exception::class)
fun encrypt(plainText: ByteArray): ByteArray {
val secretKey = SecretKeySpec(key, ALGORITHM)
val cipher = Cipher.getInstance(FULL_ALGORITHM)
val ivParameterSpec = IvParameterSpec(initVector.toByteArray())
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec)
return cipher.doFinal(plainText)
}
/**
* Decrypts the given byte array
*
* @param cipherText The data to decrypt
*/
@Throws(Exception::class)
fun decrypt(cipherText: ByteArray, start: Int = 0, end: Int = cipherText.size): ByteArray {
val secretKey = SecretKeySpec(key, ALGORITHM)
val ivParameterSpec = IvParameterSpec(initVector.toByteArray())
val cipher = Cipher.getInstance(FULL_ALGORITHM)
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec)
return cipher.doFinal(cipherText, start, end)
}
companion object {
private const val ALGORITHM = "AES"
private const val FULL_ALGORITHM = "AES/CBC/PKCS5Padding"
}
}