How to Learn WPF (Security)-C # symmetric encryption algorithms

Source: Internet
Author: User
1   Private Symmetricalgorithm mobjcryptoservice;
2 Private String Key;
3 /// // <Summary>
4 /// // Constructor of symmetric encryption
5 /// // </Summary>
6 Public Symmetricmethod (){
7 Mobjcryptoservice = New Rijndaelmanaged ();
8 Key = " Guz (% & hj7x89h $ yubi0456ftmat5 & fvhufcy76 * H % (hilj $ lhj! Y6 & (* jkp87jh7 " ;
9 }
10 ///   <Summary>  
11 /// Get key
12 ///   </Summary>
13 ///   <Returns> Key </Returns>  
14 Private Byte [] Getlegalkey ()
15 { String Stemp = key; mobjcryptoservice. generatekey ();
16 Byte [] Byttemp = mobjcryptoservice. Key; Int Keylength = byttemp. length;
17 If (Stemp. length> keylength) stemp = stemp. substring ( 0 , Keylength );
18 Else If (Stemp. Length <keylength) stemp = stemp. padright (keylength, '   ' );
19 Return Asciiencoding. ASCII. getbytes (stemp );
20 }
21 ///   <Summary>  
22 /// Obtain the initial vector IV
23 ///   </Summary>
24 ///   <Returns> Initial test vector IV </Returns>  
25 Private Byte [] Getlegaliv (){
26 String Stemp = " E4ghj * ghg7! Rnifb & 95guy86gfghub # er57hbh (U % g6hj ($ jhwk7 &! Hg4ui % $ hjk " ;
27 Mobjcryptoservice. generateiv ();
28 Byte [] Byttemp = mobjcryptoservice. IV;
29 Int Ivlength = byttemp. length;
30 If (Stemp. length> ivlength)
31 Stemp = stemp. substring ( 0 , Ivlength );
32 Else If (Stemp. Length <ivlength)
33 Stemp = stemp. padright (ivlength, '   ' );
34 Return Asciiencoding. ASCII. getbytes (stemp );
35 }
36
37 ///   <Summary>  
38 /// Encryption Method
39 ///   </Summary>
40 /// <Param name = "Source"> String to be encrypted </Param>
41 ///   <Returns> Encrypted string </Returns>  
42 Public String Encrypto ( String Source ){
43 Byte [] Bytin = utf8encoding. utf8.getbytes (source );
44 Memorystream MS = New Memorystream ();
45 Mobjcryptoservice. Key = getlegalkey ();
46 Mobjcryptoservice. IV = getlegaliv ();
47 Icryptotransform encrypto = mobjcryptoservice. createencryptor ();
48 Cryptostream cs = New Cryptostream (MS, encrypto, cryptostreammode. Write );
49 CS. Write (bytin, 0 , Bytin. Length );
50 CS. flushfinalblock ();
51 Ms. Close ();
52 Byte [] Bytout = Ms. toarray ();
53 Return Convert. tobase64string (bytout );
54 }
55
56
59 ///   <Summary>  
60 /// Decryption Method
61 /// </Summary>
62 ///   <Param name = "Source"> String to be decrypted </Param>
63 ///   <Returns> Decrypted string </Returns>  
64 Public String Decrypto ( String Source)
65 {
66 Byte [] Bytin = convert. frombase64string (source );
67 Memorystream MS = New Memorystream (bytin, 0 , Bytin. Length );
68 Mobjcryptoservice. Key = getlegalkey ();
69 Mobjcryptoservice. IV = getlegaliv ();
70 Icryptotransform encrypto = mobjcryptoservice. createdecryptor ();
71 Cryptostream cs = New Cryptostream (MS, encrypto, cryptostreammode. Read );
72 Streamreader sr = New Streamreader (CS );
73 Return Sr. readtoend ();
74 }

How to use it. If you know the basic C # syntax, it should be used. I believe you

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.