Public class encrypthepler {
// Verification Value
Static string saltvalue = "XXXX ";
// Password value
Static string pwdvalue = "XXXX ";
/// <Summary>
/// Encryption
/// </Summary>
Public static string encrypt (string input ){
Byte [] DATA = system. Text. utf8encoding. utf8.getbytes (input );
Byte [] salt = system. Text. utf8encoding. utf8.getbytes (saltvalue );
// Aesmanaged-Management of Advanced Encryption Standard (AES) symmetric algorithms
System. Security. cryptography. aesmanaged AES = new system. Security. cryptography. aesmanaged ();
// Rfc2898derivebytes-use the hmacsha1-based pseudo-random number generator to implement the password-based key derivation function (pbkdf2-a password-based key derivation function)
// Derived from the password and salt
System. Security. cryptography. rfc2898derivebytes RFC = new system. Security. cryptography. rfc2898derivebytes (pwdvalue, salt );
AES. blocksize = AES. legalblocksizes [0]. maxsize;
AES. keysize = AES. legalkeysizes [0]. maxsize;
AES. Key = RFC. getbytes (AES. keysize/8 );
AES. IV = RFC. getbytes (AES. blocksize/8 );
// Use the current key attribute and the initialization vector IV to create a symmetric encryptor object
System. Security. cryptography. icryptotransform encrypttransform = AES. createencryptor ();
// Encrypted output stream
System. Io. memorystream encryptstream = new system. Io. memorystream ();
// Connect the encrypted Target stream (encryptstream) with the encrypted conversion (encrypttransform)
System. Security. cryptography. cryptostream encryptor = new system. Security. cryptography. cryptostream
(Encryptstream, encrypttransform, system. Security. cryptography. cryptostreammode. Write );
// Write a byte sequence to the current cryptostream (the encryption process is completed)
Encryptor. Write (data, 0, Data. Length );
Encryptor. Close ();
// Convert the encrypted streams into byte arrays and convert them into strings using base64 encoding.
String encryptedstring = convert. tobase64string (encryptstream. toarray ());
Return encryptedstring;
}
/// <Summary>
/// Decrypt
/// </Summary>
Public static string decrypt (string input ){
Byte [] encryptbytes = convert. frombase64string (input );
Byte [] salt = encoding. utf8.getbytes (saltvalue );
System. Security. cryptography. aesmanaged AES = new system. Security. cryptography. aesmanaged ();
System. Security. cryptography. rfc2898derivebytes RFC = new system. Security. cryptography. rfc2898derivebytes (pwdvalue, salt );
AES. blocksize = AES. legalblocksizes [0]. maxsize;
AES. keysize = AES. legalkeysizes [0]. maxsize;
AES. Key = RFC. getbytes (AES. keysize/8 );
AES. IV = RFC. getbytes (AES. blocksize/8 );
// Use the current key attribute and the initialization vector IV to create a symmetric encryptor object
System. Security. cryptography. icryptotransform decrypttransform = AES. createdecryptor ();
// Decrypted output stream
System. Io. memorystream decryptstream = new system. Io. memorystream ();
// Connect the decrypted destination stream (decryptstream) with the decrypted and converted form
System. Security. cryptography. cryptostream decryptor = new system. Security. cryptography. cryptostream (
Decryptstream, decrypttransform, system. Security. cryptography. cryptostreammode. Write );
// Write a byte sequence to the current cryptostream (the decryption process is completed)
Decryptor. Write (encryptbytes, 0, encryptbytes. Length );
Decryptor. Close ();
// Convert the flow obtained after decryption into a string
Byte [] decryptbytes = decryptstream. toarray ();
String decryptedstring = utf8encoding. utf8.getstring (decryptbytes, 0, decryptbytes. Length );
Return decryptedstring;
}
} // Class end