ECDH鍵共有 - yamase0394/memo GitHub Wiki
Kotlin Spongy Castle
余分なのあるかも
import org.spongycastle.crypto.params.ECPublicKeyParameters
import org.spongycastle.crypto.params.KeyParameter
import org.spongycastle.crypto.util.PublicKeyFactory
import org.spongycastle.jce.ECNamedCurveTable
import org.spongycastle.jce.provider.BouncyCastleProvider
import org.spongycastle.jce.spec.ECPublicKeySpec
import java.security.KeyFactory
import java.security.KeyPairGenerator
import java.security.SecureRandom
import java.security.spec.ECGenParameterSpec
import javax.crypto.KeyAgreement
val provider = BouncyCastleProvider()
val ecKeyGen = KeyPairGenerator.getInstance("ECDH", provider)
ecKeyGen .initialize(ECGenParameterSpec("prime256v1"), SecureRandom())
val keyPair = ecKeyGen .generateKeyPair()
//送る
val kotlinPubKeyByte = keyPair.public.encoded
//受け取る
val csPubKeyByte = ...
val params = ECNamedCurveTable.getParameterSpec("prime256v1")
val csPubKeyParam = PublicKeyFactory.createKey(csPubKeyByte) as ECPublicKeyParameters
val keyFactory = KeyFactory.getInstance("ECDH", provider)
val csPubKey = keyFactory.generatePublic(ECPublicKeySpec(csPubKeyParam.q, params))
val keyAgree = KeyAgreement.getInstance("ECDH", provider)
keyAgree.init(keyPair.private)
keyAgree.doPhase(csPubKey, true)
val secretKey = keyAgree.generateSecret()
C# Bouncy Castle
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
var ecKeyGen = new ECKeyPairGenerator();
var keyGenParam = new KeyGenerationParameters(new SecureRandom(), 256);
ecKeyGen.Init(keyGenParam);
var keyPair = ecKeyGen.GenerateKeyPair();
//送る
var csPubKeyByte = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public).GetEncoded();
//受け取る
var kotlinPubKeyByte = ...
var kotlinPubKey = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(kotlinPubKeyByte);
var keyAgree = AgreementUtilities.GetBasicAgreement("ECDH");
keyAgree.Init(keyPair.Private);
var sharedSecret = keyAgree.CalculateAgreement(kotlinPubKey);
var secretKey = sharedSecret.ToByteArrayUnsigned();