Quickly understand Android password encryption (i) _android

Source: Internet
Author: User

Nonsense not to say more, directly to everyone paste code.

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 step * 1, read password * Convert required encrypted string to character array * Save password to Pbekeyspec Object * 2, Generate secret key by password * through Secretkeyfactory factory class G Etinstance static method to get Secretkeyfactory object; * GetInstance method requires a parameter--specify password encryption algorithm {* 1, Pbewithmd5anddes * 2, Pbewithhmacsha1anddesede} * Generate secret Key * 3 by Secretkeyfactory factory class Generatesecret () method, generate random number (salt) * Salt must be a byte array of 8 elements * Generate random numbers and assign random numbers to byte arrays using the Nextbyte method of the random class, the parameter is byte array * 4, create and initialize the cipher * The Cipher object is obtained by the GetInstance method, and the parameter is a password based encryption algorithm * The cipher object is specified by the Pbeparameterspec class constructor based on password-encrypted algorithm (including salt to improve crack difficulty) * 5, get plaintext, encrypt * Execute cipher dofinal () method for encrypting, the result of encryption is saved in byte array ctext * * */ /password encryption operation method public byte[] Cmdencryptionoperation (String encryptionstr,string pwdstr) throws Exception {//Read password//
Converts a password to a character array char[] pwd = Pwdstr.tochararray ();
Stores the encrypted array to the Pbekeyspec object Pbekeyspec pbekeyspec = new Pbekeyspec (PWD); Generate secret key by Password//via SecretkeyfactoryThe GetInstance method creates a Secretkeyfactory object, which is constructed with an encryption type secretkeyfactory secretkeyfactory = secretkeyfactory. GetInstance ("
Pbewithmd5anddes ");//Throw no keyword exception found//Generate password via Generatesecret secretkey key = Secretkeyfactory.generatesecret (PBEKEYSPEC);
Generate random number (salt)//Create a 8 element of the byte array of salt byte[] salt = new byte[8];
The random number is generated by the Nextbyte method of the Random class and the random number is assigned to the byte array, and the parameter is a byte array Random Random = new Random ();
Random.nextbytes (salt);
Create and initialize the cipher 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 to encrypt return ctext; }
}

Use encryption Method:

public static void Main (string[] args) throws Exception {
Token Token = new Token ();
byte[] Ctext = token.cmdencryptionoperation ("Add QQ group 499092562 Exchanges!!") "," 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]);
}

The above content is for the Android password encryption related introduction, hoped that has the help to everybody!

Related Article

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.