Practical development tips for Windows Phone (31): Password Encryption

Source: Internet
Author: User

During actual development, we may need to remember the user information, such as the user name and password. The common function is to authenticate the user information in subsequent network requests. The password is sensitive information. We need to encrypt it before storing it in an independent bucket or configuration information.

In Windows Phone, there is a simple API that can easily implement the string interface. The corresponding demo is provided below:

ProgramThe general layout is as follows: there is an input box used to enter the string to be encrypted. Click the encryption button to encrypt the string. Click the decrypt button to decrypt the encrypted string.

We use the SDK's built-in Encryption Class:Protecteddata

The encryption button Processing Event is as follows: Convert the input string to a byte array, and then encrypt it using protecteddata and a pre-defined byte array. the encrypted data is also a byte data. We can useConvert. Tobase64string to get the corresponding string:

Private voidButton#click (ObjectSender,RoutedeventargsE ){Byte[] Input = system. Text.Encoding. Utf8.getbytes (textbox1.text );StringResult =Convert. Tobase64string (Protecteddata. Protect (input, OPT); textblock3.text = result ;}

The pre-defined byte array is as follows:

 
Byte[] Opt =New byte[] {1, 2, 4, 8, 16 };

Let's take a look at the decryption button Processing Event:

Private voidButton2_click (ObjectSender,RoutedeventargsE ){If(String. Isnullorempty (textblock3.text )){MessageBox. Show ("Please encrypt");Return;}Byte[] Output =Convert. Frombase64string (textblock3.text );Byte[] En =Protecteddata. Unprotect (output, OPT); textblock2.text = system. Text.Encoding. Utf8.getstring (EN, 0, en. Length );}

First, obtain the byte array corresponding to the encrypted string, and then use the unprotect function for decryption. Note that if the option array is provided during encryption, the same byte array is also required for decryption.

You can find this article hereArticleSource code

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.