Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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# 将加密API调用转换为.NET 我在C++中有这个代码(AnsiString是char *缓冲区的包装器):_C#_.net_Cryptography_Cryptoapi - Fatal编程技术网

C# 将加密API调用转换为.NET 我在C++中有这个代码(AnsiString是char *缓冲区的包装器):

C# 将加密API调用转换为.NET 我在C++中有这个代码(AnsiString是char *缓冲区的包装器):,c#,.net,cryptography,cryptoapi,C#,.net,Cryptography,Cryptoapi,到目前为止,我已经将其转换为.NET: public static byte[] HashItB(string text) { byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(text); System.Security.Cryptography.MD5CryptoServiceProvider sp = new System.Security.Cryptography.MD5Crypto

到目前为止,我已经将其转换为.NET:

    public static byte[] HashItB(string text)
    {
        byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(text);
        System.Security.Cryptography.MD5CryptoServiceProvider sp = new System.Security.Cryptography.MD5CryptoServiceProvider();

        byte[] hashBytes = sp.ComputeHash(textBytes);
        System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(hashBytes, new byte[0]);
        hashBytes = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, hashBytes);

        // Need to export key as plain text blob here, CryptExportKey

        return (hashBytes);
    }

假设上面的代码是正确的,我的最后一步是将CryptExportKey函数转换为.NET。有人能给我指一下函数或示例吗?我不能改变C++方法,我需要我的.NET代码来匹配遗留的应用程序。或者我应该放弃.NET方法,或者创建一个C++ DLL,并调用它(不是因为DLL的另一个大小而用我的小程序来发送),还是调用所有的密码函数?< /P> < P> PaseWordDeVIEByTy.CuthTrimeVIKEY方法<强>已经返回派生密钥<强>因此,不需要导出一个与C++代码相同的键。

Pyth.Debug导出方法> <强>已经返回派生密钥<强>,因此不需要导出与C++代码中相同的键。相当于使用Win32 CryptoAPI方法散列密码,调用

CryptDeriveKey
,最后通过调用
CryptExportKey
导出密钥

您的C++代码相当于:

byte[] textBytes = System.Text.Encoding.Default.GetBytes(text); // Default is the ANSI-code-page
System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(textBytes, new byte[0]);
byte[] hashBytes = pdb.CryptDeriveKey("TripleDES", "MD5", 0, new byte[8]);

除了.NET代码从导出的密钥(PLAINTEXTKEYBLOB头)中删除一些头数据之外。对于192位3DES密钥,这是
“08 02 00 03 66 00 18 00 00”
。如果需要,您可以在.NET代码中预先添加它。

该.NET方法
PasswordDeriveBytes.CryptDeriveKey()
相当于使用Win32 CryptoAPI方法散列密码,调用
CryptDeriveKey
,最后通过调用
CryptExportKey
导出密钥

您的C++代码相当于:

byte[] textBytes = System.Text.Encoding.Default.GetBytes(text); // Default is the ANSI-code-page
System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(textBytes, new byte[0]);
byte[] hashBytes = pdb.CryptDeriveKey("TripleDES", "MD5", 0, new byte[8]);

除了.NET代码从导出的密钥(PLAINTEXTKEYBLOB头)中删除一些头数据之外。对于192位3DES密钥,这是
“08 02 00 03 66 00 18 00 00”
。如果需要,您可以在.NET代码中预先编写它。

谢谢您的帮助,如果以后有人需要阅读本文,以下是我的结论。我相信字节数组的构建可能会更好,但在我的例子中,这段代码不需要很快,所以我只使用了我易于阅读的代码:

    public static byte[] HashIt(string text)
    {
        byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(text);
        System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(textBytes, new byte[0]);
        byte[] hashBytes = pdb.CryptDeriveKey("TripleDES", "MD5", 0, new byte[8]);
        byte[] head = new byte[] { 0x08,0x02, 0x00, 0x00, 0x03, 0x66, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00 };
        byte[] hashBytesWithHead = new byte[hashBytes.Length + head.Length];
        head.CopyTo(hashBytesWithHead,0);
        hashBytes.CopyTo(hashBytesWithHead,head.Length);
        return (hashBytesWithHead);
    }

谢谢你的帮助,如果以后有人需要读这篇文章,以下是我的结论。我相信字节数组的构建可能会更好,但在我的例子中,这段代码不需要很快,所以我只使用了我易于阅读的代码:

    public static byte[] HashIt(string text)
    {
        byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(text);
        System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(textBytes, new byte[0]);
        byte[] hashBytes = pdb.CryptDeriveKey("TripleDES", "MD5", 0, new byte[8]);
        byte[] head = new byte[] { 0x08,0x02, 0x00, 0x00, 0x03, 0x66, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00 };
        byte[] hashBytesWithHead = new byte[hashBytes.Length + head.Length];
        head.CopyTo(hashBytesWithHead,0);
        hashBytes.CopyTo(hashBytesWithHead,head.Length);
        return (hashBytesWithHead);
    }

上面的C++代码有错误检查和清除代码,我确实释放了所有这些对象并检查错误。C++。上面的C++代码有错误检查和清除代码,我确实释放了所有这些对象并检查错误。注意,如果<代码>文本<代码>应该包含非ASCII字符,这将给出与C++代码不同的结果。这对您来说可能不是问题,但您至少应该意识到这一点。请注意,如果
文本
包含非ASCII字符,则这将给出与C++代码不同的结果。这对你来说可能不是问题,但你至少应该意识到这一点。