CryptExportKey在C# [英] CryptExportKey in C#

查看:383
本文介绍了CryptExportKey在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C#相当于此 CryptExportKey call?

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

这将是很好,如果你可以给你的C#代码的描述



hKey,hPublicKey和SIMPLEBLOB(或其在C#中的等效项)正在使用

解决方案

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



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); //保留
if(key是Rijndael){
w.Write(0x00006611); //加密密钥的ALG_ID。
} else if(key is TripleDES&&& key.KeySizeValue == 192){
w.Write(0x00006603); //加密密钥的ALG_ID。
} else {
throw new NotSupportedException(在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 reversedEncryptedKey [i] = encryptedKey [encryptedKey.Length_1-i];
}
w.Write(reversedEncryptedKey); //以LSB字节顺序加密的密钥
w.Flush();
return ms.ToArray();
}
}


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

解决方案

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();
  }
}

这篇关于CryptExportKey在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