window8 metro 之 RSA

            //WinRT
            String str = "String To Encrypt";
            IBuffer buf = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf16BE);
            AsymmetricKeyAlgorithmProvider asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
            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);

            byte[] bytes, bytes2;
            IBuffer ib = CryptographicEngine.Decrypt(key, enc, null);
            CryptographicBuffer.CopyToByteArray(ib, out bytes);
            bytes2 = WindowsRuntimeBufferExtensions.ToArray(ib, 0, (int)ib.Length);

            string str2 = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length).Replace("\0", "");
            string str3 = System.Text.Encoding.UTF8.GetString(bytes2, 0, bytes2.Length).Replace("\0", "");

            //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);

 

 

 

 

 

 

 

 

 

 

http://stackoverflow.com/questions/11281350/encrypt-string-in-winrt-c-sharp-and-decrypt-it-in-c-sharp-net
http://stackoverflow.com/questions/11880450/rsa-encryption-in-metro-style-application

你可能感兴趣的:(window8 metro 之 RSA)