A-A+
Java和.NET的DES加密
.NET的DES加密算法如下:
- /// <summary>
- /// DES加密算法必须使用Base64的Byte对象
- /// </summary>
- /// <param name="data">待加密的字符数据</param>
- /// <param name="key">密匙,长度必须为64位(byte[8]))</param>
- /// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
- /// <returns>加密后的字符</returns>
- private string EnDES(string data, byte[] key, byte[] iv)
- {
- DES des = DES.Create();
- //这行代码很重要,需要根据不同的字符串选择不同的转换格式
- byte[] tmp = Encoding.Unicode.GetBytes(data);
- Byte[] encryptoData;
- ICryptoTransform encryptor = des.CreateEncryptor(key, iv);
- using (MemoryStream memoryStream = new MemoryStream())
- {
- using (var cs = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
- {
- using (StreamWriter writer = new StreamWriter(cs))
- {
- writer.Write(data);
- writer.Flush();
- }
- }
- encryptoData = memoryStream.ToArray();
- }
- des.Clear();
- return Convert.ToBase64String(encryptoData);
- }
- /// <summary>
- /// DES解密算法
- /// </summary>
- /// <param name="data">待加密的字符数据</param>
- /// <param name="key">密匙,长度必须为64位(byte[8]))</param>
- /// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
- /// <returns>加密后的字符</returns>
- private string DeDes(string data, Byte[] key, Byte[] iv)
- {
- string resultData = string.Empty;
- //这行代码很重要
- Byte[] tmpData = Convert.FromBase64String(data);//转换的格式挺重要
- DES des = DES.Create();
- ICryptoTransform decryptor = des.CreateDecryptor(key, iv);
- using (var memoryStream = new MemoryStream(tmpData))
- {
- using (var cs = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
- {
- StreamReader reader = new StreamReader(cs);
- resultData = reader.ReadLine();
- }
- }
- return resultData;
- }
而在Java中,加密代码如下(以JSP为例):
- <%@ page import="javax.crypto.spec.DESKeySpec"%>
- <%@ page import="javax.crypto.SecretKey"%>
- <%@ page import="javax.crypto.Cipher"%>
- <%@ page import="javax.crypto.SecretKeyFactory"%>
- <%@ page import="javax.crypto.spec.IvParameterSpec"%>
- <%@ page import="sun.misc.BASE64Decoder"%>
- <%@ page import="sun.misc.BASE64Encoder"%>
- <%!
- /**
- *
- * 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
- *
- * 方法: void getKey(String strKey)从strKey的字条生成一个Key
- *
- * String getEncString(String strMing)对strMing进行加密,返回String密文 String
- * getDesString(String strMi)对strMin进行解密,返回String明文
- *
- * byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
- * byteD)byte[]型的解密
- */
- public class DESCrypt {
- SecretKey key;
- IvParameterSpec zeroIv;
- public DESCrypt(){
- getKey("A&&&&%G2");
- }
- /**
- * 根据参数生成KEY
- *
- * @param strKey
- */
- public void getKey(String strKey) {
- try {
- zeroIv = new IvParameterSpec(strKey.getBytes("UTF-8"));
- DESKeySpec desKey = new DESKeySpec(strKey.getBytes("UTF-8"));
- //创建一个密匙工厂,然后用它把DESKeySpec转换成
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
- key = keyFactory.generateSecret(desKey);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 加密String明文输入,String密文输出
- *
- * @param strMing
- * @return
- */
- public String getEncString(String strMing) {
- byte[] byteMi = null;
- byte[] byteMing = null;
- String strMi = "";
- BASE64Encoder base64en = new BASE64Encoder();
- try {
- byteMing = strMing.getBytes("UTF8");
- byteMi = this.getEncCode(byteMing);
- strMi = base64en.encode(byteMi);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- base64en = null;
- byteMing = null;
- byteMi = null;
- }
- return strMi;
- }
- /**
- * 解密 以String密文输入,String明文输出
- *
- * @param strMi
- * @return
- */
- public String getDesString(String strMi) {
- BASE64Decoder base64De = new BASE64Decoder();
- byte[] byteMing = null;
- byte[] byteMi = null;
- String strMing = "";
- try {
- byteMi = base64De.decodeBuffer(strMi);
- byteMing = this.getDesCode(byteMi);
- strMing = new String(byteMing, "UTF8");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- base64De = null;
- byteMing = null;
- byteMi = null;
- }
- return strMing;
- }
- /**
- * 加密以byte[]明文输入,byte[]密文输出
- *
- * @param byteS
- * @return
- */
- private byte[] getEncCode(byte[] byteS) {
- byte[] byteFina = null;
- Cipher cipher;
- try {
- cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
- byteFina = cipher.doFinal(byteS);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- cipher = null;
- }
- return byteFina;
- }
- /**
- * 解密以byte[]密文输入,以byte[]明文输出
- *
- * @param byteD
- * @return
- */
- private byte[] getDesCode(byte[] byteD) {
- Cipher cipher;
- byte[] byteFina = null;
- try {
- cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
- cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
- byteFina = cipher.doFinal(byteD);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- cipher = null;
- }
- return byteFina;
- }
- }
- %>
要想让.Net加密的字符串能够成功被Java解密,或者Java加密的字符串能够被.NET成功解密,其实只要保证Key和Iv相同即可。