Kotlin ECC加密

4

关于Kotlin中的椭圆曲线加密(Elliptic Curve Encryption)是否有相关信息?

用于生成密钥对和加密、解密消息。

这个主题的信息非常少或没有。

我想要实现ECC P-521椭圆曲线,例如。

也许可以在Kotlin中使用Java版本吗?

我们如何实现呢?


请您能详细描述一下您的使用情况吗?一方面,您对消息的加密和解密提出了非常普遍的问题(这也可能意味着ECIES),另一方面具体地提到了用于密钥交换的ECDH,请参考这里了解它们之间的区别。另外,512位是什么意思?您是指NIST P-521曲线(也称为secp521r1,第11页)吗?这是用于Android开发吗? - Topaco
@Topaco,对我没有表达清楚表示歉意。确实是 NIST P-521,是用于 Android 开发的。我想要使用 P-521 椭圆曲线进行公钥计算,对称密码使用 AES-GCM,MAC 算法使用 HMAC-SHA512,如果不会太麻烦的话 :) 但是一个简单的 Java/Kotlin 基本 ECC 实现也可以。 - Riccoh
1个回答

11
ECC提供ECIES,这是一种混合加密方案,将基于ECC的非对称加密与对称加密相结合。在此,生成共享密钥,从中派生用于对数据进行对称加密的密钥。使用MAC进行身份验证。 ECIES在各种加密标准中指定。更多详细信息可以在 这里找到。
ECIES使用您在问题中列出的组件(通过ECC共享密钥,对称加密,用于身份验证的MAC)。但是,具体的算法取决于所使用的标准或实现,因此您无法直接控制它们。如果这对您足够了,ECIES将是一个不错的选择。
ECIES由BouncyCastle支持,其实现IEEE P 1363a标准。要使用ECIES,必须首先添加BouncyCastle(例如,在应用程序/ gradle的依赖项部分中为Android Studio),也请参见此处
implementation 'org.bouncycastle:bcprov-jdk15to18:1.67'

以下 Kotlin 代码使用 ECIES 和 NIST P-521 进行加密/解密操作:
// 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 Pie进行测试。


如果您想对使用的算法有更多控制权,可以手动实现各个组件,例如:
  • 使用NIST P-521的ECDH来确定共享密钥
  • 使用SHA-512确定AES-256密钥作为哈希的前32个字节(有关在ECIES上下文中使用KDF的信息,请参见此处
  • 使用AES-256/GCM进行对称加密(GCM已经是认证加密,因此不需要显式MAC)
然后,以下Kotlin代码使用这些组件执行加密/解密:
// 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 Level 28 / Android 9 Pie 进行测试。


2
谢谢!我希望这个主题能够帮助很多人,因为它确实对我有很大的帮助。我可以从这里找到自己的方向。它运行得非常好。谢谢你提供的所有信息,现在一切都有意义了。 - Riccoh
在API 26上进行了测试,对我有效,谢谢! - Someone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接