Kotlin ECC加密

问题描述

关于Kotlin中的椭圆曲线加密的信息吗?

用于生成密钥对以及加密,解密消息。

关于该主题的信息很少甚至没有。

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

是否可以在Kotlin中使用Java版本?

我们如何实现呢?

解决方法

ECC提供ECIES,一种混合​​加密方案,将基于ECC的非对称加密与对称加密相结合。在此生成一个共享机密,从中导出用于数据对称加密的密钥。 MAC用于身份验证。 ECIES在各种加密标准中都有规定。可以在here中找到更多详细信息。

ECIES使用您在问题中列出的组件(通过ECC共享的机密,对称加密,用于身份验证的MAC)。但是,具体算法取决于所使用的标准或实现,因此您无法直接控制它们。如果这对您足够,则ECIES将是一个不错的选择。

支持

ECIES,例如由BouncyCastle负责,它实现了IEEE P 1363a标准。要使用ECIES,因此必须首先添加BouncyCastle(例如,对于app / gradle的“依赖项”部分中的Android Studio),另请参见here

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个字节(另请参见here,如在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,ivCiphertext,iv.size)
    System.arraycopy(ciphertext,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,gCMParameterSpec)
    return cipher.doFinal(ciphertext)
}

再次使用API​​级别28 / Android 9 Pie测试。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...