In Win8, you can use the dataprotectionprovider class in the windows. Security. cryptography. dataprotection namespace to encrypt and decrypt data.
Dataprotectionprovider has two constructors:
View code
// Abstract: // The constructor used for decryption. Public Dataprotectionprovider (); // // Abstract: // Constructor used for encryption operations. // // Parameters: // Protectiondescriptor: // Contains the protection descriptor used to determine the entity to encrypt. Public Dataprotectionprovider ( String Protectiondescriptor );
When encrypting data, use the second constructor, that is, the parameter with a parameter, which is of the string type. However, this string is not gibberish. Please try again, write a string at will, and an exception will be thrown during encryption. Therefore, this parameter should take the following values.
View code
**************************************** ************************************ Example protection descriptors: " D = S-1-5-21-4392301 and SID = S-1-5-21-3101812 " " SDDL = O: S-1-5-5-0-290724G: Syd :( A; CCDC; S-1-5-5-0-290724) (A; DC; WD) " " Local = user " " Local = Machine " " Webcredentials = mypasswordname " " Webcredentials = mypasswordname, myweb.com " **************************************** ************************************/
For local encryption, only two of them can be used, and exceptions may occur in other cases. Which two of them are available? You guess, look at their names. If they are used locally, they must carry the words "local". Let's see which values contain "local?
Yes, these two
Local = user
Local = Machine
What are their differences? Check their values. Do you understand? One is user-level encryption, and the other is? Ha, of course, at the machine level.
I guess this is the case. If you are interested, you can do experiments on your own.
For the user level, for example, I logged on to the current system with the username "dog", and then I ranProgramAPP: I added the kill secret to the app. If I want to decrypt the encrypted file and restore it to the kill content, the current computer must use the "dog" user login to complete the operation.
The machine level is easy to understand. It means that the password cannot be decrypted on other computers. Although the SDK documentation does not provide a clear description, I guess it should be like this.
Although this method is not safe, it is sufficient for general data.
Next, let's talk about how I encrypt it:
1. Save the content in the example box to the first.txt file.
Public Async StaticTask <streamwriter> getwritestream (StringFilename ){VaRStoragefile =AwaitApplicationdata. Current. localfolder. createfileasync (filename, creationcollisionoption. replaceexisting); stream isofilereadstream=AwaitStoragefile. openstreamforwriteasync ();Return NewStreamwriter (isofilereadstream );}
You can see applicationdata. Current. localfolder here is to get the root folder in the local application data storage area (this has the read and write permissions)
While package. current. installedlocation only has read permission. I am looking for createfileasync. It has two parameters. The first one is the file name to be created, the second is mainly about what will happen if the file I want to create exists. Here I choose to replace the original file. Of course, the specific problem is analyzed.
2. I made a simple text box to save the content in the text box to the text file. I have just created a text file, and then I will write the text file.
Using(Isowritestream) {isowritestream. writeline (txtname. Text );}