Using the pbewithmd5anddes algorithm to encrypt and decrypt the data of __ algorithm

Source: Internet
Author: User
Tags decrypt
Package com.alex.security;
Import Java.io.ByteArrayOutputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;

Import Java.security.spec.KeySpec;
Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.PBEKeySpec;

Import Javax.crypto.spec.PBEParameterSpec; public class Pbesecretkeytest {public static void main (string[] args) throws exception{//define data String that needs to be encrypted
		= "Alex Zhuang";
		SYSTEM.OUT.PRINTLN ("The data before the encryption is:" +data);
		The data is encrypted secretencrypt.

	Decrypt the data secretdecrypt ("D:/pbencrypted.data", "D:/pbsecret.key"); public static void Secretencrypt (String data) throws exception{//using pbewithmd5anddes algorithm to get Cipher instance Cipher Cipher
		= Cipher.getinstance ("Pbewithmd5anddes");
		Initialize key secretkeyfactory keyfactory = Secretkeyfactory.getinstance ("Pbewithmd5anddes"); Keyspec Keyspec = new Pbekeyspec ("password").ToCharArray ());
		Secretkey Secretkey = Keyfactory.generatesecret (Keyspec);		
		Initialize cipher to Pbeparameterspec parameterspec = new Pbeparameterspec (new byte[]{1,2,3,4,5,6,7,8},1000);
		
		Cipher.init (Cipher.encrypt_mode, Secretkey,parameterspec);
		
		Encrypt data byte[] EncryptedData = cipher.dofinal (Data.getbytes ());
		
		The output of the encrypted content System.out.println ("Encrypted data is:" +new String (EncryptedData));
		The key is persisted and serialized fileoutputstream Keyfos = new FileOutputStream ("D:/pbsecret.key");
		ObjectOutputStream Keyoos = new ObjectOutputStream (Keyfos);
		Keyoos.writeobject (Secretkey);
		Keyoos.close ();
			
			
		Keyfos.close ();
		The encrypted data is persisted fileoutputstream Encrypteddatafos = new FileOutputStream ("D:/pbencrypted.data");
		Encrypteddatafos.write (EncryptedData);
	Encrypteddatafos.close (); public static void Secretdecrypt (String datapath,string keypath) throws exception{//Get key and encrypted data through IO stream fileinputstr
		EAM Keyfis = new FileInputStream (keypath); ObjectInputStream Keyois = new ObjectiNputstream (Keyfis);
		Using Pbewithmd5anddes algorithm to obtain Cipher instance Cipher Cipher = Cipher.getinstance ("Pbewithmd5anddes");
		Initialize key Secretkey Secretkey = (secretkey) keyois.readobject ();
		
		Keyois.close ();
		Initialize cipher to decrypt pbeparameterspec parameterspec = new Pbeparameterspec (new byte[]{1,2,3,4,5,6,7,8},1000);
		
		Cipher.init (Cipher.decrypt_mode, Secretkey,parameterspec);
		Obtain the encrypted data fileinputstream Encrypteddatafis = new FileInputStream (datapath);
		Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
		
		CopyStream (Encrypteddatafis,bos);
		
		Obtain the decrypted data byte[] Decrypteddata = cipher.dofinal (Bos.tobytearray ());
		Bos.close ();
		
		Encrypteddatafis.close ();
		
	SYSTEM.OUT.PRINTLN ("Decrypted data is:" +new String (Decrypteddata)); public static void CopyStream (FileInputStream fis, Bytearrayoutputstream Bos) throws exception{byte[] buf = new by
		TE[1024];
		int len =-1;
		while ((Len=fis.read (BUF))!=-1) {bos.write (Buf,0,len);
 }
	}

}

Run Result:

The data before encryption is: Alex Zhuang
The encrypted data is:.? -H
The decrypted data is: Alex Zhuang

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.