java 版 rsa 加密例子

package com.hhhh;


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

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;


public class RSATest {

    /**
     * RSA公钥加密
     *
     * @param str
     *            加密字符串
     * @param publicKey
     *            公钥
     * @return 密文
     * @throws Exception
     *             加密过程中的异常信息
     */
    public static String encrypt( String str, String publicKey ) throws Exception{
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));

        return outStr;
    }

    /**
     * RSA私钥解密
     *
     * @param str
     *            加密字符串
     * @param privateKey
     *            私钥
     * @return 铭文
     * @throws Exception
     *             解密过程中的异常信息
     */
    public static String decrypt(String str, String privateKey) throws Exception{
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }


    public static void main(String args[]){

        System.out.println("==========================RSA加密=====================");

        String des3Str = "123456";
        String md5Str = MD5Utils.MD5Encode(des3Str,"utf8");
        System.out.println("md5后的字符串为:" + md5Str);

         //公钥
        String  publicKey =
                        "xxx" ;

        //私钥  pcks1格式
        String privateKey =
                        "xxx";

        String messageEn = null;
        try {
            messageEn = encrypt(md5Str,publicKey);
            System.out.println(md5Str + "\t加密后的字符串为:" + messageEn);
            String messageDe = decrypt(messageEn, privateKey);
            System.out.println("还原后的字符串为:" + messageDe);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
}

这个是 代码格式   记得公钥私钥  都是字符串,私钥是pcks1格式,非pcks8格式,格外重要哦!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Hutool进行RSA加密和解密的例子: ```java import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.RSA; public class RSATest { public static void main(String[] args) { // 生成RSA密钥对 RSA rsa = new RSA(); String publicKey = rsa.getPublicKeyBase64(); String privateKey = rsa.getPrivateKeyBase64(); System.out.println("公钥:" + publicKey); System.out.println("私钥:" + privateKey); // 加密 String data = "Hello, World!"; byte[] encrypt = rsa.encrypt(data.getBytes(), KeyType.PublicKey); String encryptStr = cn.hutool.core.codec.Base64.encode(encrypt); System.out.println("加密后的数据:" + encryptStr); // 解密 byte[] decrypt = rsa.decrypt(cn.hutool.core.codec.Base64.decode(encryptStr), KeyType.PrivateKey); String decryptStr = new String(decrypt); System.out.println("解密后的数据:" + decryptStr); } } ``` 在上面的例子中,我们首先使用`RSA`类生成RSA密钥对,然后使用公钥对数据进行加密,使用私钥对加密后的数据进行解密。在加密和解密时,我们需要指定密钥类型(公钥或私钥)。最后,我们将加密和解密后的数据进行输出验证。注意,我们在输出加密后的数据时,使用了`cn.hutool.core.codec.Base64.encode`方法将加密后的字节数组转换为Base64字符串,以便更方便地输出和传输。同样,在解密时,我们使用了`cn.hutool.core.codec.Base64.decode`方法将Base64字符串转换为字节数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值