RSA加密

笔记:


import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

public class RSA {

    public static String ALGORITHM = "RSA";

    //指定key的位数
    public static int KEYSIZE = 1024; // 65536

    //指定公钥的存放文件
    public static String PUBLIC_KEY_FILE = "public_key.dat";

    //指定私钥存放的文件
    public static String PRICATE_KEY_FILE = "private_key.dat";

    public static void generateKeyPair() throws Exception{
        SecureRandom sr = new SecureRandom();
        //需要一个KeyPairGenerator来生成钥对
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
        keyPairGenerator.initialize(KEYSIZE,sr);
        //生成
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        Key publicKey = keyPair.getPublic();
        Key privateKey = keyPair.getPrivate();

        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
        ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(new FileOutputStream(PRICATE_KEY_FILE));

        objectOutputStream1.writeObject(publicKey);
        objectOutputStream2.writeObject(privateKey);
        objectOutputStream1.close();
        objectOutputStream2.close();
    }

    /**
     * 加密
     */
    public static String encrypt(String source) throws Exception{
        generateKeyPair();
        //取出公钥
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
        Key key = (Key) ois.readObject();
        ois.close();
        //开始使用
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE,key);
        byte[] b = source.getBytes();
        byte[] b1 = cipher.doFinal(b);
        //转base64
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(b1);
    }

    /**
     * 解密
     */
    public static String dencrypt(String source) throws Exception{
        //取出公钥
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRICATE_KEY_FILE));
        Key key = (Key) ois.readObject();
        ois.close();
        //开始使用
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE,key);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = decoder.decodeBuffer(source);
        byte[] b1 = cipher.doFinal(b);
        //转base64

        return new String(b1);
    }

    @Test
    public void test()throws Exception{
        String content = "wwy";
        String password = encrypt(content);
        System.out.println("密码: " + password);

        //到了服务器以后
        String target = dencrypt(password);
        System.out.println("明文:  " + target);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值