Windows 8 Store Apps Learning (31) Cryptographic decryption: hashing algorithm, symmetric algorithm

Source: Internet
Author: User
Tags foreach hash hmac md5 require sha1

Introduced

Re-imagine the encryption and decryption of Windows 8 Store Apps

Hash algorithm (MD5, SHA1, SHA256, SHA384, SHA512)

HMAC algorithm (MD5, SHA1, SHA256, SHA384, SHA512)

Encrypted decryption of local data

Symmetric algorithm (AES, DES, 3DES, RC2, RC4)

Example

1. Demonstrates how to use the hash algorithm (MD5, SHA1, SHA256, SHA384, SHA512)

Crypto/hash.xaml.cs

* * Demonstrates how to use the hash algorithm (MD5, SHA1, SHA256, SHA384, SHA512) */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 Hash:page {public Hash () { This.
        InitializeComponent ();  } private void Btndemo_click (object sender, RoutedEventArgs e) {string plaintext = "I Am"
    
            WEBABCD ";
            Lblmsg.text = "Original:" + plaintext;
            Lblmsg.text + = Environment.NewLine;
    
            Lblmsg.text + = Environment.NewLine;
    
            String[] Algorithmnames = {"MD5", "SHA1", "SHA256", "SHA384", "SHA512"}; foreach (Var algorithmname in algorithmnames) {//Instantiate a hash algorithm provider based on the algorithm name HashAlg Orithmprovider HashAlgorithm = Hashalgorithmprovider.openalgorithm (algorithmname); Hashalgorithm.hashlength-The length of the value after the hash, in bytes//original binary data ibuffer vector = Cryptograph
    
                Icbuffer.convertstringtobinary (plaintext, Binarystringencoding.utf8);
    
                Hash binary data Ibuffer digest = hashalgorithm.hashdata (vector);
                Lblmsg.text + + Algorithmname + ":" + cryptographicbuffer.encodetohexstring (Digest);
    
    
    
                Lblmsg.text + = Environment.NewLine;
                Create a reusable Cryptographichash object Cryptographichash Reusablehash = Hashalgorithm.createhash (); Reusablehash.append (Cryptographicbuffer.convertstringtobinary ("I", Binarystringencoding.utf8)); Appends the binary data reusablehash.append (cryptographicbuffer.convertstringtobinary ("AM") to the cryptographichash that require a hash. Binarystringencoding.utf8)); Appends the binary data reusablehash.append (cryptographicbuffer.convertstringtobinary ("WEBABC") to the cryptographichash that require a hash. D ", BinaRystringencoding.utf8));  Append the binary data that requires hashing to the Cryptographichash//Get the hashed data, and then empty the data in the Cryptographichash digest =
    
                Reusablehash.getvalueandreset ();
                Lblmsg.text + + Algorithmname + ":" + cryptographicbuffer.encodetohexstring (Digest);
                Lblmsg.text + = Environment.NewLine;
            Lblmsg.text + = Environment.NewLine; }
        }
    }
}

2. Demonstrates how to use the HMAC algorithm (HMAC_MD5, HMAC_SHA1, hmac_sha256, hmac_sha384, hmac_sha512)

Crypto/hmac.xaml.cs

* * Demonstrates how to use the HMAC algorithm (HMAC_MD5, HMAC_SHA1, hmac_sha256, hmac_sha384, hmac_sha512) * Note: HMAC is equivalent to a hash with a key, which can be understood to encrypt the information with the key
After the hash * * 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 Hmac:page {public Hmac () { This.
        InitializeComponent ();  } private void Btndemo_click (object sender, RoutedEventArgs e) {string plaintext = "I Am"
    
            WEBABCD ";
            Lblmsg.text = "Original:" + plaintext;
            Lblmsg.text + = Environment.NewLine;
    
            Lblmsg.text + = Environment.NewLine;
    
            String[] Algorithmnames = {"Hmac_md5", "Hmac_sha1", "hmac_sha256", "hmac_sha384", "hmac_sha512"};
     foreach (Var algorithmname in algorithmnames) {//Instantiate an HMAC algorithm provider based on the algorithm name           Macalgorithmprovider hmacalgorithm = Macalgorithmprovider.openalgorithm (algorithmname); Hmacalgorithm.maclength-hmac the length of the value after the unit: Bytes//Create a random key Ibuffer key for the HMAC algorithm
    
                = Cryptographicbuffer.generaterandom (hmacalgorithm.maclength);
    
                Generate Cryptographickey object Cryptographickey Hmackey = Hmacalgorithm.createkey (key) according to Key; The content specified according to the hmackey signature Ibuffer signature = cryptographicengine.sign (Hmackey,//
                Key Cryptographicbuffer.convertstringtobinary (plaintext, Binarystringencoding.utf8)//required signature for signature
    
                ); Lblmsg.text + + Algorithmname + ":" + cryptographicbuffer.encodetohexstring (signature) + "(Key:" + CRYPTOGRAPHICBUFFER.E
                Ncodetohexstring (Key) + ")";
    
    
    
                Lblmsg.text + = Environment.NewLine; Verifying signature bool Isauthenticated = Cryptographicengine.verifysignature (Hmackey,//Key used when signing CRYPTOGRAPHICBU Ffer.
                Convertstringtobinary (plaintext, Binarystringencoding.utf8),//Required signature content signature//signature value
    
                );
                Lblmsg.text + = "isauthenticated:" + isauthenticated;
                Lblmsg.text + = Environment.NewLine;
            Lblmsg.text + = Environment.NewLine; }
        }
    }
}

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.