Simple encryption and decryption

Source: Internet
Author: User

 

1. encryption of user passwords

In this case, hash is used for encryption and base64 encoding is used to store the data in the database. When verifying the user password, you only need to re-encrypt and encode the user input and compare the values in the database.

 

There are two options: sha1 and MD5

 

Byte [] original = system. Text. encoding. utf8.getbytes ("test ");

Sha1cryptoserviceprovider hashsha1 = new sha1cryptoserviceprovider ();

Byte [] keyhash = hashsha1.computehash (original );

Hashsha1.clear ();

String result = system. Text. encoding. utf8.getstring (keyhash );

Return result;

 

Byte [] original = system. Text. encoding. utf8.getbytes ("test ");

Md5cryptoserviceprovider hashmd5 = new md5cryptoserviceprovider ();

Byte [] keyhash = hashmd5.computehash (original );

Hashmd5.clear ();

String result = system. Text. encoding. utf8.getstring (keyhash );

Return result;

 

2. encryption and decryption of other strings

In some cases, you need to save some character information, not for verification, but for reading some information. Another method is required for encryption/decryption.

 

Encryption

Tripledescryptoserviceprovider des = new tripledescryptoserviceprovider ();

Des. Key = makemd5 (key); // This mode encrypts each block separately.

Des. mode = ciphermode. ECB;

Return des. createencryptor (). transformfinalblock (original, 0, original. Length );

 

Decryption

Byte [] buff = convert. frombase64string (encrypted );

Byte [] kb = system. Text. encoding. Default. getbytes (key );

Return System. Text. encoding. getstring (decrypt (buff, KB ));

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.