在C#中使用openssl来匹配linux中的openssl des-ede3-cbc

时间:2022-10-08 18:23:20

I need to encrypt the data in linux and decrypt this data in windows C# application. I am encrypted using following command in linux. openssl enc -des-ede3-cbc -K 3B388F0EFDA72AF16DBA734FE9704AF7 -iv 0000000000000000 -in file.txt -out file.enc. If try to decrypt in C# application using DLL provided in http://sourceforge.net/projects/openssl-net/. I am not able to get proper decrypted data. Please send me equivalent C# code to decrypt in windows.

我需要在linux中加密数据并在windows C#应用程序中解密这些数据。我在linux中使用以下命令加密。 openssl enc -des-ede3-cbc -K 3B388F0EFDA72AF16DBA734FE9704AF7 -iv 0000000000000000 -in file.txt -out file.enc。如果尝试使用http://sourceforge.net/projects/openssl-net/中提供的DLL在C#应用程序中解密。我无法获得正确的解密数据。请在Windows中向我发送等效的C#代码进行解密。

1 个解决方案

#1


1  

Take a look at MSDN examples for TripleDESCryptoServiceProvider and do not forget to set correct Mode, Key, IV and Padding properties of your instance:

查看TripleDESCryptoServiceProvider的MSDN示例,不要忘记设置实例的正确Mode,Key,IV和Padding属性:

var provider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
provider.Mode = CipherMode.CBC;
provider.Key = new byte[] { 0x3B, 0x38, 0x8F, 0x0E, 0xFD, 0xA7, 0x2A, 0xF1, 0x6D, 0xBA, 0x73, 0x4F, 0xE9, 0x70, 0x4A, 0xF7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
provider.IV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
provider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

You should also add -p option to your command so you can see the actual key used by OpenSSL for encryption:

您还应该在命令中添加-p选项,以便可以看到OpenSSL用于加密的实际密钥:

$ openssl enc -des-ede3-cbc -K 3B388F0EFDA72AF16DBA734FE9704AF7 -iv 0000000000000000 -in file.txt -out file.enc -nosalt -p
key=3B388F0EFDA72AF16DBA734FE9704AF70000000000000000
iv =0000000000000000

The key you are passing on the command line is too short so OpenSSL appends zeros to it and you need to use exactly the same key in your C# code.

您在命令行上传递的密钥太短,因此OpenSSL会向其添加零,您需要在C#代码中使用完全相同的密钥。

#1


1  

Take a look at MSDN examples for TripleDESCryptoServiceProvider and do not forget to set correct Mode, Key, IV and Padding properties of your instance:

查看TripleDESCryptoServiceProvider的MSDN示例,不要忘记设置实例的正确Mode,Key,IV和Padding属性:

var provider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
provider.Mode = CipherMode.CBC;
provider.Key = new byte[] { 0x3B, 0x38, 0x8F, 0x0E, 0xFD, 0xA7, 0x2A, 0xF1, 0x6D, 0xBA, 0x73, 0x4F, 0xE9, 0x70, 0x4A, 0xF7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
provider.IV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
provider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

You should also add -p option to your command so you can see the actual key used by OpenSSL for encryption:

您还应该在命令中添加-p选项,以便可以看到OpenSSL用于加密的实际密钥:

$ openssl enc -des-ede3-cbc -K 3B388F0EFDA72AF16DBA734FE9704AF7 -iv 0000000000000000 -in file.txt -out file.enc -nosalt -p
key=3B388F0EFDA72AF16DBA734FE9704AF70000000000000000
iv =0000000000000000

The key you are passing on the command line is too short so OpenSSL appends zeros to it and you need to use exactly the same key in your C# code.

您在命令行上传递的密钥太短,因此OpenSSL会向其添加零,您需要在C#代码中使用完全相同的密钥。