C#加密解密代码如下
public static String sKey = "xxxxxx";
/// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static String Encrypt(String EncryptText)
{
return Encrypt(EncryptText, sKey);
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static String Encrypt(String Text, String sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
// DESKeySpec k = new DESKeySpec(sKey.getBytes("UTF-8"));
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
/// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static String Decrypt(String MD5Text)
{
return Decrypt(MD5Text, sKey);
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static String Decrypt(String Text, String sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
我写的解密java代码如下
private final byte[] DESkey = DigestUtils.md5Hex("xxxxxx").substring(0, 8).getBytes("UTF-8");// 设置密钥
private final byte[] DESIV = DigestUtils.md5Hex("xxxxxx").substring(0, 8).getBytes("UTF-8");// 设置向量
private AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现
private Key key = null;
public CryptoTools() throws Exception {
DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
iv = new IvParameterSpec(DESIV);// 设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
key = keyFactory.generateSecret(keySpec);// 得到密钥对象
}
public String decode(String data) throws Exception {
Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
deCipher.init(Cipher.DECRYPT_MODE, key, iv);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] inputByteArray = base64Decoder.decodeBuffer(data);
byte[] pasByte = deCipher.doFinal(inputByteArray);
return new String(pasByte, "UTF-8");
}
public static void main(String[] args) {
try {
String test = "0608E27EC1E8A308A84E87D1A6FBC3B0";
CryptoTools des = new CryptoTools();//自定义密钥
System.out.println("加密前的字符:"+test);
System.out.println("解密后的字符:"+des.decode(test));
} catch (Exception e) {
e.printStackTrace();
}
}
测试运行一直报错,求大神解答
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at com.walch.wx.util.md5.util.CryptoTools.decode(CryptoTools.java:59)
at com.walch.wx.util.md5.util.CryptoTools.main(CryptoTools.java:69)
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [util.c:840]
加解密的编码是否相同,是否都是字节或者字符串
MD5 是不可逆算法。不能通过密钥来对密文进行解密,解密只能靠暴力破解。
如果需要可逆的加密方法,请考虑使用 DES,3DES,AES,RSA 等。