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