• java rsa 加解密


    参考 http://blog.csdn.net/a394268045/article/details/52232120

    package rsa;
    
    import org.apache.commons.codec.binary.Hex;
    
    import javax.crypto.Cipher;
    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;
    
    /**
     * Created by Administrator on 2018/3/12.
     */
    public class Main {
        public static void main(String[] args) throws Exception {
            String source = "hhh测试";
            String cryptograph = encrypt(source);
            System.out.println("生成的密文-->" + cryptograph);
    
            String target = decrypt(cryptograph);
            System.out.println("解密密文-->" + target);
        }
    
        public static void generateKeyPair() throws Exception {
            SecureRandom sr = new SecureRandom();
    
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    
            kpg.initialize(512, sr);
    
            KeyPair kp = kpg.generateKeyPair();
    
            Key publicKey = kp.getPublic();
            Key privateKey = kp.getPrivate();
    
            ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream("e:/publicKey.keystore"));
            ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream("e:/privateKey.keystore"));
            oos1.writeObject(publicKey);
            oos2.writeObject(privateKey);
    
            oos1.close();
            oos2.close();
        }
    
        public static String encrypt(String source) throws Exception {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/publicKey.keystore"));
            Key key = (Key) ois.readObject();
            ois.close();
    
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] b = source.getBytes("utf-8");
    
            byte[] b1 = cipher.doFinal(b);
    //        return Base64.encodeBase64String(b1);
            return Hex.encodeHexString(b1);
        }
    
        public static String decrypt(String cryptograph) throws Exception {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/privateKey.keystore"));
            Key key = (Key) ois.readObject();
    
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, key);
    
    //        byte[] b1 = Base64.decodeBase64(cryptograph);
    //
    //        byte[] b = cipher.doFinal(b1);
    //
    //        return new String(b, "utf-8");
    
            byte[] b1 = Hex.decodeHex(cryptograph);
    
            byte[] b = cipher.doFinal(b1);
    
            return new String(b, "utf-8");
        }
    }
  • 相关阅读:
    Linux下运行java项目
    Matlab 绘图完整入门
    Matlab命令合集 妈妈再也不用担心我不会用matlab了
    详尽全面的matlab绘图教程
    拉格朗日乘子法 那些年学过的高数
    【转】几款网络仿真软件的比较
    正则表达式30分钟入门教程 ——堪称网上能找到的最好的正则式入门教程
    并发编程详细整理
    高并发通信模型NIO
    Java并发编程的艺术笔记(九)——FutureTask详解
  • 原文地址:https://www.cnblogs.com/white-knight/p/8548249.html
Copyright © 2020-2023  润新知