RSA非对称加密算法

  1. 非对称加密算法使用过程:
    乙方生成两把密钥(公钥和私钥)
    甲方获取乙方的公钥,然后用它对信息加密。
    乙方得到加密后的信息,用私钥解密,乙方也可用私钥加密字符串
    甲方获取乙方私钥加密数据,用公钥解密
    优点:
    更安全,密钥越长,它就越难破解
    缺点:
    加密速度慢
    常用算法:
    RSA、Elgamal、背包算法、Rabin、D-H、ECC(椭圆曲线加密算法)

  2. 流程图如下:
    在这里插入图片描述

  3. 代码实现:

//import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

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

public class RSATest {
    /**
     * 加解密时所需的密码,内部使用可以直接写死,外部调用动态传输可以直接在形参中定义
     */
    private static final String password ="gjh%^&(&  {}77";

    /**
     * 使用RSA对字符串进行私钥加密
     * @param str
     * @return
     */
    public static byte[] jdkRASEncode(String str,KeyPair keyPair){
        try {
            //初始化密钥
            
            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();

            //私钥加密,公钥解密,私钥加密部分
            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 [] bytes = cipher.doFinal(str.getBytes());
            return bytes;//Base64.encode(bytes);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 使用RSA进行公钥解密
     * @param str
     * @return
     */
    public static String jdkRASDecode(byte[] byteContent,KeyPair keyPair){
        try {
            
            RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();

            //私钥加密,公钥解密,公钥解密部分
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE,publicKey);
            //byte [] byteContent = Base64.decode(str);
            byte [] bytes = cipher.doFinal(byteContent);
            return new String(bytes);

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

    public static void main(String[] args) throws NoSuchAlgorithmException {
    	//初始化密钥
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512,new SecureRandom(password.getBytes()));
        KeyPair keyPair =keyPairGenerator.generateKeyPair();
        String string = "welcome to first RSA";
        byte[] str = jdkRASEncode(string,keyPair);
        System.out.println(str);
        System.out.println(jdkRASDecode(str,keyPair));
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值