Encrypt and decrypt files

Source: Internet
Author: User

 

1 The decrypt method allows you to decrypt files encrypted using the encrypt method. The decrypt method can only decrypt files encrypted using the current user account.
2
3 The decrypt method requires exclusive access to the decrypted file. If another process is using the file, this method will cause an exception. If the file is not encrypted, decrypt returns a non-zero value, which indicates that the operation is successful.
4
5 Both the encrypt method and decrypt method use the file encryption key of the encrypted service provider (CSP) installed on the computer and the process that calls the method.
6
7 The current file system must be formatted as NTFS, and the current operating system must be Microsoft Windows NT or later.
8
9
10 Using System;
11 Using System. IO;
12 Using System. Security. accesscontrol;
13
14 Namespace Filesystemexample
15 {
16 Class Fileexample
17 {
18 Public   Static   Void Main ()
19 {
20 Try
21 {
22 String Filename =   " Test. xml " ;
23
24 Console. writeline ( " Enceypt "   + Filename );
25
26 // Encrypt the file.
27 Addencryption (filename );
28
29 Console. writeline ( " Decrypt "   + Filename );
30
31 // Decrypt the file.
32 Removeencryption (filename );
33
34 Console. writeline ( " Done " );
35 }
36 Catch (Exception E)
37 {
38Console. writeline (E );
39}
40
41 Console. Readline ();
42 }
43
44
45 // Encrypt a file.
46 Public   Static   Void Addencryption ( String Filename)
47 {
48
49File. Encrypt (filename );
50
51}
52
53 // Decrypt a file.
54 Public   Static   Void Removeencryption ( String Filename)
55 {
56File. decrypt (filename );
57}
58 }
59 }

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.