RSA非对称加密算法

RSA 基于因子分解

package com.encode;

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;

/**
 * Created by chris on 2019/2/23.
 */
public class RSA {

    private static int keySize = 512;

    private static RSAPublicKey rsaPublicKey;

    private static RSAPrivateKey rsaPrivateKey;

    static {
        // 初始化
        try {
            KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
            rsa.initialize(keySize);
            KeyPair keyPair = rsa.generateKeyPair();
            rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
            rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 私钥加密
     * @param src
     * @return
     * @throws Exception
     */
    public static String privateKeyEncode(String src) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
        KeyFactory rsa = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = rsa.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(src.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 私钥解密
     * @param src
     * @return
     * @throws Exception
     */
    public static String privateKeyDecode(String src) throws Exception {
        byte[] bytes = Base64.decodeBase64(src);
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
        KeyFactory rsa = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = rsa.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(bytes);
        return new String(result);
    }

    /**
     * 公钥加密
     * @param src
     * @return
     * @throws Exception
     */
    public static String publicKeyEncode(String src) throws Exception {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
        KeyFactory rsa = KeyFactory.getInstance("RSA");
        PublicKey publicKey = rsa.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(src.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 公钥解密
     * @param src
     * @return
     * @throws Exception
     */
    public static String publicKeyDecode(String src) throws Exception {
        byte[] bytes = Base64.decodeBase64(src);
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
        KeyFactory rsa = KeyFactory.getInstance("RSA");
        PublicKey publicKey = rsa.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(bytes);
        return new String(result);
    }

    /**
     * 获取公钥
     * @return
     */
    public static String getPublicKey() {
        return Base64.encodeBase64String(rsaPublicKey.getEncoded());
    }

    /**
     * 获取私钥
     * @return
     */
    public static String getPrivateKey() {
        return Base64.encodeBase64String(rsaPrivateKey.getEncoded());
    }

    public static void main(String[] args) throws Exception {
        System.out.println("public key:" + getPublicKey());
        System.out.println("private key:" + getPrivateKey());
        String src = "hello world rsa ";
        String s = privateKeyEncode(src);
        System.out.println(s);
        System.out.println(s.length());
        String s1 = publicKeyDecode(s);
        System.out.println(s1);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值