椭圆曲线加密算法,英文全称是Elliptic Curve Cryptography,缩写为ECC,是一种基于椭圆曲线数学的公开密钥加密算法,其本质是利用离散对数问题实现加密。
在1985年由Neal Koblitz和Victor Miller分别独立提出,与2004年开始得到广泛的使用。
相比于RSA算法,ECC可以使用更短的密钥,来实现与RSA相当或者更高安全。
根据研究,160位ECC加密安全性相当于1024位的RSA加密,210位ECC加密相当于2048位的RSA加密。
ECC算法在TLS、PGP和SSH等方面有着广泛的应用。
代码
需要使用maven依赖。
1 2 3 4 5 6
| <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.70</version> </dependency>
|
Java代码参考。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| @Test public void testECC() throws Exception{ Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator ecKeyGen = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME); ecKeyGen.initialize(new ECGenParameterSpec("secp256r1")); KeyPair ecKeyPair = ecKeyGen.generateKeyPair();
String message = "Hello World";
Cipher iesCipher = Cipher.getInstance("ECIESwithAES-CBC"); iesCipher.init(Cipher.ENCRYPT_MODE, ecKeyPair.getPublic()); byte[] ciphertext = iesCipher.doFinal(message.getBytes()); System.out.println(Hex.toHexString(ciphertext));
Cipher iesDecipher = Cipher.getInstance("ECIESwithAES-CBC"); iesDecipher.init(Cipher.DECRYPT_MODE, ecKeyPair.getPrivate(), iesCipher.getParameters()); byte[] plaintext = iesDecipher.doFinal(ciphertext); System.out.println(new String(plaintext)); }
|
参考
写给开发人员的实用密码学(七)—— 非对称密钥加密算法 RSA/ECC
Elliptic-curve cryptography
椭圆曲线加密算法原理解析(ECC)
Elliptic Curve Cryptography: a gentle introduction
椭圆曲线加密与NSA后门考古
Java使用Cipher类实现加密,包括DES,DES3,AES和RSA加密
AES五种加密模式(CBC、ECB、CTR、OCF、CFB)