paulwong

          javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes 解決辦法

          如果用RSA加密數(shù)據(jù)的話,會(huì)有數(shù)據(jù)長(zhǎng)度的要求,否則會(huì)拋異常:
          javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes

          推薦的做法:

          1. 隨機(jī)生成一個(gè)密鑰,用作對(duì)稱密鑰UUID
          2. 用此對(duì)稱密鑰,用對(duì)稱加密法AES加密數(shù)據(jù)
          3. 用RSA的公鑰加密此對(duì)稱密鑰
          4. 發(fā)送加密后的對(duì)稱密鑰和加密數(shù)據(jù)
          5. 用RSA私鑰解密加密后的對(duì)稱密鑰
          6. 用解密密后的對(duì)稱密鑰,解密數(shù)據(jù)
          7. 完成
          AESSecurityUtil.java
          import java.security.Key;
          import java.util.UUID;

          import javax.crypto.Cipher;
          import javax.crypto.spec.SecretKeySpec;

          import sun.misc.BASE64Decoder;
          import sun.misc.BASE64Encoder;

          public class AESSecurityUtil {

              // 加密算法
              /** 指定加密算法為RSA */
              private static final String ALGORITHM = "AES";

              // 加密密鑰
              
          // private static final byte[] keyValue = new byte[] { 'T', 'h', 'e',
              
          // 'B','e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
              
          // 16位的加密密鑰
          //    private byte[] keyValue;

              /**
               * 用來進(jìn)行加密的操作
               * 
               * 
          @param Data
               * 
          @return
               * 
          @throws Exception
               
          */
              public static String encrypt(String keyString, String data)
                      throws Exception {
                  Key key = generateKey(keyString);
                  Cipher c = Cipher.getInstance(ALGORITHM);
                  c.init(Cipher.ENCRYPT_MODE, key);
                  byte[] encVal = c.doFinal(data.getBytes());
                  String encryptedValue = new BASE64Encoder().encode(encVal);
                  return encryptedValue;
              }

              /**
               * 用來進(jìn)行解密的操作
               * 
               * 
          @param encryptedData
               * 
          @return
               * 
          @throws Exception
               
          */
              public static String decrypt(String keyString, String encryptedData) throws Exception {
                  Key key = generateKey(keyString);
                  Cipher c = Cipher.getInstance(ALGORITHM);
                  c.init(Cipher.DECRYPT_MODE, key);
                  byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
                  byte[] decValue = c.doFinal(decordedValue);
                  String decryptedValue = new String(decValue);
                  return decryptedValue;
              }
              
              public static String generateKeyString()
              {
                  //必須長(zhǎng)度為16
                  return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
              }

              /**
               * 根據(jù)密鑰和算法生成Key
               * 
               * 
          @return
               * 
          @throws Exception
               
          */
              private static Key generateKey(String keyString) throws Exception {
                  Key key = new SecretKeySpec(keyString.getBytes(), ALGORITHM);
                  return key;
              }
              
              public static void main(String [] args) throws Exception
              {
                  String keyString = generateKeyString();
          //        String keyString = "1234567890123456";
                  System.out.println("密鑰:" + keyString);
                  
                  String source = "恭喜發(fā)財(cái)!";// 要加密的字符串
                  System.out.println("準(zhǔn)備用密鑰加密的字符串為:" + source);
                  
                  String cryptograph = encrypt(keyString, source);// 生成的密文
                  System.out.print("用密鑰加密后的結(jié)果為:" + cryptograph);
                  System.out.println();

                  String target = decrypt(keyString, cryptograph);// 解密密文
                  System.out.println("用密鑰解密后的字符串為:" + target);
                  System.out.println();
              }

          }


          CryptoUtil.java
          import com.tcl.project7.boss.common.crypto.CryptoData;
          import com.tcl.project7.boss.common.util.JsonManager;
          import com.tcl.project7.boss.common.util.file.FileUtil;
          import com.tcl.project7.boss.gameapplication.yearendactivities.bigwheelgame.player.valueobject.BigWheelGameRequest;

          public class CryptoUtil {
              
              public static CryptoData encrypt(String data) throws Exception
              {
                  //1、產(chǎn)生AES密鑰
                  String keyString = AESSecurityUtil.generateKeyString();
                  
                  //2、用AES法加密數(shù)據(jù)
                  String cryptograph = AESSecurityUtil.encrypt(keyString, data);
                  
                  //3、用RSA加密AES密鑰
                  String finalKey = RSASecurityUtil.encrypt(keyString);
          //        System.out.print("用RSA加密AES密鑰為:" + finalKey);
          //        System.out.print("加密數(shù)據(jù):" + cryptograph);
                  
                  CryptoData cryptoData = new CryptoData();
                  cryptoData.setKey(finalKey);
                  cryptoData.setContent(cryptograph);
                  
                  //4、返回?cái)?shù)據(jù)
                  return cryptoData;
              }
              
              public static String decrypt(String keyString, String data) throws Exception
              {
                  //1、解密密鑰
                  String decryptKeyString = RSASecurityUtil.decrypt(keyString);
                  
                  //2、解密內(nèi)容
                  String decryptData = AESSecurityUtil.decrypt(decryptKeyString, data);
                  
                  //3、返回
                  return decryptData;
                  
              }
              
              public static void main(String [] args) throws Exception
              {
                  String aFilePath = "DATA/TESTING-FILE/TOCRYPTO/tocrypto.txt";
                  String source = FileUtil.getContents(aFilePath);
                  
                  CryptoData cryptoData = encrypt(source);
                  System.out.print(cryptoData);
                  
                  String target = decrypt(cryptoData.getKey(), cryptoData.getContent());
                  System.out.print(target);
                  
                  BigWheelGameRequest bigWheelGameRequest = JsonManager.getBean(target, BigWheelGameRequest.class);
                  System.out.print(bigWheelGameRequest);
              }

          }


          CryptoData.java
          import java.io.Serializable;

          public class CryptoData implements Serializable{

              private static final long serialVersionUID = -4774469372648172844L;
              
              private String key;
              
              private String content;

              public String getKey() {
                  return key;
              }

              public void setKey(String key) {
                  this.key = key;
              }

              public String getContent() {
                  return content;
              }

              public void setContent(String content) {
                  this.content = content;
              }

              public String toString() {
                  return "CryptoData [key=" + key + ", content=" + content + "]";
              }

          }

          posted on 2015-11-18 15:27 paulwong 閱讀(14663) 評(píng)論(0)  編輯  收藏 所屬分類: J2SE

          主站蜘蛛池模板: 双辽市| 林口县| 扎鲁特旗| 福建省| 濮阳县| 富锦市| 滨州市| 林周县| 林芝县| 建昌县| 宜春市| 措勤县| 津南区| 杨浦区| 渑池县| 泉州市| 咸阳市| 隆安县| 丹寨县| 澄迈县| 嘉善县| 临湘市| 宁远县| 慈溪市| 张北县| 定结县| 庐江县| 铜川市| 丰城市| 康马县| 驻马店市| 洛南县| 南安市| 宜州市| 礼泉县| 兴城市| 丹阳市| 玉山县| 沐川县| 资阳市| 平昌县|