C # simple encryption

Source: Internet
Author: User
Tags rfc
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

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.