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 ));