欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

AES/CBC/PKCS7Padding加密算法的C版与Java版实现(续)

最编程 2024-08-14 16:10:28
...
object AesCryptUtil { private const val AES_MODE = "AES/CBC/PKCS7Padding" private const val CHARSET = "UTF-8" private const val CIPHER = "AES" private const val HASH_ALGORITHM = "SHA-256" private val IV_BYTES = byteArrayOf('e'.toByte(), 'f'.toByte(), 'g'.toByte(), 'h'.toByte(), 'e'.toByte(), 'f'.toByte(), 'g'.toByte(), 'h'.toByte(), 'e'.toByte(), 'f'.toByte(), 'g'.toByte(), 'h'.toByte(), 'e'.toByte(), 'f'.toByte(), 'g'.toByte(), 'h'.toByte() ) @Throws(NoSuchAlgorithmException::class, UnsupportedEncodingException::class) private fun generateKey(password: ByteArray): SecretKeySpec { return SecretKeySpec(password, CIPHER) } @Throws(GeneralSecurityException::class) fun encrypt(password: String, message: String): String { try { val key = generateKey(password) val cipherText = encrypt(key, IV_BYTES, message.toByteArray(charset(CHARSET))) //NO_WRAP is important as was getting \n at the end return Base64Utils.encodeToString(cipherText) } catch (e: UnsupportedEncodingException) { throw GeneralSecurityException(e) } } @Throws(GeneralSecurityException::class) fun encrypt(password: ByteArray, message: String): String { try { val key = generateKey(password) val cipherText = encrypt(key, IV_BYTES, message.toByteArray()) // DDLog.info("cipherText=${cipherText.contentToString()}") for (b in cipherText) { val st = String.format("%02X", b) } //NO_WRAP is important as was getting \n at the end return String(Base64Utils.encode(cipherText)) } catch (e: UnsupportedEncodingException) { throw GeneralSecurityException(e) } } @Throws(GeneralSecurityException::class) fun encrypt(key: SecretKeySpec, iv: ByteArray, message: ByteArray): ByteArray { val cipher = Cipher.getInstance(AES_MODE, "BC") val ivSpec = IvParameterSpec(iv) cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec) return cipher.doFinal(message) } @Throws(GeneralSecurityException::class) fun decrypt(password: String, base64EncodedCipherText: String): String { try { val key = generateKey(password) val decodedCipherText = Base64Utils.decode(base64EncodedCipherText.toByteArray()) val decryptedBytes = decrypt(key, IV_BYTES, decodedCipherText) return String(decryptedBytes, charset(CHARSET)) } catch (e: UnsupportedEncodingException) { throw GeneralSecurityException(e) } } @Throws(GeneralSecurityException::class) fun decrypt(password: ByteArray, base64EncodedCipherText: String): String { try { val key = generateKey(password) val decodedCipherText = Base64Utils.decode(base64EncodedCipherText.toByteArray()) val decryptedBytes = decrypt(key, IV_BYTES, decodedCipherText) return String(decryptedBytes, charset(CHARSET)) } catch (e: UnsupportedEncodingException) { throw GeneralSecurityException(e) } } @Throws(GeneralSecurityException::class) fun decrypt(key: SecretKeySpec, iv: ByteArray, decodedCipherText: ByteArray): ByteArray { val cipher = Cipher.getInstance(AES_MODE) val ivSpec = IvParameterSpec(iv) cipher.init(Cipher.DECRYPT_MODE, key, ivSpec) return cipher.doFinal(decodedCipherText) } }