Kotlin ECC 암호화

Nov 11 2020

Kotlin 내에 Elliptic Curve Encryption에 대한 정보가 있습니까?

키 쌍을 생성하고 메시지를 암호화하고 해독합니다.

이 주제에 대한 정보는 거의 없습니다.

예를 들어 ECC P-521 타원 곡선을 구현하고 싶습니다.

Kotlin 내에서 Java 버전을 사용할 수 있습니까?

그리고 이것을 어떻게 구현합니까?

답변

2 Topaco Nov 17 2020 at 23:03

ECC는 ECC 기반 비대칭 암호화와 대칭 암호화를 결합한 하이브리드 암호화 체계 인 ECIES를 제공합니다. 여기에서 데이터의 대칭 암호화를위한 키가 파생되는 공유 비밀이 생성됩니다. MAC은 인증에 사용됩니다. ECIES는 다양한 암호화 표준에 지정되어 있습니다. 자세한 내용은 여기에서 확인할 수 있습니다 .

ECIES는 질문에 나열된 구성 요소를 사용합니다 (ECC를 통한 공유 비밀, 대칭 암호화, 인증을위한 MAC). 그러나 특정 알고리즘은 사용되는 표준 또는 구현에 따라 다르므로 직접 제어 할 수 없습니다. 이것이 당신에게 충분하다면 ECIES가 좋은 선택이 될 것입니다.

ECIES는 IEEE P 1363a 표준을 구현하는 BouncyCastle에 의해 지원됩니다. 따라서 ECIES를 사용하려면 먼저 BouncyCastle을 추가해야합니다 (예 : app / 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 해시의 처음 32 바이트와 AES-256 키를 결정 (참고 여기 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 레벨 28 / Android 9 Pie로 다시 테스트되었습니다.