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