C# RSA 与 C++ 等效的代码 加密API
DWORD nSize;
LPBYTE lpData;
HCRYPTKEY hPublicKey;
nSize = ReadFromFile(lpszUserPublicKey, NULL);
if(nSize == -1)
return FALSE;
lpData = new BYTE[nSize];
ReadFromFile(lpszUserPublicKey, lpData);
if(!CryptImportKey(hProv, lpData, nSize, NULL, 0, &hPublicKey)) {
delete lpData;
return FALSE;
}
Erase(lpData, nSize);
// Get the data size(&nSize)
if(!CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, NULL, &nSize))
return FALSE;
lpData = new BYTE[nSize];
CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, lpData, &nSize);
if(WriteToFile(lpszLicenseFile, lpData, nSize) == -1) {
delete lpData;
return FALSE;
}
delete lpData;
return CryptDestroyKey(hPublicKey);
上面的代码用C#怎么写。 我对 Crypto API 调用特别感兴趣。 注意,使用的加密方法是RSA
DWORD nSize;
LPBYTE lpData;
HCRYPTKEY hPublicKey;
nSize = ReadFromFile(lpszUserPublicKey, NULL);
if(nSize == -1)
return FALSE;
lpData = new BYTE[nSize];
ReadFromFile(lpszUserPublicKey, lpData);
if(!CryptImportKey(hProv, lpData, nSize, NULL, 0, &hPublicKey)) {
delete lpData;
return FALSE;
}
Erase(lpData, nSize);
// Get the data size(&nSize)
if(!CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, NULL, &nSize))
return FALSE;
lpData = new BYTE[nSize];
CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, lpData, &nSize);
if(WriteToFile(lpszLicenseFile, lpData, nSize) == -1) {
delete lpData;
return FALSE;
}
delete lpData;
return CryptDestroyKey(hPublicKey);
How would the above code be written in C#. I am particularily interested in the Crypto API calls. Note, the encryption method that is used is RSA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这篇 codeproject 文章似乎适合您的需求。 如文章所示,C# 有一个 RSACryptoServiceProvider System.Security.Cryptography 中的类使事情变得更容易,这样您就不必滚动整个解决方案并手动翻译所有代码。
This codeproject article seems it fits your needs. As shown in the article, C# has a RSACryptoServiceProvider Class in System.Security.Cryptography to make things a little easier so you don't have to roll an entire solution and translate all of that code manually.
如果您有兴趣,我用 C++ 和 C# 写了一篇关于 RSA 的文章。
它包含代码以及让 RSA 在两种语言中工作、在它们之间交换密钥和消息所需了解的所有内容:)。
您可以在这里找到它:
C++ 中的加密和 C# 中的解密 (和 C++)
I wrote an article about RSA in C++ and C# if you are interested.
It contains the code and all you need to know to have RSA working in both languages, exchanging keys and messages between them :).
You can find it here:
Crypt in C++ and Decrypt in C# (and C++)