相当于 TripleDESCryptoServiceProvider 的 openssl

openssl equivalent for TripleDESCryptoServiceProvider

我有一些使用 TripleDES 进行加密和解密的 C# 代码。我已将其缩减为最小的发布示例。

using System;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Text;

class TDes
{
    static void Main() {
        string key = "ABCDEF0123456789";
        string iv = "ABCDEF01";

        TDes tdes = new TDes(key, iv);

        string dataToDecrypt = "x9iWzVc4FfU=";
        string decrypted = tdes.Decrypt(dataToDecrypt,key);
        Console.WriteLine(decrypted);

        string dataToEncrypt = "abcdegf";
        string encrypted = tdes.Encrypt(dataToEncrypt, key);
        Console.WriteLine(encrypted);
    }

    public TripleDESCryptoServiceProvider TdesProvider;

    public TDes(string Key, string IV)
    {
        TdesProvider = new TripleDESCryptoServiceProvider();
        TdesProvider.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        TdesProvider.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV);
    }

    public string Decrypt(string Source, string Key)
    {
        long lLen;
        int nRead, nReadTotal;
        byte[] buf = new byte[3];
        byte[] decData;
        byte[] encData;
        System.IO.MemoryStream sin;
        System.IO.MemoryStream sout;
        CryptoStream decStream;

        encData = System.Convert.FromBase64String(Source);
        sin = new MemoryStream(encData);
        sout = new MemoryStream();

        decStream = new CryptoStream(sin,
            TdesProvider.CreateDecryptor(),
            CryptoStreamMode.Read);

        lLen = sin.Length;
        nReadTotal = 0;
        while (nReadTotal < lLen)
        {
            nRead = decStream.Read(buf, 0, buf.Length);
            if (0 == nRead) break;

            sout.Write(buf, 0, nRead);
            nReadTotal += nRead;
        }
        decStream.Close();

        decData = sout.ToArray();

        ASCIIEncoding ascEnc = new ASCIIEncoding();
        return ascEnc.GetString(decData);
    }

    public string Encrypt(string Source, string Key)
    {
        long lLen;
        int nRead, nReadTotal;
        byte[] buf = new byte[3];
        byte[] srcData;
        byte[] encData;
        System.IO.MemoryStream sin;
        System.IO.MemoryStream sout;
        CryptoStream encStream;

        srcData = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
        sin = new MemoryStream();
        sin.Write(srcData,0,srcData.Length);
        sin.Position = 0;
        sout = new MemoryStream();

        encStream = new CryptoStream(sout,
            TdesProvider.CreateEncryptor(),
            CryptoStreamMode.Write);
        lLen = sin.Length;
        nReadTotal = 0;
        while (nReadTotal < lLen)
        {
            nRead = sin.Read(buf, 0, buf.Length);
            encStream.Write(buf, 0, nRead);
            nReadTotal += nRead;
        }
        encStream.Close();

        encData = sout.ToArray();
        return System.Convert.ToBase64String(encData);
    }
}

这是有效的(从某种意义上说,它可以用相同的密钥和 iv 解密它加密的内容)。但是,我一直未能在 openssl 中提出等效项。我最近尝试过的事情:

echo abcdefg | openssl enc -e -des-ede3-cbc -a -k ABCDEF0123456789 -iv ABCDEF01
U2FsdGVkX1+o9K0itpYTEqGfyMjN8gARTYIDB2ZHg1U=

C# 代码产生了与 x9iWzVc4FfU= 截然不同的结果 同样,如果我将 x9iWzVc4FfU= 输入到反向 openssl 命令中,它会 barfs。

我对此感到很紧张。 C# 代码是不可更改的。需要是 openssl,因为我正在使用 php。

有几件事:

  1. 打字错误。在您的 C# 中,您正在加密 "abcdegf",在您的 OpenSSL 中,您正在使用 "abcdefg"(注意 fg 的顺序)。

  2. echo 在它的输出中添加一个换行符。使用 echo -n 获得与您的代码相同的结果(检查您的 echo 是否确实具有 -n 选项,或者在加密时使用 printf)。

  3. 你想要 command line option -K rather than -k-k指定了一个password,通过KDF函数输入,你想直接指定key。

  4. 您的 C# 代码使用键和 IV 字符串的 ASCII 字节。 OpenSSL 命令行将它们解释为十六进制编码的字符串。您要在命令行上使用的是 41424344454630313233343536373839 作为密钥(构成“ABCDEF0123456789”的字节的十六进制编码)和 4142434445463031 作为 IV(“ABCDEF01”的十六进制编码)。

  5. 您的密钥长度为 16 个字节。这意味着C# code is using “2 key” 3DES。在 OpenSSL 中,您需要使用 des-ede-cbc 明确指定(注意缺少 3)。

所以结合所有这些,要复制你的 C# 代码,你需要这样的东西:

$ echo -n abcdegf | openssl enc -e -des-ede-cbc -a -K 41424344454630313233343536373839 -iv 4142434445463031
x9iWzVc4FfU=