zoukankan      html  css  js  c++  java
  • 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");
        }
    }
  • 相关阅读:
    文献收录船舶建造的现代工程分解结构
    FLEX MDI窗口开发实例(2)
    收录国外造船信息Pursuit of a product work breakdown structure (PWBS)
    FLEX MDI窗口开发实例
    买了本<<flash actionscript 3.0殿堂之路>>
    今天我捐款了
    记Lambda的一个使用方法
    JXMS 低代码开发平台
    我的博客也Sliverlight
    代码编辑插件使用
  • 原文地址:https://www.cnblogs.com/white-knight/p/8548249.html
Copyright © 2011-2022 走看看