为什么C#和java都使用DES加密,但是加密的结果不一样?
C#代码:publicstaticstringEncrypt(stringpToEncrypt,stringsKey){DESCryptoServiceProviderp...
C#代码:
public static string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(pToEncrypt);
provider.Key = Encoding.ASCII.GetBytes(sKey);
provider.IV = Encoding.ASCII.GetBytes(sKey);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray()) {
builder.AppendFormat("{0:X2}", num);
}
return builder.ToString();
}
java代码:
public static String encrypt(String message,String encoding) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(encoding));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes(encoding));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] buf = cipher.doFinal(message.getBytes(encoding));
String a = toHexString(buf).toUpperCase();
return a;
}
密钥是83569356。
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
return hexString.toString();
} 展开
public static string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(pToEncrypt);
provider.Key = Encoding.ASCII.GetBytes(sKey);
provider.IV = Encoding.ASCII.GetBytes(sKey);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray()) {
builder.AppendFormat("{0:X2}", num);
}
return builder.ToString();
}
java代码:
public static String encrypt(String message,String encoding) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(encoding));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes(encoding));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] buf = cipher.doFinal(message.getBytes(encoding));
String a = toHexString(buf).toUpperCase();
return a;
}
密钥是83569356。
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
return hexString.toString();
} 展开
展开全部
1.注意取字符串bytes是编码保持一致,不如都用Unicode
2.key和Iv保持一致
3.加密模式和填充方式保持一致
比如C#里
algo.Mode=CipherMode.ECB;
algo.Padding=PaddingMode.None;
则java里对应的为
final Cipher algo=Cipher.getInstance("DES/ECB/NoPadding");
2.key和Iv保持一致
3.加密模式和填充方式保持一致
比如C#里
algo.Mode=CipherMode.ECB;
algo.Padding=PaddingMode.None;
则java里对应的为
final Cipher algo=Cipher.getInstance("DES/ECB/NoPadding");
追问
C#是别人写的,我不能改。我只能改java代码。我应该怎改呢?
追答
没什么大问题,把你的encoding参数改称"ASCII"结果就一样了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询