zoukankan      html  css  js  c++  java
  • 关于RSA加密算法的工具类

    关于RSA加密算法的工具类 

    最近在捣鼓SSO(单点登录),就是一个在应用(系统)登录之后,当切换其他应用(系统)的时候,可以省去登录,提高用户的使用的便捷。(具体有时间在写) 

    期间涉及的安全问题,发送数据涉及账户密码以及分布系统之间的信息安全问题。 

    Java代码  收藏代码
    1. package test.rsa;  
    2.   
    3. import java.io.*;  
    4. import java.math.BigInteger;  
    5. import java.security.*;  
    6. import java.security.interfaces.*;  
    7. import java.security.spec.*;  
    8. import javax.crypto.*;  
    9. import org.bouncycastle.util.encoders.Hex;  
    10. import sun.misc.*;  
    11.   
    12. public class TestRSA {  
    13.     /** 
    14.      * * 生成密钥对 * 
    15.      *  
    16.      * @return KeyPair * 
    17.      * @throws EncryptException 
    18.      */  
    19.     public static KeyPair generateKeyPair() throws Exception {  
    20.         try {  
    21.             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",  
    22.                     new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    23.             final int KEY_SIZE = 1024;// 块加密的大小,是不要太大,否则效率会低  
    24.             keyPairGen.initialize(KEY_SIZE, new SecureRandom());  
    25.             KeyPair keyPair = keyPairGen.generateKeyPair();  
    26.             saveKeyPair(keyPair);  
    27.             return keyPair;  
    28.         } catch (Exception e) {  
    29.             throw new Exception(e.getMessage());  
    30.         }  
    31.     }  
    32.   
    33.     public static KeyPair getKeyPair() throws Exception {  
    34.         FileInputStream fis = new FileInputStream(  
    35.                 "D:/javasoft/TempTest/RSAKey.txt");  
    36.         ObjectInputStream oos = new ObjectInputStream(fis);  
    37.         KeyPair kp = (KeyPair) oos.readObject();  
    38.         oos.close();  
    39.         fis.close();  
    40.         return kp;  
    41.     }  
    42.   
    43.     public static void saveKeyPair(KeyPair kp) throws Exception {  
    44.   
    45.         FileOutputStream fos = new FileOutputStream(  
    46.                 "D:/javasoft/TempTest/RSAKey.txt");  
    47.         ObjectOutputStream oos = new ObjectOutputStream(fos);  
    48.         // 生成密钥  
    49.         oos.writeObject(kp);  
    50.         oos.close();  
    51.         fos.close();  
    52.     }  
    53.   
    54.     /** 
    55.      * * 生成公钥 * 
    56.      *  
    57.      * @param modulus * 
    58.      * @param publicExponent * 
    59.      * @return RSAPublicKey * 
    60.      * @throws Exception 
    61.      */  
    62.     public static RSAPublicKey generateRSAPublicKey(byte[] modulus,  
    63.             byte[] publicExponent) throws Exception {  
    64.         KeyFactory keyFac = null;  
    65.         try {  
    66.             keyFac = KeyFactory.getInstance("RSA",  
    67.                     new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    68.         } catch (NoSuchAlgorithmException ex) {  
    69.             throw new Exception(ex.getMessage());  
    70.         }  
    71.   
    72.         RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(  
    73.                 modulus), new BigInteger(publicExponent));  
    74.         try {  
    75.             return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);  
    76.         } catch (InvalidKeySpecException ex) {  
    77.             throw new Exception(ex.getMessage());  
    78.         }  
    79.     }  
    80.   
    81.     /** 
    82.      * * 生成私钥 * 
    83.      *  
    84.      * @param modulus * 
    85.      * @param privateExponent * 
    86.      * @return RSAPrivateKey * 
    87.      * @throws Exception 
    88.      */  
    89.     public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus,  
    90.             byte[] privateExponent) throws Exception {  
    91.         KeyFactory keyFac = null;  
    92.         try {  
    93.             keyFac = KeyFactory.getInstance("RSA",  
    94.                     new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    95.         } catch (NoSuchAlgorithmException ex) {  
    96.             throw new Exception(ex.getMessage());  
    97.         }  
    98.   
    99.         RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(  
    100.                 modulus), new BigInteger(privateExponent));  
    101.         try {  
    102.             return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);  
    103.         } catch (InvalidKeySpecException ex) {  
    104.             throw new Exception(ex.getMessage());  
    105.         }  
    106.     }  
    107.   
    108.     /** 
    109.      * 加密的方法 
    110.      * @throws IOException  
    111.      * @throws NoSuchPaddingException  
    112.      * @throws NoSuchAlgorithmException  
    113.      */  
    114.     private static String encrypt(PublicKey pk,String source) throws Exception{  
    115.   
    116.         Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    117.         cipher.init(Cipher.ENCRYPT_MODE, pk);  
    118.         byte[] sbt = source.getBytes();  
    119.         byte[] epByte = cipher.doFinal(sbt);  
    120.         BASE64Encoder encoder = new BASE64Encoder();  
    121.         String epStr = encoder.encode(epByte);  
    122.         return epStr;  
    123.   
    124.     }  
    125.     private static String encrypt1(PublicKey pk,String source) throws Exception{  
    126.         Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    127.         cipher.init(Cipher.ENCRYPT_MODE, pk);  
    128.         byte[] s = source.getBytes();  
    129.         byte[] en_s = cipher.doFinal(s);  
    130.         return new String(Hex.encode(en_s));  
    131.   
    132.     }  
    133.     /** 
    134.      * 解密的方法 
    135.      * @throws Exception 
    136.      */  
    137.     public static String decrypt(PrivateKey pk,String source) throws Exception {  
    138.   
    139.         /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */  
    140.         Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    141.         cipher.init(Cipher.DECRYPT_MODE, pk);  
    142.         BASE64Decoder decoder = new BASE64Decoder();  
    143.         byte[] b1 = decoder.decodeBuffer(source);  
    144.         /** 执行解密操作 */  
    145.         byte[] b = cipher.doFinal(b1);  
    146.         return new String(b);  
    147.     }  
    148.     public static String decrypt1(PrivateKey pk,String source) throws Exception {  
    149.   
    150.         /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */  
    151.         Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());  
    152.         cipher.init(Cipher.DECRYPT_MODE, pk);  
    153.         /** 执行解密操作 */  
    154.         byte[] b = cipher.doFinal(Hex.decode(source));  
    155.         return new String(b);  
    156.     }  
    157.     public static void main(String[] args) throws Exception {  
    158.         String s = "1-Test-我";  
    159.         String en_s = encrypt1(getKeyPair().getPublic(), s);  
    160.         System.out.println("----------------分割线--------------------------");  
    161.         System.out.println("加密之后:");  
    162.         System.out.println(en_s);  
    163.           
    164.         System.out.println("----------------分割线--------------------------");  
    165.         String de_s = decrypt1(getKeyPair().getPrivate(),en_s);    
    166.         System.out.println("还原密文:");   
    167.         System.out.println(de_s);  
    168.     }  
    169. }  



  • 相关阅读:
    linux下ping命令出现ping: sendto: Network is unreachable
    tiny4412--linux驱动学习(2)
    tiny4412--linux驱动学习(1)
    linux-kernel-4.4 移植 (2)解决上部遗留DMA-PL330的问题
    linux-kernel-4.4 移植 (1)启动
    Busybox构建根文件系统和制作Ramdisk
    tiny4412 --Uboot移植(6) SD卡驱动,启动内核
    select响应事件
    项目总结1
    一个盒子只是显示两行
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/8617097.html
Copyright © 2011-2022 走看看