夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks

           

          package com.roadway.sms.dao.test;

          import java.security.KeyFactory;
          import java.security.KeyPair;
          import java.security.KeyPairGenerator;
          import java.security.PrivateKey;
          import java.security.PublicKey;
          import java.security.spec.PKCS8EncodedKeySpec;
          import java.security.spec.X509EncodedKeySpec;

          import javax.crypto.Cipher;

          public class RSAUtil
          {
              
              
          public static void main(String[] args) throws Exception
              
          {
                  String[] ppk 
          = RSAUtil.getKeyPair();
                  System.out.println(
          "公鑰: " + ppk[0]);
                  System.out.println(
          "私鑰: " + ppk[1]);
                  
                  String content 
          = "123456惠萬鵬~!@#$%^&*()_+|";
                  System.out.println(
          "原字符串:    " + content);
                  
                  String encryptStr 
          = RSAUtil.encrypt(ppk[0], content);
                  System.out.println(
          "加密后字符串: " + encryptStr);
                  
                  String decryptStr 
          = RSAUtil.decrypt(ppk[1], encryptStr);
                  System.out.println(
          "解密后字符串: " + decryptStr);
              }

              
              
          public static String[] getKeyPair()
              
          {
                  String[] publicPrivateKey 
          = nullnull };
                  
          try
                  
          {
                      KeyPairGenerator keyPairGenerator 
          = KeyPairGenerator.getInstance("RSA");
                      keyPairGenerator.initialize(
          1024);
                      KeyPair keyPair 
          = keyPairGenerator.genKeyPair();
                      publicPrivateKey[
          0= parseByte2HexStr(keyPair.getPublic().getEncoded());
                      publicPrivateKey[
          1= parseByte2HexStr(keyPair.getPrivate().getEncoded());
                  }

                  
          catch (Exception ex)
                  
          {
                      ex.printStackTrace();
                  }

                  
          return publicPrivateKey;
              }

              
              
          public static String encrypt(String publicKeyCode, String content)
              
          {
                  String encryptRes 
          = null;
                  
          try
                  
          {
                      Cipher cipher 
          = Cipher.getInstance("RSA");
                      X509EncodedKeySpec keySpec 
          = new X509EncodedKeySpec(RSAUtil.parseHexStr2Byte(publicKeyCode));
                      KeyFactory keyFactory 
          = KeyFactory.getInstance("RSA");
                      PublicKey pubKey 
          = keyFactory.generatePublic(keySpec);
                      cipher.init(Cipher.ENCRYPT_MODE, pubKey);
                      
          byte[] publicKeyEncryptText = cipher.doFinal(content.getBytes());
                      encryptRes 
          = parseByte2HexStr(publicKeyEncryptText);
                  }

                  
          catch (Exception ex)
                  
          {
                      ex.printStackTrace();
                  }

                  
          return encryptRes;
              }

              
              
          public static String decrypt(String privateKeyCode, String content)
              
          {
                  String decryptRes 
          = null;
                  
          try
                  
          {
                      Cipher cipher 
          = Cipher.getInstance("RSA");
                      PKCS8EncodedKeySpec pkcs8KeySpec 
          = new PKCS8EncodedKeySpec(RSAUtil.parseHexStr2Byte(privateKeyCode));
                      KeyFactory keyFactory2 
          = KeyFactory.getInstance("RSA");
                      PrivateKey priKey 
          = keyFactory2.generatePrivate(pkcs8KeySpec);
                      cipher.init(Cipher.DECRYPT_MODE, priKey);
                      
          byte[] privateKeyDecryptText = cipher.doFinal(RSAUtil.parseHexStr2Byte(content));
                      decryptRes 
          = new String(privateKeyDecryptText);
                  }

                  
          catch (Exception ex)
                  
          {
                      ex.printStackTrace();
                  }

                  
          return decryptRes;
              }

              
              
          private static String parseByte2HexStr(byte buf[])
              
          {
                  StringBuffer sb 
          = new StringBuffer();
                  
          for (int i = 0; i < buf.length; i++)
                  
          {
                      String hex 
          = Integer.toHexString(buf[i] & 0xFF);
                      
          if (hex.length() == 1)
                      
          {
                          hex 
          = '0' + hex;
                      }

                      sb.append(hex.toUpperCase());
                  }

                  
          return sb.toString();
              }

              
              
          private static byte[] parseHexStr2Byte(String hexStr)
              
          {
                  
          if (hexStr.length() < 1)
                      
          return null;
                  
          byte[] result = new byte[hexStr.length() / 2];
                  
          for (int i = 0; i < hexStr.length() / 2; i++)
                  
          {
                      
          int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                      
          int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                      result[i] 
          = (byte) (high * 16 + low);
                  }

                  
          return result;
              }

          }
          posted on 2010-05-28 16:31 HUIKK 閱讀(269) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 三台县| 昭平县| 盘锦市| 无锡市| 河间市| 光泽县| 宾阳县| 延吉市| 沙河市| 西藏| 新乐市| 四子王旗| 大冶市| 庄河市| 丹阳市| 宝应县| 星子县| 当涂县| 靖边县| 肇州县| 溧阳市| 麻城市| 淮滨县| 建昌县| 丰都县| 乌恰县| 兴义市| 桦甸市| 瑞安市| 桃园县| 濮阳县| 鄂温| 阿尔山市| 钟祥市| 昭苏县| 石门县| 申扎县| 南皮县| 白沙| 阿拉尔市| 元朗区|