gpt4 book ai didi

c# - 3DES 加密实现报告来自 C# 实现的不同输出

转载 作者:太空宇宙 更新时间:2023-11-03 19:25:14 24 4
gpt4 key购买 nike

我一直在尝试使用 BouncyCaSTLe 套件在 Java 中使用 3DES 加密明文,但没有成功。此结果应该与现有 C# 实现生成的结果相匹配,因为我计划稍后对其进行解密。

虽然我确信我已经用 Java 生成了 C# 算法的“等价物”,但我不断得到不同的结果。有人可以仔细查看这两个片段并提出建议吗?我将不胜感激。

C# 加密:

public static byte[] encryptStringToBytes_3DES(string plainText, string passKey)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");

// Declare the streams used
// to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;
ASCIIEncoding ascii = new System.Text.ASCIIEncoding();


// used to encrypt the data.
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
string passphrase = passKey;
byte[] iv = ascii.GetBytes("AVREWASH");
byte[] key = ascii.GetBytes(passphrase);

try
{
// Create a TripleDES object
// with the specified key and IV.
//Console.WriteLine("Key size is " + tdes.KeySize+" and IV is "+tdes.IV+" and that of key is "+key.Length);
tdes.Key = key;
tdes.IV = iv;
tdes.Padding = PaddingMode.Zeros;

// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = tdes.CreateEncryptor(tdes.Key, tdes.IV);

// Create the streams used for encryption.
msEncrypt = new MemoryStream();
csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
swEncrypt = new StreamWriter(csEncrypt);

//Write all data to the stream.
swEncrypt.Write(plainText);

}
catch (Exception ex)
{
Console.WriteLine("Error is " + ex.Message);
while (true)
{
}
}

finally
{
// Clean things up.

// Close the streams.
if (swEncrypt != null)
swEncrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();

// Clear the TripleDES object.
if (tdes != null)
tdes.Clear();
}

// Return the encrypted bytes from the memory stream.
return msEncrypt.ToArray();

}

我在将结果转换为十六进制时使用了这个辅助函数...

public static string ByteArrayToString(byte[] ba)
{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}

本应执行“等效”加密的 Java 代码段也如下所示:

public void encrypt(String plaintext, String IV, String tripleDesKey){

try{

SecretKey keySpec = new SecretKeySpec(tripleDesKey.getBytes("US-ASCII"),"DESede");

IvParameterSpec iv = new IvParameterSpec(IV.getBytes("US-ASCII"));

Cipher e_cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
e_cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);

byte [] cipherText = e_cipher.doFinal(plaintext.trim().getBytes("US-ASCII"));

System.out.println("Ciphertext: " + asHex(cipherText));
}
catch(Exception exc){
ex.printStackTrace();
}
}

这里是它对应的Hex..函数

public static String asHex (byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;

for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");

strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}

return strbuf.toString();
}

请帮忙。

最佳答案

您正在使用不同的填充模式。在 C# 中,您编写了 tdes.Padding = PaddingMode.Zeros;在 Java 中,您在 CBC 模式下使用了 PKCS5Padding。这不是一回事。

关于c# - 3DES 加密实现报告来自 C# 实现的不同输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com