Android quickly understands password-based encryption (1) and android

Source: Internet
Author: User

Android quickly understands password-based encryption (1) and android


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 procedure
* 1. Read the password
* Convert the encrypted string to a character array
* Save the password to the PBEKeySpec object.
* 2. The key is generated by the password.
* Obtain the SecretKeyfactory object through the getInstance static method of the SecretKeyFactory class;
* The getInstance method requires a parameter -- specify the password encryption algorithm {
* 1. PBEWithMD5AndDES
* 2. PBEWithHmacSHA1AndDESede}
* Use the generateSecret () method of the SecretKeyFactory factory class to generate a secret key
* 3. Generate a random number (SALT)
* The salt must be a byte array of 8 elements.
* Use the nextbyte method of the Random class to generate a Random number and assign the Random number to the byte array. The parameter is a byte array.
* 4. Create and initialize a secret
* The getInstance method is used to obtain the secret object. The parameter is a password-based encryption algorithm.
* Use the PBEParameterSpec class constructor to specify password-based encryption algorithms (including salt that increases the difficulty of cracking) for the Cipher object)
* 5. Obtain and encrypt the plaintext.
* The encrypted doFinal () method is executed to encrypt the data. The encrypted results are stored in the byte array ctext.
**/
// Password encryption Operation Method
Public byte [] cmdEncryptionOperation (String encryptionStr, String pwdStr) throws Exception
{
// Read the password
// Convert the password into a character array
Char [] pwd = pwdStr. toCharArray ();
// Store the encrypted array to the PBEKeySpec object
PBEKeySpec pbeKeySpec = new PBEKeySpec (pwd );

// Generate a key using the password
// Create a SecretKeyFactory object using the getinstance method of SecretKeyFactory. The construction parameter is of the encryption type.
SecretKeyFactory secretKeyFactory = SecretKeyFactory
. GetInstance ("PBEWithMD5AndDES"); // throw a keyword error.
// GenerateSecret to generate a password
SecretKey key = secretKeyFactory. generateSecret (pbeKeySpec );

// Generate random number (SALT)
// Create the salt of the byte array with 8 Elements
Byte [] salt = new byte [8];
// Use the nextbyte method of the Random class to generate a Random number and assign the Random number to the byte array. The parameter is a byte array.
Random random = new Random ();
Random. nextBytes (salt );

// Create and initialize the secret
Cipher cipher = Cipher. getInstance ("PBEWithMD5AndDES ");
PBEParameterSpec parameterSpec = new PBEParameterSpec (salt, 1000 );
Cipher. init (Cipher. ENCRYPT_MODE, key, parameterSpec );

// Obtain the plaintext and encrypt it.
Byte [] ptext = encryptionStr. getBytes ("UTF-8 ");
Byte [] ctext = cipher. doFinal (ptext); // The dofinal method of cipher for encryption
Return ctext;
}

}

 

Encryption Method:

 

Public static void main (String [] args) throws Exception {
Token token = new Token ();
Byte [] ctext = token. cmdEncryptionOperation ("add QQ Group 499092562 !! "," 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]);
}

}

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.