Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于TripleDESCryptoServiceProvider的等效openssl_C#_Php_Encryption_Openssl_3des - Fatal编程技术网

C# 用于TripleDESCryptoServiceProvider的等效openssl

C# 用于TripleDESCryptoServiceProvider的等效openssl,c#,php,encryption,openssl,3des,C#,Php,Encryption,Openssl,3des,我有一些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 =

我有一些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);
    }
}
如果我将
x9iWzVc4FfU=
馈送到反向openssl命令中,C代码产生了非常不同的
x9iWzVc4FfU=
结果


我对这件事非常生气。c代码是不可更改的。需要是openssl,因为我正在使用php

有几件事:

  • 打字错误。在C#中,您正在加密“abcdegf”,在OpenSSL中,您正在使用“abcdefg”(注意
    f
    g
    的顺序)

  • echo
    在其输出内容中添加新行。使用
    echo-n
    获得与代码相同的结果(检查
    echo
    是否实际具有
    -n
    选项,或者在加密时使用
    printf

  • 你想要这个
    -k
    指定通过KDF函数输入的密码,您希望直接指定密钥

  • 您的C#代码使用密钥和IV字符串的ASCII字节。OpenSSL命令行将它们解释为十六进制编码字符串。您希望在命令行中使用的是键的
    4142434445463031323334353637383838339
    (组成“ABCDEF0123456789”的字节的十六进制编码)和IV的
    4142434445463031
    (ABCDEF01的十六进制编码)

  • 您的密钥长度为16字节。这意味着。在OpenSSL中,您需要使用
    des ede cbc
    明确指定这一点(注意
    3

  • 因此,结合所有这些,要复制您的C#代码,您是否需要以下内容:

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

    哦,我的上帝,谢谢你。我现在在运输途中,但我一到电脑就会试试这个,哈哈,当然,我在整理问题时最后一次尝试的是打字错误。。。我确实尝试过很多次,没有输入错误。我用php实现了这一点。我唯一需要做的修改就是算法名。感谢againDo没有在新作品中使用3DES,它已被AES取代。此外,在这个问题中,使用了两个密钥3DE,这是非常不安全的,只有112位密钥,并且仅比DES强四倍。对于现有的工作,最好尽最大努力更新到AES。我知道@zaph。谢谢,但我无法控制。在这一点上,你只需要穿上鞋钉就可以了。请展示你的基于C的OpenSSL代码。否则,这个问题只需要所有的代码或教程。是的,安全性并不是那么重要,通常似乎有理由忽略它,对吗?到2050年,我们还会有两个关键的3D游戏吗?可能仍然有同样的理由。为什么这段代码可能会被引用为不更新对方代码的原因,一个永无止境的循环!解决方案是,只需添加一个版本标签以及遗留版本和安全版本,然后双方都可以及时分别进行更新,最终实现安全加密。它只需要一个1字节的版本指示器。正是版本控制允许更新HTTP/SSL/TLS并向后兼容。@zaph您误读了我的评论吗?我说我不能控制它。
    $ echo -n abcdegf | openssl enc -e -des-ede-cbc -a -K 41424344454630313233343536373839 -iv 4142434445463031
    x9iWzVc4FfU=