简体   繁体   English

在winrt c#中加密字符串,然后在c#.net中解密

[英]Encrypt String in winrt c# and decrypt it in c# .net

How one would encrypt a string using asymmetric encryption on WinRT Metro in C# and decrypt this in classic Windows, let's say in a webservice? 比如说在Web服务中,如何在C#中使用WinRT Metro上的非对称加密来加密字符串,然后在经典Windows中解密呢?

I want to use RsaPkcs1 algorithm and I can see that CryptographicKey class and RSAParameters class are not even near compatible. 我想使用RsaPkcs1算法,并且可以看到CryptographicKey类和RSAParameters类甚至不兼容。

How this could be accomplished? 如何做到这一点?

I have found the solution on how to export the keys from the CryptographicKey in a format that .net 4 can successfully use. 我找到了有关如何以.net 4可以成功使用的格式从CryptographicKey导出密钥的解决方案。

CryptographicKey has 2 functions 1. Export and 2. ExportPublicKey . CryptographicKey具有2个功能:1. Export和2. ExportPublicKey I used the 1st function that exports the private key. 我使用了导出私钥的第一函数。 This function has 2 parameters 1st is the type of export and 2nd the byte[] that will fill. 该函数有2个参数,第一个是导出的类型,第二个将填充的byte []。

If CryptographicPrivateKeyBlobType.Capi1PrivateKey is used, the blob that Export will produce is compatible with the WinCAPI that RSACryptoServiceProvider of .net 4 can import. 如果使用CryptographicPrivateKeyBlobType.Capi1PrivateKey ,则Export将产生的Blob与.net 4的RSACryptoServiceProvider可以导入的WinCAPI兼容。

WinRT WinRT的

String str = "String To Encrypt";
IBuffer buf = CryptographicBuffer.ConvertStringToBinary(str,BinaryStringEncoding.Utf16BE);
String AsymmetricAlgName = Windows.Security.Cryptography.Core.AsymmetricAlgorithmNames.RsaPkcs1;
AsymmetricKeyAlgorithmProvider asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgName);
CryptographicKey key = asym.CreateKeyPair(512);
IBuffer enc = CryptographicEngine.Encrypt(key, buf, null);
byte[] encryptedbyteArr;
CryptographicBuffer.CopyToByteArray(enc, out encryptedbyteArr);
String encryptedBase64Str = Convert.ToBase64String(encryptedbyteArr);


//Export the private Key in WinCapi format

byte[] privatekeyBytes;
CryptographicBuffer.CopyToByteArray(key.Export(CryptographicPrivateKeyBlobType.Capi1PrivateKey), out privatekeyBytes);
String privatekeyBase64 = Convert.ToBase64String(privatekeyBytes);

encryptedBase64Str now contains the encrypted string in Base64. encryptedBase64Str现在在Base64中包含加密的字符串。 And lets say that encryptedBase64Str="BwIAAACkAABSU0EyAAIAAAEAAQCFrMTqMU3T14zSUM5..." 可以说, encryptedBase64Str="BwIAAACkAABSU0EyAAIAAAEAAQCFrMTqMU3T14zSUM5..."

Web Service 网络服务

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
String privateKey64 = "BwIAAACkAABSU0EyAAIAAAEAAQCFrMTqMU3T14zSUM5...";
String EncryptedString = "PbauDOjqMLD2P6WSmEw==";
byte[] EncryptedDataBlob = Convert.FromBase64String(EncryptedString);
byte[] privateKeyBlob = Convert.FromBase64String(privateKey64);
byte[] decryptedBytes;
rsa.ImportCspBlob(privateKeyBlob);
decryptedBytes = rsa.Decrypt(EncryptedDataBlob, false);
String decryptedString =System.Text.Encoding.BigEndianUnicode.GetString(decryptedBytes);


decryptedString now contains the decrypted string that WinRT had. decryptedString现在包含解密的字符串WinRT的了。

You must use public key to decript the content. 您必须使用公共密钥来描述内容。 Don't forget export public key in WinRt Version! 不要忘记WinRt版本中的导出公钥!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM