非对称加密算法之java实现

非对称加密算法的秘钥分为公钥和私钥,公钥和私钥通常情况下是成对出现的,使用公钥加密的数据只有和它对应的私钥才能解开,反之亦然.在非对称加密算法中有一些算法是可以公钥加密,私钥解密的,反过来,私钥加密,公钥解密,下面是一些常见的非对称加密算法:

  • DH(Diffie-Hellman)秘钥交换算法
  • RSA–基于大数因子分解
  • ElGamal–基于离散对数
  • ECC(Elliptical curve Cryptograhpy)–椭圆曲线加密

非对称算法通过构建本地秘钥, 解决了对称加密算法对于传递秘钥的安全性

  1. DH(Diffie-Hellman)秘钥交换算法

    秘钥长度 默认 工作模式 填充方式 实现方
    512~1024(64倍数) 1024 jdk

    代码实现DH算法
try {
            //1.初始化发送方秘钥
            KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator.getInstance("DH");
            senderKeyPairGenerator.initialize(512);
            KeyPair senderKeyPair = senderKeyPairGenerator.generateKeyPair();
            byte[] senderPublicBytes = senderKeyPair.getPublic().getEncoded();//发送方秘钥,发送给接收方(通过网络,文件,U盘等)

            //2.初始化接收方秘钥,从发送方的公钥获取参数,再用这些参数去生成本地秘钥
            KeyFactory receiverKeyFactory = KeyFactory.getInstance("DH");
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(senderPublicBytes);
            PublicKey receiverPublicKey = receiverKeyFactory.generatePublic(x509EncodedKeySpec);
            DHParameterSpec dhParameterSpec = ((DHPublicKey)receiverPublicKey).getParams();
            KeyPairGenerator receiverKeyPairGenerator = KeyPairGenerator.getInstance("DH");
            receiverKeyPairGenerator.initialize(dhParameterSpec);
            KeyPair receiverKeyPair = receiverKeyPairGenerator.generateKeyPair();
            PrivateKey receiverPriveteKey = receiverKeyPair.getPrivate();
            byte[] receiverPublicBytes = receiverKeyPair.getPublic().getEncoded(); //发送给另一方

            //3.构建秘钥
            KeyAgreement receiverKeyAgreement = KeyAgreement.getInstance("DH");
            receiverKeyAgreement.init(receiverPriveteKey);
            receiverKeyAgreement.doPhase(receiverPublicKey, true);
            SecretKey receiverDesKey = receiverKeyAgreement.generateSecret("DES");
            //发送方构建秘钥,先读取在构建
            KeyFactory senderKeyFactory = KeyFactory.getInstance("DH");
            x509EncodedKeySpec = new X509EncodedKeySpec(receiverPublicBytes);
            PublicKey senderPublicKey = senderKeyFactory.generatePublic(x509EncodedKeySpec);

            KeyAgreement senderKeyAgreement = KeyAgreement.getInstance("DH");
            senderKeyAgreement.init(senderKeyPair.getPrivate());
            senderKeyAgreement.doPhase(senderPublicKey, true);
            SecretKey senderDesKey = senderKeyAgreement.generateSecret("DES");
            if(Objects.equals(senderDesKey, receiverDesKey))
            {
                System.out.println("两者秘钥相同");
            }

            //加密
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, senderDesKey);
            byte[] result = cipher.doFinal(str.getBytes());
            System.out.println("jdk dh en:"+ Base64.toBase64String(result));
            //解密
            cipher.init(Cipher.DECRYPT_MODE, receiverDesKey);
            byte[] raw = cipher.doFinal(result);
            System.out.println("jdk dh de:"+ new String(raw));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  1. RSA非对称加密算法
    唯一广泛接受并实现
    数据加密&数字签名
    既可公钥加密,私钥解密
    又可私钥加密,公钥解密非对称加密算法之java实现_第1张图片

    代码实现

try {
            //初始化秘钥
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(512);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();

            //私钥加密,公钥解密--加密
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
            KeyFactory factory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = factory.generatePrivate(pkcs8EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            byte[] result = cipher.doFinal(str.getBytes());
            System.out.println("私加公解 rsa en:"+Base64.toBase64String(result));

            //私钥加密,公钥解密--解密
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
            factory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = factory.generatePublic(x509EncodedKeySpec);
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            byte[] raw = cipher.doFinal(result);
            System.out.println("私加公解 rsa de:"+ new String(raw));

            //公钥加密,私钥解密--加密
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] result2 = cipher.doFinal(str.getBytes());
            System.out.println("公加私解 rsa en:"+Base64.toBase64String(result2));

            //公钥加密,私钥解密--解密
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] raw2 = cipher.doFinal(result2);
            System.out.println("公加私解 rsa de:" + new String (raw2));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

你可能感兴趣的:(非对称加密算法)