Import Java.util.Random;
Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.PBEKeySpec;
Import Javax.crypto.spec.PBEParameterSpec;
public class Token {
/*
* Password-based encryption creation steps
* 1, read the password
* Convert the required encrypted string into a character array
* Save the password to the Pbekeyspec object
* 2. Generate secret key from password
* Obtain Secretkeyfactory objects by getinstance static method of Secretkeyfactory factory class;
* The GetInstance method requires a parameter--Specify the password encryption algorithm {
* 1, Pbewithmd5anddes
* 2, Pbewithhmacsha1anddesede}
* Generate a secret key through the Generatesecret () method of the Secretkeyfactory factory class
* 3, Generate random number (salt)
* Salt must be a byte array of 8 elements
* Generate random numbers using the Nextbyte method of the random class and assign a random number to a byte array with a parameter of byte
* 4, create and initialize the cipher
* Using getinstance method to obtain cipher object, parameter is password-based encryption algorithm
* Specify password-based encryption algorithms (including salt to improve the difficulty of cracking) through the Pbeparameterspec class constructor to the Cipher object
* 5, get plaintext, encrypt
* The Dofinal () method of the execution cipher is encrypted, and the encrypted result is saved in the byte array ctext
* */
Password encryption Operation method
Public byte[] Cmdencryptionoperation (String encryptionstr,string pwdstr) throws Exception
{
Read password
Converts a password into a character array
char[] pwd = Pwdstr.tochararray ();
Storing an encrypted array to a Pbekeyspec object
Pbekeyspec Pbekeyspec = new Pbekeyspec (PWD);
Generate secret key from password
Create Secretkeyfactory objects with Secretkeyfactory getinstance method, construct parameters as encryption type
Secretkeyfactory secretkeyfactory = secretkeyfactory
. getinstance ("Pbewithmd5anddes");//throws no keyword exception found
Generate a password from Generatesecret
Secretkey key = Secretkeyfactory.generatesecret (PBEKEYSPEC);
Generate random number (salt)
Create a salt that is a byte array of 8 elements
Byte[] Salt = new byte[8];
Generate random numbers by the Nextbyte method of the random class and assign a random number to a byte array with a parameter of byte
Random random = new random ();
Random.nextbytes (salt);
To create and initialize a cryptographic device
Cipher Cipher = cipher.getinstance ("Pbewithmd5anddes");
Pbeparameterspec Parameterspec = new Pbeparameterspec (salt, 1000);
Cipher.init (Cipher.encrypt_mode, Key,parameterspec);
Get plaintext, encrypt
byte[] Ptext = encryptionstr.getbytes ("UTF-8");
byte[] Ctext = cipher.dofinal (ptext);//cipher Dofinal method for encryption
return ctext;
}
}
Use the encryption method:
public static void Main (string[] args) throws Exception {
Token token = new token ();
byte[] Ctext = token.cmdencryptionoperation ("Add QQ group 499092562 AC!! "," 2016/4/5 ");
FileOutputStream OS = new FileOutputStream ("PBEEnc.dat");
Os.write (Ctext);
for (int i = 0; i < ctext.length; i++) {
System.out.print (Ctext[i]);
}
}
Android Password-based encryption quick to understand (a)