基本信息
源码名称:java版:ECC加密解密示例源码
源码大小:0.02M
文件格式:.zip
开发语言:Java
更新时间:2019-09-19
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
maven中包含 org.bouncycastle和commons-codec
maven中包含 org.bouncycastle和commons-codec
package test9; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.Security; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import org.bouncycastle.jce.interfaces.ECPrivateKey; import org.bouncycastle.jce.interfaces.ECPublicKey; public class ECCUtil { static { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); } //生成秘钥对 public static KeyPair getKeyPair() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC"); keyPairGenerator.initialize(256, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); return keyPair; } //获取公钥(Base64编码) public static String getPublicKey(KeyPair keyPair) { ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic(); byte[] bytes = publicKey.getEncoded(); return test9.UtilHelper.byte2Base64(bytes); } //获取私钥(Base64编码) public static String getPrivateKey(KeyPair keyPair) { ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate(); byte[] bytes = privateKey.getEncoded(); return test9.UtilHelper.byte2Base64(bytes); } //将Base64编码后的公钥转换成PublicKey对象 public static ECPublicKey string2PublicKey(String pubStr) throws Exception { byte[] keyBytes = test9.UtilHelper.base642Byte(pubStr); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC"); ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic(keySpec); return publicKey; } //将Base64编码后的私钥转换成PrivateKey对象 public static ECPrivateKey string2PrivateKey(String priStr) throws Exception { byte[] keyBytes = test9.UtilHelper.base642Byte(priStr); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC"); ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate(keySpec); return privateKey; } //公钥加密 public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("ECIES", "BC"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] bytes = cipher.doFinal(content); return bytes; } //私钥解密 public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("ECIES", "BC"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] bytes = cipher.doFinal(content); return bytes; } public static void main(String[] args) throws Exception { KeyPair keyPair = ECCUtil.getKeyPair(); String publicKeyStr = ECCUtil.getPublicKey(keyPair); String privateKeyStr = ECCUtil.getPrivateKey(keyPair); System.out.println("ECC公钥Base64编码:" publicKeyStr); System.out.println("ECC私钥Base64编码:" privateKeyStr); ECPublicKey publicKey = string2PublicKey(publicKeyStr); ECPrivateKey privateKey = string2PrivateKey(privateKeyStr); byte[] publicEncrypt = publicEncrypt("hello world".getBytes(), publicKey); byte[] privateDecrypt = privateDecrypt(publicEncrypt, privateKey); System.out.println(new String(privateDecrypt)); } }