美文网首页
JAVA实现非对称加密

JAVA实现非对称加密

作者: Hey_Shaw | 来源:发表于2018-07-12 08:26 被阅读44次
  • 高级加密算法
  • 双保险
  • 公钥、私钥
  • DH(Diffie-Hellman)密钥交换算法
  • RSA - 基于因子分解
  • ElGamal - 基于离散对数
  • ECC(Elliptical Curve Cryptography)- 椭圆曲线加密

非对称加密算法 - DH(密钥交换)

  • 对称加密带来的困扰
    • 数据安全
    • 密钥管理复杂
    • 密钥传递过程复杂
  • 构建本地密钥
  • 对称
密钥长度 默认 工作模式 填充方式 实现方
512~1024(64倍数) 1024 JDK
  • 初始化发送方密钥
    • KeyPairGenerator:得到 KeyPair 对象
    • KeyPair:密钥载体(密钥对),包括两个信息:公钥、私钥。
    • PublicKey:公钥
  • 初始化接收方密钥
    • KeyFactory:密钥工厂(既可生成公钥,也可生成私钥),通过某种密钥规范还原密钥
    • X509EncodedKeySpec:根据 ASN.1 标准进行密钥编码;返回的是按照X.509规范进行编码的密钥字节
    • DHPublicKey:PublicKey的具体形式
    • DHParameterSpec:随同DH算法的参数集合
    • KeyPairGenerator
    • PrivateKey:私钥
  • 密钥构建
    • KeyAgreement:提供密钥一致性(或密钥交换)协议的功能。
    • SecretKey:秘密密钥(对称密钥),生成一个分组的秘密密钥,也提供一些类型安全的操作
    • KeyFactory
    • X509EncodedKeySpec
    • PrivateKey
  • 加密、解密
    • Cipher:为加密和解密提供密码功能的类;它是GCE框架的核心内容。
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;

import org.apache.commons.codec.binary.Base64;

import com.sun.org.apache.xalan.internal.utils.Objects;

public class DHTest{
    
    public static final String src = "dh test";

    public static void main(String[] args) {
        jdkDH();
    }
    
