DIEA
BouncyCastle有Ed25519KeyPairGenerator、X25519KeyPairGenerator、PrivateKeyInfoFactory和SubjectPublicKeyInfoFactory可以帮助制作密钥。这是 C# 中的示例。将 ECKeyPairGenerator 替换为 X25519KeyPairGenerator。此示例使用标准密钥和 NIST 曲线,因为我无法让 Curve25519 使用 X25519 生成的密钥,因为当前实现不支持 oid。public static async Task Bouncy() { var originalSecret = "X25519 example"; var message = Encoding.UTF8.GetBytes(originalSecret); // Generate signing keys var gen = new Ed25519KeyPairGenerator(); gen.Init(new Ed25519KeyGenerationParameters(new SecureRandom())); var kp = gen.GenerateKeyPair(); // Sign data with private key var signer = new Ed25519Signer(); signer.Init(true, kp.Private); signer.BlockUpdate(message, 0, message.Length); var sig = signer.GenerateSignature(); // Verify signature with public key var verifier = new Ed25519Signer(); verifier.Init(false, kp.Public); verifier.BlockUpdate(message, 0, message.Length); var sigresult = verifier.VerifySignature(sig); // Generate encryption keys var genX = new ECKeyPairGenerator(); genX.Init(new KeyGenerationParameters(new SecureRandom(), 521)); var p1 = genX.GenerateKeyPair(); var p1_private = ECPrivateKeyStructure.GetInstance(PrivateKeyInfoFactory.CreatePrivateKeyInfo(p1.Private)); var p1_x25519_priv = new X25519PrivateKeyParameters(p1_private.GetDerEncoded(), 0); var p2 = genX.GenerateKeyPair(); var p2_public = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(p2.Public); var p2_x25519_pub = new X25519PublicKeyParameters(p2_public.GetDerEncoded(), 0); // Generate secret from keys var secret = new byte[32]; p1_x25519_priv.GenerateSecret(p2_x25519_pub, secret, 0); // Setup ECIES (Elliptical Curve Integrated Encryption Scheme) var gcm = new GcmBlockCipher(new AesEngine()); var ies = new IesEngine(new ECDHBasicAgreement(), new Kdf2BytesGenerator(new Sha512Digest()), new HMac(new Sha512Digest()), new PaddedBufferedBlockCipher(gcm.GetUnderlyingCipher())); // 256bit MAC, 256 key var p = new IesWithCipherParameters(secret, new byte[1], 256, 256); // Encrypt secret ies.Init(true, p1.Private, p2.Public, p); var encrypted = ies.ProcessBlock(message, 0, message.Length); // Decrypt secret ies.Init(false, p2.Private, p1.Public, p); var decrypted = ies.ProcessBlock(encrypted, 0, encrypted.Length); var decrypted_string = Encoding.UTF8.GetString(decrypted); }