Kotlin ECC 加密
Kotlin ECC Encryption
Kotlin 中是否有任何关于椭圆曲线加密的信息?
用于生成密钥对和加密、解密消息。
关于这个主题的信息很少甚至没有。
我想实现 ECC P-521 椭圆曲线的例子。
是否可以在 Kotlin 中使用 Java 版本?
我们如何实施?
ECC 提供 ECIES,这是一种将基于 ECC 的非对称加密与对称加密相结合的混合加密方案。这里生成一个共享秘密,从中导出用于数据对称加密的密钥。 MAC 用于身份验证。 ECIES 在各种加密标准中都有规定。可以找到更多详细信息 here。
ECIES 使用您在问题中列出的组件(通过 ECC 共享机密、对称加密、MAC 进行身份验证)。但是,具体算法取决于所使用的标准或实现,因此您无法直接控制它们。如果这对你来说足够了,ECIES 将是一个不错的选择。
支持 ECIES,例如由 BouncyCastle 开发,它实现了 IEEE P 1363a 标准。要使用 ECIES,BouncyCastle must therefore first be added (e.g. for the Android Studio in the dependencies section of app/gradle), see also :
implementation 'org.bouncycastle:bcprov-jdk15to18:1.67'
然后,以下 Kotlin 代码使用 ECIES 和 NIST P-521 执行 encryption/decryption:
// Add BouncyCastle
Security.removeProvider("BC")
Security.addProvider(BouncyCastleProvider())
// Key Pair Generation
val keyPairGenerator = KeyPairGenerator.getInstance("ECDH")
keyPairGenerator.initialize(ECGenParameterSpec("secp521r1"))
val keyPair = keyPairGenerator.generateKeyPair()
// Encryption
val plaintext = "The quick brown fox jumps over the lazy dog".toByteArray(StandardCharsets.UTF_8)
val cipherEnc = Cipher.getInstance("ECIES")
cipherEnc.init(Cipher.ENCRYPT_MODE, keyPair.public) // In practice, the public key of the recipient side is used
val ciphertext = cipherEnc.doFinal(plaintext)
// Decryption
val cipherDec = Cipher.getInstance("ECIES")
cipherDec.init(Cipher.DECRYPT_MODE, keyPair.private)
val decrypted = cipherDec.doFinal(ciphertext)
println(String(decrypted, StandardCharsets.UTF_8))
测试 API 级别 28 / Android 9 派。
如果您想更好地控制所使用的算法,可以手动实现各个组件,例如
- ECDH 与 NIST P-521 确定共享秘密
- SHA-512 将 AES-256 密钥确定为哈希的前 32 个字节(另请参阅 here 以了解在 ECIES 上下文中使用 KDF)
- AES-256/GCM 用于对称加密(GCM 已经是经过身份验证的加密,因此不需要显式 MAC)
然后,以下 Kotlin 代码使用这些组件执行 encryption/decryption:
// Generate Keys
val keyPairA = generateKeyPair()
val keyPairB = generateKeyPair()
// Generate shared secrets
val sharedSecretA = getSharedSecret(keyPairA.private, keyPairB.public)
val sharedSecretB = getSharedSecret(keyPairB.private, keyPairA.public)
// Generate AES-keys
val aesKeyA = getAESKey(sharedSecretA)
val aesKeyB = getAESKey(sharedSecretB)
// Encryption (WLOG by A)
val plaintextA = "The quick brown fox jumps over the lazy dog".toByteArray(StandardCharsets.UTF_8)
val ciphertextA = encrypt(aesKeyA, plaintextA)
// Decryption (WLOG by B)
val plaintextB = decrypt(aesKeyB, ciphertextA)
println(String(plaintextB, StandardCharsets.UTF_8))
与:
private fun generateKeyPair(): KeyPair {
val keyPairGenerator = KeyPairGenerator.getInstance("EC")
keyPairGenerator.initialize(ECGenParameterSpec("secp521r1"))
return keyPairGenerator.generateKeyPair()
}
private fun getSharedSecret(privateKey: PrivateKey, publicKey: PublicKey): ByteArray {
val keyAgreement = KeyAgreement.getInstance("ECDH")
keyAgreement.init(privateKey)
keyAgreement.doPhase(publicKey, true)
return keyAgreement.generateSecret()
}
private fun getAESKey(sharedSecret: ByteArray): ByteArray {
val digest = MessageDigest.getInstance("SHA-512")
return digest.digest(sharedSecret).copyOfRange(0, 32)
}
private fun encrypt(aesKey: ByteArray, plaintext: ByteArray): ByteArray {
val secretKeySpec = SecretKeySpec(aesKey, "AES")
val iv = ByteArray(12) // Create random IV, 12 bytes for GCM
SecureRandom().nextBytes(iv)
val gCMParameterSpec = GCMParameterSpec(128, iv)
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gCMParameterSpec)
val ciphertext = cipher.doFinal(plaintext)
val ivCiphertext = ByteArray(iv.size + ciphertext.size) // Concatenate IV and ciphertext (the MAC is implicitly appended to the ciphertext)
System.arraycopy(iv, 0, ivCiphertext, 0, iv.size)
System.arraycopy(ciphertext, 0, ivCiphertext, iv.size, ciphertext.size)
return ivCiphertext
}
private fun decrypt(aesKey: ByteArray, ivCiphertext: ByteArray): ByteArray {
val secretKeySpec = SecretKeySpec(aesKey, "AES")
val iv = ivCiphertext.copyOfRange(0, 12) // Separate IV
val ciphertext = ivCiphertext.copyOfRange(12, ivCiphertext.size) // Separate ciphertext (the MAC is implicitly separated from the ciphertext)
val gCMParameterSpec = GCMParameterSpec(128, iv)
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gCMParameterSpec)
return cipher.doFinal(ciphertext)
}
再次测试 API 28 级/Android 9 派。
Kotlin 中是否有任何关于椭圆曲线加密的信息?
用于生成密钥对和加密、解密消息。
关于这个主题的信息很少甚至没有。
我想实现 ECC P-521 椭圆曲线的例子。
是否可以在 Kotlin 中使用 Java 版本?
我们如何实施?
ECC 提供 ECIES,这是一种将基于 ECC 的非对称加密与对称加密相结合的混合加密方案。这里生成一个共享秘密,从中导出用于数据对称加密的密钥。 MAC 用于身份验证。 ECIES 在各种加密标准中都有规定。可以找到更多详细信息 here。
ECIES 使用您在问题中列出的组件(通过 ECC 共享机密、对称加密、MAC 进行身份验证)。但是,具体算法取决于所使用的标准或实现,因此您无法直接控制它们。如果这对你来说足够了,ECIES 将是一个不错的选择。
支持 ECIES,例如由 BouncyCastle 开发,它实现了 IEEE P 1363a 标准。要使用 ECIES,BouncyCastle must therefore first be added (e.g. for the Android Studio in the dependencies section of app/gradle), see also
implementation 'org.bouncycastle:bcprov-jdk15to18:1.67'
然后,以下 Kotlin 代码使用 ECIES 和 NIST P-521 执行 encryption/decryption:
// Add BouncyCastle
Security.removeProvider("BC")
Security.addProvider(BouncyCastleProvider())
// Key Pair Generation
val keyPairGenerator = KeyPairGenerator.getInstance("ECDH")
keyPairGenerator.initialize(ECGenParameterSpec("secp521r1"))
val keyPair = keyPairGenerator.generateKeyPair()
// Encryption
val plaintext = "The quick brown fox jumps over the lazy dog".toByteArray(StandardCharsets.UTF_8)
val cipherEnc = Cipher.getInstance("ECIES")
cipherEnc.init(Cipher.ENCRYPT_MODE, keyPair.public) // In practice, the public key of the recipient side is used
val ciphertext = cipherEnc.doFinal(plaintext)
// Decryption
val cipherDec = Cipher.getInstance("ECIES")
cipherDec.init(Cipher.DECRYPT_MODE, keyPair.private)
val decrypted = cipherDec.doFinal(ciphertext)
println(String(decrypted, StandardCharsets.UTF_8))
测试 API 级别 28 / Android 9 派。
如果您想更好地控制所使用的算法,可以手动实现各个组件,例如
- ECDH 与 NIST P-521 确定共享秘密
- SHA-512 将 AES-256 密钥确定为哈希的前 32 个字节(另请参阅 here 以了解在 ECIES 上下文中使用 KDF)
- AES-256/GCM 用于对称加密(GCM 已经是经过身份验证的加密,因此不需要显式 MAC)
然后,以下 Kotlin 代码使用这些组件执行 encryption/decryption:
// Generate Keys
val keyPairA = generateKeyPair()
val keyPairB = generateKeyPair()
// Generate shared secrets
val sharedSecretA = getSharedSecret(keyPairA.private, keyPairB.public)
val sharedSecretB = getSharedSecret(keyPairB.private, keyPairA.public)
// Generate AES-keys
val aesKeyA = getAESKey(sharedSecretA)
val aesKeyB = getAESKey(sharedSecretB)
// Encryption (WLOG by A)
val plaintextA = "The quick brown fox jumps over the lazy dog".toByteArray(StandardCharsets.UTF_8)
val ciphertextA = encrypt(aesKeyA, plaintextA)
// Decryption (WLOG by B)
val plaintextB = decrypt(aesKeyB, ciphertextA)
println(String(plaintextB, StandardCharsets.UTF_8))
与:
private fun generateKeyPair(): KeyPair {
val keyPairGenerator = KeyPairGenerator.getInstance("EC")
keyPairGenerator.initialize(ECGenParameterSpec("secp521r1"))
return keyPairGenerator.generateKeyPair()
}
private fun getSharedSecret(privateKey: PrivateKey, publicKey: PublicKey): ByteArray {
val keyAgreement = KeyAgreement.getInstance("ECDH")
keyAgreement.init(privateKey)
keyAgreement.doPhase(publicKey, true)
return keyAgreement.generateSecret()
}
private fun getAESKey(sharedSecret: ByteArray): ByteArray {
val digest = MessageDigest.getInstance("SHA-512")
return digest.digest(sharedSecret).copyOfRange(0, 32)
}
private fun encrypt(aesKey: ByteArray, plaintext: ByteArray): ByteArray {
val secretKeySpec = SecretKeySpec(aesKey, "AES")
val iv = ByteArray(12) // Create random IV, 12 bytes for GCM
SecureRandom().nextBytes(iv)
val gCMParameterSpec = GCMParameterSpec(128, iv)
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gCMParameterSpec)
val ciphertext = cipher.doFinal(plaintext)
val ivCiphertext = ByteArray(iv.size + ciphertext.size) // Concatenate IV and ciphertext (the MAC is implicitly appended to the ciphertext)
System.arraycopy(iv, 0, ivCiphertext, 0, iv.size)
System.arraycopy(ciphertext, 0, ivCiphertext, iv.size, ciphertext.size)
return ivCiphertext
}
private fun decrypt(aesKey: ByteArray, ivCiphertext: ByteArray): ByteArray {
val secretKeySpec = SecretKeySpec(aesKey, "AES")
val iv = ivCiphertext.copyOfRange(0, 12) // Separate IV
val ciphertext = ivCiphertext.copyOfRange(12, ivCiphertext.size) // Separate ciphertext (the MAC is implicitly separated from the ciphertext)
val gCMParameterSpec = GCMParameterSpec(128, iv)
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gCMParameterSpec)
return cipher.doFinal(ciphertext)
}
再次测试 API 28 级/Android 9 派。