RSA加密解密以及使用Base64解决中文乱码问题

package test_rsa;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.*;

public class Skey_RSA {
    public static void main(String[] args) throws Exception {
        //创建密钥生成器,指定加密和解密算法为sra
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        //指定密钥的长度,初始化密钥生成器
        kpg.initialize(1024);
        //生成密钥对
        KeyPair kp = kpg.genKeyPair();

        //获取公钥
        PublicKey pbkey = kp.getPublic();
        //获取私钥
        PrivateKey prkey = kp.getPrivate();

        //保存公钥到文件
        FileOutputStream f1 = new FileOutputStream("Skey_RSA_pub.dat");
        ObjectOutputStream b1 = new ObjectOutputStream(f1);
        b1.writeObject(pbkey);

        //保存私钥到文件
        FileOutputStream f2 = new FileOutputStream("Skey_RSA_priv.dat");
        ObjectOutputStream b2 = new ObjectOutputStream(f2);
        b2.writeObject(prkey);
    }
}
package test_rsa;

import java.io.*;
import java.math.BigInteger;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;


public class Enc_RSA implements test_rsa.Base64 {
    public static void main(String[] args) throws  Exception{
        //需要加密的明文字符串
        String s1 = "15688888888,你好!哈哈.........";
        //使用Base64加密器加密
        String s= test_rsa.Base64.encodeBufferBase64(s1.getBytes());
        System.out.println("s:"+s);
        //从文件中读取公钥
        FileInputStream f = new FileInputStream("Skey_RSA_pub.dat");
        ObjectInputStream b = new ObjectInputStream(f);
        RSAPublicKey pbk = (RSAPublicKey) b.readObject();
        //rsa算法是使用整数进行加密的,在rsa公钥中包含两个整数信息,e和n,
        BigInteger e = pbk.getPublicExponent();
        BigInteger n = pbk.getModulus();
        System.out.println("e:"+e);
        System.out.println("n:"+n);
        //获取明文的大整数
        byte ptext[] = s.getBytes("utf-8");
        BigInteger m = new BigInteger(ptext);
        //加密明文
        BigInteger c = m.modPow(e, n);
        //打印密文
        System.out.println(c);
        //将密文以字符串的形式保存在文件中
        String cs = c.toString();
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Enc_RSA.dat")));
        out.write(cs,0,cs.length());
        out.close();

    }
}

package test_rsa;

import java.io.*;
import java.math.BigInteger;
import java.security.interfaces.RSAPrivateKey;

public class Dec_RSA {
    public static void main(String[] args) throws Exception {
        //读取密文
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("Enc_RSA.dat")));
        String ctext = in.readLine();
        BigInteger c = new BigInteger(ctext);
        //获取私钥
        FileInputStream f = new FileInputStream("Skey_RSA_priv.dat");
        ObjectInputStream b = new ObjectInputStream(f);
        RSAPrivateKey prk = (RSAPrivateKey) b.readObject();
        System.out.println("prk:" + prk);
        //获取私钥参数
        BigInteger d = prk.getPrivateExponent();
        BigInteger n = prk.getModulus();
        System.out.println("d:" + d);
        System.out.println("n:" + n);
        //解密明文
        BigInteger m = c.modPow(d, n);
        System.out.println("m:" + m);
        //计算明文对应的字符串
        byte[] mt = m.toByteArray();
        //使用Base64解密器解密并输出
        String r=new String(Base64.decodeBytes(mt));
        System.out.println("解密内容为:"+r);

    }
}

package test_rsa;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;

public interface Base64 {
    BASE64Encoder encoder = new BASE64Encoder();
    BASE64Decoder decoder = new BASE64Decoder();
    /**
     * BASE64 编码
     *
     * @param s
     * @return
     */
    public static String encodeBufferBase64(byte[] buff)
    {
        return buff == null?null:encoder.encodeBuffer(buff).trim();
    }


    /**
     * BASE64解码
     *
     * @param s
     * @return
     */
    public static byte[] decodeBufferBase64(String s)
    {
        try
        {
            return s == null ? null : decoder.decodeBuffer(s);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * base64编码
     *
     * @param bytes
     *            字符数组
     * @return
     * @throws IOException
     */
    public static String encodeBytes(byte[] bytes) throws IOException
    {
        return new BASE64Encoder().encode(bytes).replace("\n", "").replace("\r", "");
    }

    /**
     * base64解码
     *
     * @param bytes
     *            字符数组
     * @return
     * @throws IOException
     */
    public static String decodeBytes(byte[] bytes) throws IOException
    {
        return new String(new BASE64Decoder().decodeBuffer(new String(bytes)));
    }

}

你可能感兴趣的:(RSA加密解密以及使用Base64解决中文乱码问题)