RSA非对称加密解密例子

------要加密的内容

    String encryptText = "encryptText";

    keyPairGen.initialize(1024);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    // Generate keys
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    //存放公钥密钥路径。
    FileOutputStream pubfos = new FileOutputStream("E:/xlmPublicKey.dat");
    ObjectOutputStream puboos = new ObjectOutputStream(pubfos);
    //生成公钥密钥
    puboos.writeObject(publicKey);
    puboos.close();
    
    //保存私钥文件
    FileOutputStream prifos = new FileOutputStream("E:/xlmPrivateKey.dat");
    ObjectOutputStream prioos = new ObjectOutputStream(prifos);
    prioos.writeObject(privateKey);
    prioos.close();

   

---------采用公钥进行加密

    byte[] e = encrypt.encrypt2(privateKey, encryptText.getBytes());
    byte[] de = encrypt.decrypt2(publicKey, e);
    System.out.println(encrypt.bytesToString(e));
    System.out.println(encrypt.bytesToString(de));
    
    //保存密文
    FileOutputStream lfos = new FileOutputStream("E:/license.dat");
    DataOutputStream ldos=new DataOutputStream(lfos);
    ldos.write(e);

    ldos.close();




解密:

RSAEncrypt encrypt = new RSAEncrypt();
            // 生成实现指定摘要算法的 KeyPairGenerator 对象。RSA摘要
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
            
            FileInputStream pubfis = new FileInputStream("E:/xlmPublicKey.dat");
            ObjectInputStream puboos = new ObjectInputStream(pubfis);
            //生成公钥密钥
            RSAPublicKey pk=(RSAPublicKey)puboos.readObject();
            puboos.close();
            
            

            byte[] b = new byte[128];
            int len = 0;
            FileInputStream lfis = new FileInputStream("E:/license.dat");
            DataInputStream ldos=new DataInputStream(lfis);
            
            while ((len = ldos.read(b)) != -1) {
                ldos.read(b, 0, len);
            }
            byte[] de = encrypt.decrypt2(pk, b);
            System.out.println(encrypt.bytesToString(de));








/**
* 私钥加密
*
* @return byte[]
*/
protected byte[] encrypt2(RSAPrivateKey privateKey, byte[] obj) {
   if (privateKey != null) {
    try {
     Cipher cipher = Cipher.getInstance("RSA");
//     ENCRYPT_MODE : 用于将 cipher 初始化为加密模式的常量。
     cipher.init(Cipher.ENCRYPT_MODE, privateKey);
     return cipher.doFinal(obj);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   return null;
}

/**
* Basic decrypt method 公钥解密
*
* @return byte[]
*/
protected byte[] decrypt2(RSAPublicKey publicKey, byte[] obj) {
   if (publicKey != null) {
    try {
     Cipher cipher = Cipher.getInstance("RSA");
//     DECRYPT_MODE : 用于将 cipher 初始化为解密模式的常量。
     cipher.init(Cipher.DECRYPT_MODE, publicKey);
     return cipher.doFinal(obj);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }

   return null;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值