1. Encryption
1 public class encrypthepler {2 // check value 3 static string saltvalue = "XXXX"; 4 // password value 5 static string pwdvalue = "XXXX "; 6 7 /// <summary> 8 // encrypt 9 /// </Summary> 10 public static string encrypt (string input) {11 byte [] DATA = system. text. utf8encoding. utf8.getbytes (input); 12 byte [] salt = system. text. utf8encoding. utf8.getbytes (saltvalue); 13 14 // aesmanaged-management class of Advanced Encryption Standard (AES) symmetric algorithms 15 system. security. cryptography. aesmanaged AES = new system. security. cryptography. aesmanaged (); 16 // rfc2898derivebytes-implement password-based key derivation by using a pseudo-random number generator based on hmacsha1 (pbkdf2-a password-based key derivation function) 17 // secret key 18 system derived from the password and salt. security. cryptography. rfc2898derivebytes RFC = new system. security. cryptography. rfc2898derivebytes (pwdvalue, salt); 19 20 AES. blocksize = AES. legalblocksizes [0]. maxsize; 21 AES. keysize = AES. legalkeysizes [0]. maxsize; 22 AES. key = RFC. getbytes (AES. keysize/8); 23 AES. IV = RFC. getbytes (AES. blocksize/8); 24 25 // use the current key attribute and the initialization vector IV to create a symmetric encryptor object 26 system. security. cryptography. icryptotransform encrypttransform = AES. createencryptor (); 27 // encrypted output stream 28 system. io. memorystream encryptstream = new system. io. memorystream (); 29 // connect the encrypted Target stream (encryptstream) with the encrypted conversion (encrypttransform) to 30 system. security. cryptography. cryptostream encryptor = new system. security. cryptography. cryptostream31 (encryptstream, encrypttransform, system. security. cryptography. cryptostreammode. write); 32 33 // write a byte sequence to the current cryptostream (encryption completed) 34 encryptor. write (data, 0, Data. length); 35 encryptor. close (); 36 // convert the encrypted flow to a byte array and convert it to a string 37 string encryptedstring = convert. tobase64string (encryptstream. toarray (); 38 return encryptedstring; 39}
View code
2. decryption
1 /// <summary> 2 /// decrypt 3 /// </Summary> 4 public static string decrypt (string input) {5 byte [] encryptbytes = convert. frombase64string (input); 6 byte [] salt = encoding. utf8.getbytes (saltvalue); 7 system. security. cryptography. aesmanaged AES = new system. security. cryptography. aesmanaged (); 8 system. security. cryptography. rfc2898derivebytes RFC = new system. security. cryptography. rfc2898derivebytes (pwdvalue, salt); 9 10 AES. blocksize = AES. legalblocksizes [0]. maxsize; 11 AES. keysize = AES. legalkeysizes [0]. maxsize; 12 AES. key = RFC. getbytes (AES. keysize/8); 13 AES. IV = RFC. getbytes (AES. blocksize/8); 14 15 // use the current key attribute and the initialization vector IV to create a symmetric encryptor object 16 system. security. cryptography. icryptotransform decrypttransform = AES. createdecryptor (); 17 // decrypted output stream 18 system. io. memorystream decryptstream = new system. io. memorystream (); 19 20 // connect the decrypted destination stream (decryptstream) to the decrypted transform (decrypttransform) 21 system. security. cryptography. cryptostream decryptor = new system. security. cryptography. cryptostream (22 decryptstream, decrypttransform, system. security. cryptography. cryptostreammode. write); 23 // write a byte sequence to the current cryptostream (the decryption process is completed) 24 decryptor. write (encryptbytes, 0, encryptbytes. length); 25 decryptor. close (); 26 27 // convert the flow obtained after decryption to the string 28 byte [] decryptbytes = decryptstream. toarray (); 29 string decryptedstring = utf8encoding. utf8.getstring (decryptbytes, 0, decryptbytes. length); 30 return decryptedstring; 31} 32} // class end
View code