Java Base64, AES, SHA1, MD5 encryption algorithm __ Code

Source: Internet
Author: User


Package com.example.decript;
Import java.io.UnsupportedEncodingException;
Import java.security.InvalidKeyException;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
Import Java.security.SecureRandom;
Import javax.crypto.BadPaddingException;
Import Javax.crypto.Cipher;
Import javax.crypto.IllegalBlockSizeException;
Import Javax.crypto.KeyGenerator;
Import javax.crypto.NoSuchPaddingException;
Import Javax.crypto.SecretKey;

Import Javax.crypto.spec.SecretKeySpec; public class Decripttest {public static string SHA1 (String decript) {try {messagedigest digest = java.security.m
			Essagedigest. getinstance ("SHA-1");
			Digest.update (Decript.getbytes ());
			byte messagedigest[] = Digest.digest ();
			Create Hex String StringBuffer hexstring = new StringBuffer (); The byte array is converted to a hexadecimal number for (int i = 0; i < messagedigest.length; i++) {String Shahex = integer.tohexstring (Messagedi
				Gest[i] & 0xFF);
			if (Shahex.length () < 2) {		Hexstring.append (0);
			} hexstring.append (Shahex);

		return hexstring.tostring ();
		catch (NoSuchAlgorithmException e) {e.printstacktrace ();
	Return ""; public static string SHA (String decript) {try {messagedigest digest = java.security.MessageDigest. Getinst
			ance ("SHA");
			Digest.update (Decript.getbytes ());
			byte messagedigest[] = Digest.digest ();
			Create Hex String StringBuffer hexstring = new StringBuffer (); The byte array is converted to a hexadecimal number for (int i = 0; i < messagedigest.length; i++) {String Shahex = integer.tohexstring (Messagedi
				Gest[i] & 0xFF);
				if (Shahex.length () < 2) {hexstring.append (0);
			} hexstring.append (Shahex);

		return hexstring.tostring ();
		catch (NoSuchAlgorithmException e) {e.printstacktrace ();
	Return ""; public static string MD5 (String input) {try {//Get MD5 Digest algorithm MessageDigest object messagedigest mdinst = Messagedi
			Gest.getinstance ("MD5"); Update summary m with the specified byteDinst.update (Input.getbytes ());
			Get ciphertext byte[] MD = Mdinst.digest ();
			Converts cipher text into a string form of 16 StringBuffer hexstring = new StringBuffer ();
				byte array to hexadecimal number for (int i = 0; i < md.length i++) {String Shahex = integer.tohexstring (Md[i] & 0xFF);
				if (Shahex.length () < 2) {hexstring.append (0);
			} hexstring.append (Shahex);
		return hexstring.tostring ();
		catch (NoSuchAlgorithmException e) {e.printstacktrace ();
	Return ""; /** * Encryption * @param content * @param password * Encryption password * @return/PU Blic static byte[] Encryptaes (string content, string password) {try {keygenerator KGen = keygenerator.getinstance ("
			AES ");
			Kgen.init (128, New SecureRandom (Password.getbytes ()));
			Secretkey Secretkey = Kgen.generatekey ();
			byte[] Encodeformat = secretkey.getencoded ();
			Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES"); Cipher Cipher = cipher.getinstance ("AES");Create cipher byte[] bytecontent = content.getbytes ("Utf-8");
			Cipher.init (Cipher.encrypt_mode, key);//Initialize byte[] result = cipher.dofinal (bytecontent); return result;
		Encryption} catch (NoSuchAlgorithmException e) {e.printstacktrace ();
		catch (Nosuchpaddingexception e) {e.printstacktrace ();
		catch (InvalidKeyException e) {e.printstacktrace ();
		catch (Unsupportedencodingexception e) {e.printstacktrace ();
		catch (Illegalblocksizeexception e) {e.printstacktrace ();
		catch (Badpaddingexception e) {e.printstacktrace ();
	return null; /** * Decrypt * @param content * @param password * Decryption key * @return/Publ IC Static byte[] Decryptaes (byte[] content, String password) {try {keygenerator KGen = keygenerator.getinstance ("AE
			S ");
			Kgen.init (128, New SecureRandom (Password.getbytes ()));
			Secretkey Secretkey = Kgen.generatekey ();
			byte[] Encodeformat = secretkey.getencoded (); Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES"); Cipher Cipher = cipher.getinstance ("AES");//Create cipher Cipher.init (Cipher.decrypt_mode, key);//Initialize byte[] result = Ciph
			Er.dofinal (content); return result;
		Encryption} catch (NoSuchAlgorithmException e) {e.printstacktrace ();
		catch (Nosuchpaddingexception e) {e.printstacktrace ();
		catch (InvalidKeyException e) {e.printstacktrace ();
		catch (Illegalblocksizeexception e) {e.printstacktrace ();
		catch (Badpaddingexception e) {e.printstacktrace ();
	return null;  /** * BASE64 Decryption * * @param key * @return * @throws Exception/public static string decryptBASE64 (String
	Key) {return "";  /** * BASE64 Encryption * * @param key * @return * @throws Exception/public static string encryptBASE64 (String
	Key) {return "";
 }
}


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.