Introduced
Re-imagine the encryption and decryption of Windows 8 Store Apps
Asymmetric algorithm (RSA)
Signature and authentication signature (RSA)
Mutual conversion between string hex base64 binary is realized through Cryptographicbuffer
Example
1. Demonstrates how to use an asymmetric algorithm (RSA)
Crypto/asymmetric.xaml.cs
* * Demonstrates how to use the asymmetric algorithm (RSA) */using System;
Using Windows.Security.Cryptography;
Using Windows.Security.Cryptography.Core;
Using Windows.Storage.Streams;
Using Windows.UI.Xaml;
Using Windows.UI.Xaml.Controls;
Namespace Xamldemo.crypto {public sealed partial class Asymmetric:page {public asymmetric () { This.
InitializeComponent (); } private void Btndemo_click (object sender, RoutedEventArgs e) {string plaintext = "I Am"
WEBABCD ";
UINT KeySize = 2048;
Lblmsg.text = "Original:" + plaintext;
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = "KeySize:" + keysize/8;
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = Environment.NewLine;
String[] Algorithmnames = {"Rsa_pkcs1", "Rsa_oaep_sha1", "rsa_oaep_sha256", "rsa_oaep_sha384", "rsa_oaep_sha512"};
foreach (Var algorithmname in Algorithmnames) {/* * for RSA Asymmetric encryption, the length of the original text is limited, so generally RSA to encrypt the key of the symmetric algorithm *
* RSA_PKCS1 Requirements Original length <= key length-3, Unit: BYTE * OAEP requirements Original length <= key length-2 * HashBlock-2, Unit: bytes * RSA_OAEP_SHA1-When the key length is 1024, the maximum original length 1024/8-2 * 20-2 = Rsa_oaep_sha 256-When the key length is 1024, the maximum original length 1024/8-2 * (256/8)-2 = $ * rsa_oaep_sha384-Maximum original length 2048 when the key length is 2 048/8-2 * (384/8)-2 = 162 * rsa_oaep_sha512-when the key length is 2048, the maximum original length 2048/8-2 * (512/8)- 2 = 130 * * Ibuffer buffer; The original text ibuffer encrypted; Ibuffer decrypted after encryption; After decryption Ibuffer Blobpublickey; Public key Ibuffer Blobkeypair; Public key private key to Cryptographickey KeyPair; Binary Data buffer = CRYPTOGRAPHICBUFFER.CONVERTSTR for public key private key pair//Original textIngtobinary (plaintext, Binarystringencoding.utf8); Instantiates an asymmetric algorithm provider based on the algorithm name Asymmetrickeyalgorithmprovider AsymmetricAlgorithm = asymmetrickeyalgorithmprovide
R.openalgorithm (Algorithmname); try {//randomly create a public key private key to KeyPair = Asymmetricalgorithm.createkey based on key length
Pair (KeySize); catch (Exception ex) {lblmsg.text = ex.
ToString ();
Lblmsg.text + = Environment.NewLine;
Return
}//Encrypted data (via public key) encrypted = Cryptographicengine.encrypt (keyPair, buffer, NULL); After the result of the encryption Lblmsg.text + + + "Encrypted:" + Cryptographicbuffer.encodetohex String (encrypted) + "(" + encrypted.
Length + ")";
Lblmsg.text + = Environment.NewLine; Export Public key BLObpublickey = Keypair.exportpublickey ();
Export public key Private key to Blobkeypair = Keypair.export ();
Import Public key Cryptographickey PublicKey = Asymmetricalgorithm.importpublickey (Blobpublickey);
Import public key private key to Cryptographickey KeyPair2 = Asymmetricalgorithm.importkeypair (Blobkeypair);
Decrypt data (via private key) decrypted = Cryptographicengine.decrypt (KEYPAIR2, encrypted, NULL); After decrypting the result Lblmsg.text + + + "decrypted:" + cryptographicbuffer.convertbinarytostring (Binar
Ystringencoding.utf8, decrypted);
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = Environment.NewLine; }
}
}
}