- 細節:
- 1、進行MD5加密,得到byte[]
- /**
- * 進行MD5加密
- * @param String 原始的SPKEY
- * @return byte[] 指定加密方式爲md5後的byte[]
- */
- private byte[] md5(String strSrc)
- {
- byte[] returnByte = null;
- try
- {
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- returnByte = md5.digest(strSrc.getBytes("GBK"));
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return returnByte;
- }
- 2、得到3-DES的密鑰匙
- /**
- * 得到3-DES的密鑰匙
- * 根據根據需要,如密鑰匙爲24個字節,md5加密出來的是16個字節,因此後面補8個字節的0
- * @param String 原始的SPKEY
- * @return byte[] 指定加密方式爲md5後的byte[]
- */
- private byte[] getEnKey(String spKey)
- {
- byte[] desKey=null;
- try
- {
- byte[] desKey1 = md5(spKey);
- desKey = new byte[24];
- int i = 0;
- while (i < desKey1.length && i < 24) {
- desKey[i] = desKey1[i];
- i++;
- }
- if (i < 24) {
- desKey[i] = 0;
- i++;
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return desKey;
- }
- 3、3-DES加密
- /**
- * 3-DES加密
- * @param byte[] src 要進行3-DES加密的byte[]
- * @param byte[] enKey 3-DES加密密鑰
- * @return byte[] 3-DES加密後的byte[]
- */
- public byte[] Encrypt(byte[] src,byte[] enKey)
- {
- byte[] encryptedData = null;
- try
- {
- DESedeKeySpec dks = new DESedeKeySpec(enKey);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
- SecretKey key = keyFactory.generateSecret(dks);
- Cipher cipher = Cipher.getInstance("DESede");
- cipher.init(Cipher.ENCRYPT_MODE, key);
- encryptedData = cipher.doFinal(src);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return encryptedData;
- }
- 4、對字符串進行Base64編碼
- /**
- * 對字符串進行Base64編碼
- * @param byte[] src 要進行編碼的字符
- *
- * @return String 進行編碼後的字符串
- */
- public String getBase64Encode(byte[] src)
- {
- String requestValue="";
- try{
- BASE64Encoder base64en = new BASE64Encoder();
- requestValue=base64en.encode(src);
- //System.out.println(requestValue);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return requestValue;
- }
- 5、根據需要可以去掉字符串的換行符號
- /**
- * 去掉字符串的換行符號
- * base64編碼3-DES的數據時,得到的字符串有換行符號,根據需要可以去掉
- */
- private String filter(String str)
- {
- String output = null;
- StringBuffer sb = new StringBuffer();
- for(int i = 0; i < str.length(); i++)
- {
- int asc = str.charAt(i);
- if(asc != 10 && asc != 13)
- sb.append(str.subSequence(i, i + 1));
- }
- output = new String(sb);
- return output;
- }
- 6、對字符串進行URLDecoder.encode(strEncoding)編碼
- /**
- * 對字符串進行URLDecoder.encode(strEncoding)編碼
- * @param String src 要進行編碼的字符串
- *
- * @return String 進行編碼後的字符串
- */
- public String getURLEncode(String src)
- {
- String requestValue="";
- try{
- requestValue = URLEncoder.encode(src);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return requestValue;
- }
- 7、對字符串進行URLDecoder.decode(strEncoding)解碼
- /**
- * 對字符串進行URLDecoder.decode(strEncoding)解碼
- * @param String src 要進行解碼的字符串
- *
- * @return String 進行解碼後的字符串
- */
- public String getURLDecoderdecode(String src)
- {
- String requestValue="";
- try{
- requestValue = URLDecoder.decode(src);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return requestValue;
- }
- 8、進行3-DES解密(密鑰匙等同於加密的密鑰匙)
- /**
- *
- *進行3-DES解密(密鑰匙等同於加密的密鑰匙)。
- * @param byte[] src 要進行3-DES解密byte[]
- * @param String spkey分配的SPKEY
- * @return String 3-DES解密後的String
- */
- public String deCrypt(byte[] debase64,String spKey)
- {
- String strDe = null;
- Cipher cipher = null;
- try
- {
- cipher=Cipher.getInstance("DESede");
- byte[] key = getEnKey(spKey);
- DESedeKeySpec dks = new DESedeKeySpec(key);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
- SecretKey sKey = keyFactory.generateSecret(dks);
- cipher.init(Cipher.DECRYPT_MODE, sKey);
- byte ciphertext[] = cipher.doFinal(debase64);
- strDe = new String(ciphertext,"UTF-16LE");
- }
- catch(Exception ex)
- {
- strDe = "";
- ex.printStackTrace();
- }
- return strDe;
- 經過以上步驟就可以完成MD5加密,3-DES加密、base64編碼傳輸、base64解碼、3-DES解密得到原文。