c#的加密方法
base64
public static string Encrypt(string message, string key)
{
DES des = new DESCryptoServiceProvider();
des.Key = Encoding.UTF8.GetBytes(key);
des.IV = Encoding.UTF8.GetBytes(key);
byte[] bytes = Encoding.UTF8.GetBytes(message);
byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
return Convert.ToBase64String(resultBytes);
}
java的加密方法Des
public static byte[] desEncrypt(String message, String key) throws Exception {
Cipher cipher = Cipher.getInstance( "DES/CBC/PKCS5Padding ");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes( "UTF-8 "));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "DES ");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes( "UTF-8 "));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return cipher.doFinal(message.getBytes( "UTF-8 "));
}
在JAVA的调用时我再用
BASE64Encoder().encode( "内容 ")转换,当我两个地方同时输入aaa时C#和JAVA的加密结果一样,当我输入再长一些的字符串时他们的加密结果就不一样了,请问这是什么原因啊!
------解决方案--------------------
发贴: 2007-5-27 12:32:57
结贴: 2007-05-27 21:45:30