C# 中的 CryptExportKey

发布于 2024-07-15 11:56:01 字数 322 浏览 4 评论 0原文

与此 CryptExportKey 调用等效的 C# 是什么?

CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, lpData, &nSize);

的 C# 代码进行描述,那就太好了

如果您能对使用 hKey、hPublicKey 和 SIMPLEBLOB(或它们在 C# 中的等效项)

What is the C# equivalent to this CryptExportKey call?

CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, lpData, &nSize);

and it would be nice if you can give a description on your C# code where

hKey, hPublicKey and SIMPLEBLOB (or their equivalent in C#) are being used

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮茶煮酒煮时光 2024-07-22 11:56:01

我用的就是这个?

    using System.Security.Cryptography;
    using System.Security.Cryptography.Xml;

    // retrieve from named keystore
    private void btnRetrieve_Click(object sender, RoutedEventArgs e)
    {
        string keyContainer = this.tbContainerName.Text;

        CspParameters parms = new CspParameters(1);
        parms.Flags = CspProviderFlags.UseMachineKeyStore;
        parms.KeyContainerName = keyContainer;
        parms.KeyNumber = 2;
        RSACryptoServiceProvider RsaCsp = new RSACryptoServiceProvider(parms);

        tbPubKeyBlob.Text = RsaCsp.ToXmlString(false);
    }

    // generate key pair
    private void btnCreateKeypair_Click(object sender, RoutedEventArgs e)
    {
        int keySize = 0;
        if (!System.Int32.TryParse(this.tbKeySize.Text, out keySize))
            keySize = 1024;
        byte[] key = Keys.GenerateKeyPair(keySize);

        RSACryptoServiceProvider RsaCsp = new RSACryptoServiceProvider();
        RsaCsp.ImportCspBlob(key);

        tbPubKeyBlob.Text = RsaCsp.ToXmlString(false);

    }

This is what I use?

    using System.Security.Cryptography;
    using System.Security.Cryptography.Xml;

    // retrieve from named keystore
    private void btnRetrieve_Click(object sender, RoutedEventArgs e)
    {
        string keyContainer = this.tbContainerName.Text;

        CspParameters parms = new CspParameters(1);
        parms.Flags = CspProviderFlags.UseMachineKeyStore;
        parms.KeyContainerName = keyContainer;
        parms.KeyNumber = 2;
        RSACryptoServiceProvider RsaCsp = new RSACryptoServiceProvider(parms);

        tbPubKeyBlob.Text = RsaCsp.ToXmlString(false);
    }

    // generate key pair
    private void btnCreateKeypair_Click(object sender, RoutedEventArgs e)
    {
        int keySize = 0;
        if (!System.Int32.TryParse(this.tbKeySize.Text, out keySize))
            keySize = 1024;
        byte[] key = Keys.GenerateKeyPair(keySize);

        RSACryptoServiceProvider RsaCsp = new RSACryptoServiceProvider();
        RsaCsp.ImportCspBlob(key);

        tbPubKeyBlob.Text = RsaCsp.ToXmlString(false);

    }
几度春秋 2024-07-22 11:56:01

我认为你最好的选择是手动处理它。

SIMPLEBLOB 格式是

BLOBHEADER blobheader;
ALG_ID algid;
BYTE encryptedkey[rsapubkey.bitlen/8];

BLOBHEADER 所在的位置

BYTE   bType;
BYTE   bVersion;
WORD   reserved;
ALG_ID aiKeyAlg;

,因此应该使用类似的格式(抱歉,未测试):

public byte[] CryptExportKey(SymmetricAlgorithm key, RSA publicKey){
  using(MemoryStream ms = new MemoryStream())
  using(BinaryWriter w = new BinaryWriter(w)){
    w.Write((byte) 0x01); // SIMPLEBLOB
    w.Write((byte) 0x02); // Version 2
    w.Write((byte) 0x00); // Reserved
    w.Write((byte) 0x00); // Reserved
    if(key is Rijndael){
      w.Write(0x00006611);  // ALG_ID for the encrypted key.
    }else if (key is TripleDES && key.KeySizeValue == 192){
      w.Write(0x00006603);  // ALG_ID for the encrypted key.
    }else{
      throw new NotSupportedException("Look the value up on http://msdn.microsoft.com/en-us/library/aa375549%28VS.85%29.aspx");
    }
    w.Write(0x0000a400);  // CALG_RSA_KEYX
    byte[] encryptedKey = publicKey.Encrypt(key.Key);
    byte[] reversedEncryptedKey = new byte[encryptedKey.Length];
    for(int i=0;i<encryptedKey.Length;i++){
      reversedEncryptedKey[i] = encryptedKey[encryptedKey.Length - 1 - i];
    }
    w.Write(reversedEncryptedKey); // encrypted key in LSB byte order
    w.Flush();
    return ms.ToArray();
  }
}

I think your best bet is handling it manually.

The SIMPLEBLOB format is

BLOBHEADER blobheader;
ALG_ID algid;
BYTE encryptedkey[rsapubkey.bitlen/8];

where BLOBHEADER is

BYTE   bType;
BYTE   bVersion;
WORD   reserved;
ALG_ID aiKeyAlg;

so something like this should do it (sorry, not tested):

public byte[] CryptExportKey(SymmetricAlgorithm key, RSA publicKey){
  using(MemoryStream ms = new MemoryStream())
  using(BinaryWriter w = new BinaryWriter(w)){
    w.Write((byte) 0x01); // SIMPLEBLOB
    w.Write((byte) 0x02); // Version 2
    w.Write((byte) 0x00); // Reserved
    w.Write((byte) 0x00); // Reserved
    if(key is Rijndael){
      w.Write(0x00006611);  // ALG_ID for the encrypted key.
    }else if (key is TripleDES && key.KeySizeValue == 192){
      w.Write(0x00006603);  // ALG_ID for the encrypted key.
    }else{
      throw new NotSupportedException("Look the value up on http://msdn.microsoft.com/en-us/library/aa375549%28VS.85%29.aspx");
    }
    w.Write(0x0000a400);  // CALG_RSA_KEYX
    byte[] encryptedKey = publicKey.Encrypt(key.Key);
    byte[] reversedEncryptedKey = new byte[encryptedKey.Length];
    for(int i=0;i<encryptedKey.Length;i++){
      reversedEncryptedKey[i] = encryptedKey[encryptedKey.Length - 1 - i];
    }
    w.Write(reversedEncryptedKey); // encrypted key in LSB byte order
    w.Flush();
    return ms.ToArray();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文