    // jdk实现:
    public static void jdkDH(){     
        try {
            // 1.初始化发送方密钥
            KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator.getInstance("DH");
            senderKeyPairGenerator.initialize(512);
            KeyPair senderKeyPair = senderKeyPairGenerator.generateKeyPair();
            // 发送方公钥,发送给接收方(网络、文件。。。)
            byte[] senderPublicKeyEnc = senderKeyPair.getPublic().getEncoded();
            
            // 2.初始化接收方密钥
            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 receiverKeypair = receiverKeyPairGenerator.generateKeyPair();
            PrivateKey receiverPrivateKey = receiverKeypair.getPrivate();
            byte[] receiverPublicKeyEnc = receiverKeypair.getPublic().getEncoded();
            
            // 3.密钥构建
            KeyAgreement receiverKeyAgreement = KeyAgreement.getInstance("DH");
            receiverKeyAgreement.init(receiverPrivateKey);
            receiverKeyAgreement.doPhase(receiverPublicKey, true);
            SecretKey receiverDesKey = 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 senderDesKey = senderKeyAgreement.generateSecret("DES");
            if(Objects.equals(receiverDesKey, senderDesKey))
            {
                System.out.println("双方密钥相同。");
            }
            
            // 4.加密
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, senderDesKey);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("bc dh encrypt:" + Base64.encodeBase64String(result));
            
            // 5.解密
            cipher.init(Cipher.DECRYPT_MODE, receiverDesKey);
            result = cipher.doFinal(result);
            System.out.println("bc dh decrypt:" + new String(result));
                        
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
非对称加密算法-DH

非对称加密算法-RSA

  • 唯一广泛接受并实现
  • 数据加密&数字签名
  • 公钥加密、私钥解密
  • 私钥加密、公钥解密
  • RSA相对比较慢
  • 基于“大数因子分解”非对称加密算法的实现方式
密钥长度 默认 工作模式 填充方式 实现方
512~65536(64整数倍) 1024 ECB NoPadding、PKCS1Padding、OAEPWITHMD5AndMGF1Pading、OAEPWITHSHA1AndMGF1Pading、OAEPWITHSHA256AndMGF1Pading、OAEPWITHSHA384AndMGF1Pading、OAEPWITHSHA512AndMGF1Pading JDK
2048 NONE NoPadding、PKCS1Padding、OAEPWITHMD5AndMGF1Pading、OAEPWITHSHA1AndMGF1Pading、OAEPWITHSHA224AndMGF1Pading、OAEPWITHSHA256AndMGF1Pading、OAEPWITHSHA384AndMGF1Pading、OAEPWITHSHA512AndMGF1Pading、ISO9796-1Padding BC
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;

import org.apache.commons.codec.binary.Base64;

import com.sun.org.apache.xalan.internal.utils.Objects;

public class RSATest {
    public static final String src = "rsa test";

    public static void main(String[] args) {
        jdkRSA();
    }
    
    // jdk实现:
    public static void jdkRSA(){        
        try {
            // 1.初始化发送方密钥
            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("Public Key:" + Base64.encodeBase64String(rsaPublicKey.getEncoded()));
            System.out.println("Private Key:" + Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
            
            // 2.私钥加密、公钥解密 ---- 加密
            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(src.getBytes());
            System.out.println("私钥加密、公钥解密 ---- 加密:" + Base64.encodeBase64String(result));
            
            // 3.私钥加密、公钥解密 ---- 解密
            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("私钥加密、公钥解密 ---- 解密:" + new String(result));
            
            
            
            // 4.公钥加密、私钥解密 ---- 加密
            X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
            KeyFactory keyFactory2 = KeyFactory.getInstance("RSA");
            PublicKey publicKey2 = keyFactory2.generatePublic(x509EncodedKeySpec2);
            Cipher cipher2 = Cipher.getInstance("RSA");
            cipher2.init(Cipher.ENCRYPT_MODE, publicKey2);
            byte[] result2 = cipher2.doFinal(src.getBytes());
            System.out.println("公钥加密、私钥解密 ---- 加密:" + Base64.encodeBase64String(result2));
            
            // 5.私钥解密、公钥加密 ---- 解密
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
            KeyFactory keyFactory5 = KeyFactory.getInstance("RSA");
            PrivateKey privateKey5 = keyFactory5.generatePrivate(pkcs8EncodedKeySpec5);
            Cipher cipher5 = Cipher.getInstance("RSA");
            cipher5.init(Cipher.DECRYPT_MODE, privateKey5);
            byte[] result5 = cipher5.doFinal(result2);
            System.out.println("公钥加密、私钥解密 ---- 解密:" + new String(result5));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
非对称加密算法-RSA

非对称加密算法-ElGamal

  • 只提供公钥加密算法
  • JDK并未提供实现,Bouncy Castle
密钥长度 默认 工作模式 填充方式 实现方
160~16384(8整数倍) 1024 ECB、NONE NoPadding、PKCS1Padding、OAEPWITHMD5AndMGF1Pading、OAEPWITHSHA1AndMGF1Pading、OAEPWITHSHA224AndMGF1Pading、OAEPWITHSHA256AndMGF1Pading、OAEPWITHSHA384AndMGF1Pading、OAEPWITHSHA512AndMGF1Pading、ISO9796-1Padding BC
import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
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 javax.crypto.spec.DHParameterSpec;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class ElGamalTest {

    public static final String src = "Elgamal test";

    public static void main(String[] args) {
        jdkElgamal();

    }
    
    /**
     *  
     *  对于:“Illegal key size or default parameters”异常,是因为美国的出口限制,Sun通过权限文件(local_policy.jar、US_export_policy.jar)做了相应限制。因此存在一些问题:
     *  Java 6 无政策限制文件:http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
     *  Java 7 无政策限制文件:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
     *  我的时java7,自己安装的。
     *  /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/security目录下,对应覆盖local_policy.jar和US_export_policy.jar两个文件。
     *  
     *  切换到%JDK_Home%\jre\lib\security目录下,对应覆盖local_policy.jar和US_export_policy.jar两个文件。同时,你可能有必要在%JRE_Home%\lib\security目录下,也需要对应覆盖这两个文件。
     */
    
    // jdk实现:“私钥解密、公钥加密” , 对于:“私钥加密、公钥解密”有问题,因为Elgamal不支持
    public static void jdkElgamal(){        
        try {
            // 加入对BouncyCastle支持  
            Security.addProvider(new BouncyCastleProvider());
            
            // 1.初始化发送方密钥
            AlgorithmParameterGenerator algorithmParameterGenerator = AlgorithmParameterGenerator.getInstance("Elgamal");
            // 初始化参数生成器
            algorithmParameterGenerator.init(256);
            // 生成算法参数
            AlgorithmParameters algorithmParameters = algorithmParameterGenerator.generateParameters();
            // 构建参数材料
            DHParameterSpec dhParameterSpec = (DHParameterSpec)algorithmParameters.getParameterSpec(DHParameterSpec.class);
            // 实例化密钥生成器
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("Elgamal");    
            // 初始化密钥对生成器  
            keyPairGenerator.initialize(dhParameterSpec, new SecureRandom());
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            // 公钥
            PublicKey elGamalPublicKey = keyPair.getPublic();
            // 私钥 
            PrivateKey elGamalPrivateKey = keyPair.getPrivate();
            System.out.println("Public Key:" + Base64.encodeBase64String(elGamalPublicKey.getEncoded()));
            System.out.println("Private Key:" + Base64.encodeBase64String(elGamalPrivateKey.getEncoded()));
            
            
            // 2.私钥解密、公钥加密 ---- 加密
            // 初始化公钥  
            // 密钥材料转换
            X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(elGamalPublicKey.getEncoded());
            // 实例化密钥工厂
            KeyFactory keyFactory2 = KeyFactory.getInstance("Elgamal");
            // 产生公钥
            PublicKey publicKey2 = keyFactory2.generatePublic(x509EncodedKeySpec2);
            // 数据加密 
            // Cipher cipher2 = Cipher.getInstance("Elgamal");
            Cipher cipher2 = Cipher.getInstance(keyFactory2.getAlgorithm()); 
            cipher2.init(Cipher.ENCRYPT_MODE, publicKey2);
            byte[] result2 = cipher2.doFinal(src.getBytes());
            System.out.println("私钥加密、公钥解密 ---- 加密:" + Base64.encodeBase64String(result2));
            
            // 3.私钥解密、公钥加密 ---- 解密
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(elGamalPrivateKey.getEncoded());
            KeyFactory keyFactory5 = KeyFactory.getInstance("Elgamal");
            PrivateKey privateKey5 = keyFactory5.generatePrivate(pkcs8EncodedKeySpec5);
//          Cipher cipher5 = Cipher.getInstance("Elgamal");
            Cipher cipher5 = Cipher.getInstance(keyFactory5.getAlgorithm()); 
            cipher5.init(Cipher.DECRYPT_MODE, privateKey5);
            byte[] result5 = cipher5.doFinal(result2);
            System.out.println("Elgamal 私钥加密、公钥解密 ---- 解密:" + new String(result5));
            
            

            /*  
            //  私钥加密、公钥解密: 有问题
            // 4.私钥加密、公钥解密 ---- 加密
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(elGamalPrivateKey.getEncoded());
            KeyFactory keyFactory = KeyFactory.getInstance("Elgamal");
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("Elgamal");
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("私钥加密、公钥解密 ---- 加密:" + Base64.encodeBase64String(result));
            
            // 5.私钥加密、公钥解密 ---- 解密
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(elGamalPublicKey.getEncoded());
            keyFactory = KeyFactory.getInstance("Elgamal");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            cipher = Cipher.getInstance("Elgamal");
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            result = cipher.doFinal(result);
            System.out.println("Elgamal 私钥加密、公钥解密 ---- 解密:" + new String(result));
            */
            
        } catch (Exception e) {     
            e.printStackTrace();
        }   
    }       
}
非对称加密算法-ElGamal

相关文章

  • Java实现常用的三种加密算法详解

    Java实现常用的三种加密算法详解 信息摘要算法、对称加密算法以及非对称加密算法。本文将利用Java实现这几种常见...

  • 2019-12-20

    RSA非对称加密技术(JAVA版实现加解密处理)介绍 非对称的加密技术就是指加密过程是不可逆,不能通过密文直...

  • Java实现Base64加密

    加密方式 对称加密 非对称加密 散列函数 md5 哈希 主要用于验证 数据签名 (非对称加密的具体应用) java...

  • JAVA实现非对称加密

    高级加密算法 双保险 公钥、私钥 DH(Diffie-Hellman)密钥交换算法 RSA - 基于因子分解 El...

  • JAVA加密系列(三)- 非对称加密算法 RSA、DSA

    JAVA加密系列(三)- 非对称加密算法 RSA、DSA 非对称加密算法需要两个密钥:公开密钥(publickey...

  • RSA非对称加密

    RSA非对称加密 RSA非对称加密, 适用于Java和iOS 应用场景:用户登录时对登录密码进行加密 启动终端, ...

  • java实现RSA非对称加密解密

    之前写过一篇java实现AES对称加密解密在对密码加密传输的场景下 RSA非对称加密解密可能会更加适合。原理就是后...

  • RSA加密

    RSA加密为非对称加密实现 对称加密:加密解密使用同一个算法 非对称加密:加密和解密使用不同算法 rsa加密原理 ...

  • day12 比特币的技术原理

    比特币的技术原理 去中心化·如何实现 非对称加密、分布式存储非对称加密:加密解密的时候,使用不同密钥的加密算法。 ...

  • golang实现RSA加密解密

    非对称加密示意图: 在此可以看到,非对称加密是通过两个密钥(公钥-私钥)来实现对数据的加密和解密的。公钥用于加密,...

网友评论

      本文标题:JAVA实现非对称加密

      本文链接:https://www.haomeiwen.com/subject/aojkpftx.html