Windows 8 Store Apps Learning (32) encryption decryption: Asymmetric algorithm, data conversion auxiliary class

Source: Internet
Author: User
Tags decrypt asymmetric encryption

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.