標籤:mba ini 編碼方式 logs string 轉換 ring actor odi
加密方法說明:
1.根據約定秘鑰,採用Des密碼編譯演算法進行加密產生位元組數組;
2.將加密後產生的位元組數組採用BASE64演算法編碼;
解密方法說明:
1.採用BASE64演算法對密文進行解碼產生位元組數組;
2.根據約定的秘鑰,對產生的位元組數組進行採用Des解密;
java代碼:
package com.ab.mediation.util; import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader; import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** * 對外介面資料加密/解密類 * @author xin * */public class DesUtil { private final static String DES = "DES"; public static void main(String[] args) throws Exception {String tDoc = "";// 請求報文String encoding = "GBK";// 將函數參數賦給本地參數String path = "D:\\111111.xml";// String path = "F:\\111111\\111111.xml";String path1 = path;// 初始設定檔案對象fFile f = new File(path1);// 初始化讀資料流對象readerInputStreamReader reader = new InputStreamReader(new FileInputStream(path1), encoding);// 根據f檔案長度初始化字串資料c[]char c[] = new char[(int) (f.length())];// 取到字串長度,並將檔案f內容寫入數組cint length = reader.read(c);// 逐位元組將字串數組c[],賦給變數tDocfor (int i = 0; i < length; i++) {tDoc = tDoc + c[i];}// System.out.println(tDoc); String key = "111111111";System.out.println(encrypt(tDoc, key));System.out.println(decrypt(encrypt(tDoc, key), key)); } /** * Description 根據索引值進行加密 * * @param data * @param key * 加密鍵byte數組 * @return * @throws Exception */public static String encrypt(String data, String key) throws Exception {byte[] bt = encrypt(data.getBytes(), key.getBytes());String strs = new BASE64Encoder().encode(bt);return strs;} /** * 指定字元編碼方式並加密 * @param data * @param key * @param encoding * @return * @throws Exception */public static String encrypt(String data, String key, String encoding) throws Exception {byte[] bt = encrypt(data.getBytes(encoding), key.getBytes());String strs = new BASE64Encoder().encode(bt);return strs;} /** * Description 根據索引值進行解密 * * @param data * @param key * 加密鍵byte數組 * @return * @throws IOException * @throws Exception */public static String decrypt(String data, String key) throws IOException,Exception {if (data == null)return null;BASE64Decoder decoder = new BASE64Decoder();byte[] buf = decoder.decodeBuffer(data);byte[] bt = decrypt(buf, key.getBytes());return new String(bt);} /** * 根據索引值解密並返回指定編碼方式字串 * @param data * @param key * @param encoding * @return * @throws IOException * @throws Exception */public static String decrypt(String data, String key, String encoding) throws IOException,Exception {if (data == null)return null;BASE64Decoder decoder = new BASE64Decoder();byte[] buf = decoder.decodeBuffer(data);byte[] bt = decrypt(buf, key.getBytes());return new String(bt, encoding);} /** * Description 根據索引值進行加密 * * @param data * @param key * 加密鍵byte數組 * @return * @throws Exception */private static byte[] encrypt(byte[] data, byte[] key) throws Exception {// 產生一個可信任的隨機數源SecureRandom sr = new SecureRandom(); // 從原始密鑰資料建立DESKeySpec對象DESKeySpec dks = new DESKeySpec(key); // 建立一個密鑰工廠,然後用它把DESKeySpec轉換成SecretKey對象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks); // Cipher對象實際完成加密操作Cipher cipher = Cipher.getInstance(DES); // 用密鑰初始化Cipher對象cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data);} /** * Description 根據索引值進行解密 * * @param data * @param key * 加密鍵byte數組 * @return * @throws Exception */private static byte[] decrypt(byte[] data, byte[] key) throws Exception {// 產生一個可信任的隨機數源SecureRandom sr = new SecureRandom(); // 從原始密鑰資料建立DESKeySpec對象DESKeySpec dks = new DESKeySpec(key); // 建立一個密鑰工廠,然後用它把DESKeySpec轉換成SecretKey對象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks); // Cipher對象實際完成解密操作Cipher cipher = Cipher.getInstance(DES); // 用密鑰初始化Cipher對象cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data);}}
對應的.net代碼:
public static string DesDecrypt(string str, string key) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); key = key.Substring(0, 8);//截取前八位 DES.Key = ASCIIEncoding.Default.GetBytes(key); DES.Mode = CipherMode.ECB; DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform DESDecrypt = DES.CreateDecryptor(); string result = ""; try { byte[] Buffer = Convert.FromBase64String(str); result = Encoding.Default.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); } catch (CryptographicException e) { Console.WriteLine(" error : {0}", e.Message); return null; } return result; } public static string DesEncrypt(string str, string key) { string res = ""; key = key.Substring(0,8);//截取前八位 try { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = Encoding.UTF8.GetBytes(key); DES.Mode = CipherMode.ECB; DES.Padding = PaddingMode.PKCS7; ICryptoTransform DESEncrypt = DES.CreateEncryptor(); byte[] Buffer = Encoding.GetEncoding("GBK").GetBytes(str); res = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); } catch (CryptographicException e) { Console.WriteLine(" error : {0}", e.Message); return null; } return res; }
des加解密(java和.net)