如何在Java项目中实现一个非对称加密算法?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
具体如下:
对称加密算法在加密和解密时使用的是同一个秘钥;而非对称加密算法需要两个密钥来进行加密和解密,这两个秘钥是公开密钥(public key,简称公钥)和私有密钥(private key,简称私钥)。
是一种 高级的双保险加密方式,一般的实现加密方式有DH密钥交换算法,RSA基于因子分解算法,ElGamal离散对数算法及ECC椭圆曲线加密等。
DH加密解密
public class EncryptDH {private static String dhStr = "encrypt test by DH";public static void main(String[] args) {jdkDh();}private static void jdkDh() {try {// 初始化发送方密钥KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator.getInstance("DH");senderKeyPairGenerator.initialize(512);KeyPair senderKeyPair = senderKeyPairGenerator.generateKeyPair();byte[] senderPublicKeyEnc = senderKeyPair.getPublic().getEncoded();// 发送方的公钥,发送给接受方,发送方式多种,比如文件,网络等// 初始化接受方密钥KeyFactory receiverKeyFactory = KeyFactory.getInstance("DH");X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(senderPublicKeyEnc);PublicKey receiverPublicKey = receiverKeyFactory.generatePublic(x509EncodedKeySpec);DHParameterSpec dhParameterSpec = ((DHPublicKey) receiverPublicKey).getParams();KeyPairGenerator receiverKeyPairGenerator = KeyPairGenerator.getInstance("DH");receiverKeyPairGenerator.initialize(dhParameterSpec);KeyPair receiveKeyPair = receiverKeyPairGenerator.generateKeyPair();PrivateKey receiverPrivateKey = receiveKeyPair.getPrivate();byte[] receiverPublicKeyEnc = receiveKeyPair.getPublic().getEncoded();// 密钥构建KeyAgreement receiverKeyAgreement = KeyAgreement.getInstance("DH");receiverKeyAgreement.init(receiverPrivateKey);receiverKeyAgreement.doPhase(receiverPublicKey, true);SecretKey receiverSecretKey = receiverKeyAgreement.generateSecret("DES");KeyFactory senderKeyFactory = KeyFactory.getInstance("DH");x509EncodedKeySpec = new X509EncodedKeySpec(receiverPublicKeyEnc);PublicKey senderPublicKey = senderKeyFactory.generatePublic(x509EncodedKeySpec);KeyAgreement senderKeyAgreement = KeyAgreement.getInstance("DH");senderKeyAgreement.init(senderKeyPair.getPrivate());senderKeyAgreement.doPhase(senderPublicKey, true);SecretKey snederSecretKey = senderKeyAgreement.generateSecret("DES");if (Objects.equals(receiverSecretKey, snederSecretKey)) {System.out.println("双方密钥相同");}// 加密Cipher cipher = Cipher.getInstance("DES");cipher.init(Cipher.ENCRYPT_MODE, snederSecretKey);byte[] result = cipher.doFinal(dhStr.getBytes());System.out.println("DH加密后为:" + Base64.encode(result));// 解密cipher.init(Cipher.DECRYPT_MODE, receiverSecretKey);result = cipher.doFinal(result);System.out.println("DH解密后为:" + new String(result));}catch (Exception e) {e.printStackTrace();}}}
RSA加密解密
public class EncryptRSA {private static String rsaStr = "encrypt test by ElGamal";public static void main(String[] args) {jdkRSA();}private static void jdkRSA() {try {// 初始化密钥KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");keyPairGenerator.initialize(512);KeyPair keyPair = keyPairGenerator.generateKeyPair();RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();System.out.println("RSA公钥:" + Base64.encode(rsaPublicKey.getEncoded()));System.out.println("RSA私钥:" + Base64.encode(rsaPrivateKey.getEncoded()));// 私钥加密,公钥解密--加密PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());KeyFactory keyFactory = KeyFactory.getInstance("RSA");PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, privateKey);byte[] result = cipher.doFinal(rsaStr.getBytes());System.out.println("RSA私钥加密,公钥解密--加密:" + Base64.encode(result));// 私钥加密,公钥解密--解密X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());keyFactory = KeyFactory.getInstance("RSA");PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, publicKey);result = cipher.doFinal(result);System.out.println("RSA私钥加密,公钥解密--解密:" + new String(result));// 公钥加密,私钥解密--加密x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());keyFactory = KeyFactory.getInstance("RSA");publicKey = keyFactory.generatePublic(x509EncodedKeySpec);cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publicKey);result = cipher.doFinal(rsaStr.getBytes());System.out.println("公钥加密,私钥解密--加密:" + Base64.encode(result));// 公钥加密,私钥解密--解密pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());keyFactory = KeyFactory.getInstance("RSA");privateKey= keyFactory.generatePrivate(pkcs8EncodedKeySpec);cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, privateKey);result = cipher.doFinal(result);System.out.println("公钥加密,私钥解密--解密:" + new String(result));}catch (Exception e) {e.printStackTrace();}}}
ElGamal加密
public class EncrypElGamal {private static String rsaStr = "encrypt test by ElGamal";public static void main(String[] args) {jdkRSA();}private static void jdkRSA() {try {// 公钥加密,私钥解密// Security.addProvider(new BouncyCastleProvider());//需要添加bouncycastleprovider jar//初始化密钥AlgorithmParameterGenerator algorithmParameterGenerator=AlgorithmParameterGenerator.getInstance("ElGamal");algorithmParameterGenerator.init(256);AlgorithmParameters algorithmParameters=algorithmParameterGenerator.generateParameters();DHParameterSpec dhParameterSpec=algorithmParameters.getParameterSpec(DHParameterSpec.class);KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("ElGamal");keyPairGenerator.initialize(dhParameterSpec,new SecureRandom());KeyPair keyPair=keyPairGenerator.generateKeyPair();PublicKey publicKey=keyPair.getPublic();PrivateKey privateKey=keyPair.getPrivate();System.out.println("ElGamal加密公钥:"+Base64.encode(publicKey.getEncoded()));System.out.println("ElGamal加密私钥:"+Base64.encode(privateKey.getEncoded()));//加密解密同Rsa是一样的}catch (Exception e) {e.printStackTrace();}}}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网行业资讯频道,感谢您对编程网的支持。