Here’s a full encryption and decryption with a golden private key.
Security.addProvider(new BouncyCastleProvider());
KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
X509EncodedKeySpec publicKeySpec = new
X509EncodedKeySpec(fromHex("3059301306072a8648ce3d020106"
+
"082a8648ce3d03010703420004b629e654f0df6ebb559c8fa0a2e7902777030f4871b76d048beee5f505b556"
+
"8f32084eacf91eef6cce758f47bc82f8cb2137b28e33975ac26c052a1cbca22140"));
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
PKCS8EncodedKeySpec privateKeySpec = new
PKCS8EncodedKeySpec(fromHex("308193020100301306072a864"
+
"8ce3d020106082a8648ce3d0301070479307702010104209219a2380634f27db6e7a0871b377765f4630db76"
+
"d9ac73f5e6bc0c443bdecdba00a06082a8648ce3d030107a14403420004b629e654f0df6ebb559c8fa0a2e79"
+
"02777030f4871b76d048beee5f505b5568f32084eacf91eef6cce758f47bc82f8cb2137b28e33975ac26c052"
+ "a1cbca22140"));
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
KeyPair keys = new KeyPair(publicKey, privateKey);
byte[] data = randomByteString(100);
Cipher encryptCipher = Cipher.getInstance("ECIES/DHAES/NOPADDING", "BC");
encryptCipher.init(Cipher.ENCRYPT_MODE, keys.getPublic(), new
SecureRandom());
byte[] ciphertext = encryptCipher.doFinal(data);
IESParameterSpec parameterSpec = new IESParameterSpec(null, new
byte[0], 128, 128);
Cipher decryptCipher = Cipher.getInstance("ECIES/DHAES/NOPADDING", "BC");
decryptCipher.init(Cipher.DECRYPT_MODE, keys.getPrivate(),
parameterSpec, new SecureRandom());
byte[] decrypted = decryptCipher.doFinal(ciphertext);
assertThat(decrypted).isEqualTo(data);
And this will generate a new key pair:
Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator keyPairGenerator =
KeyPairGenerator.getInstance("EC", "BC");
keyPairGenerator.initialize(256, new SecureRandom());
KeyPair keys = keyPairGenerator.generateKeyPair();
--
--
You received this message because you are subscribed to the "Crypto++ Users"
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at
http://www.cryptopp.com.
---
You received this message because you are subscribed to the Google Groups
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.