什么是相当于这个CryptExportKey调用的C#?
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); // 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(); } }
这是我用的?
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); }