SoFunction
Updated on 2025-04-06

Quickly understand Android password encryption (I)

I won’t say much nonsense, I will just post code to you.

import ;
import ;
import ;
import ;
import ;
import ;
public class Token {
/*
 *Step creation steps based on password encryption
 * 1. Read the password
 * Convert encrypted strings into character arrays
 * Save password to PBEKeySpec object
 * 2. Generate a secret key from the password
 * Get the SecretKeyFactory object through the getInstance static method of the SecretKeyfactory factory class;
 * getInstance method requires a parameter-specify password encryption algorithm{
 * 1. PBEWithMD5AndDES
 * 2. PBEWithHmacSHA1AndDESede}
 * Generate the secret key through the generateSecret() method of the SecretKeyFactory factory class
 * 3. Generate random number (salt)
 * The salt must be an array of 8 elements
 * Generate random numbers through the nextbyte method of Random class and assign random numbers to the byte array, with the parameter being byte array
 * 4. Create and initialize the password
 * Get the password object through getInstance method, the parameters are password-based encryption algorithm
 * Specify password encryption-based algorithms to Cipher objects through the PBEParameterSpec class constructor (including salts that increase cracking difficulty)
 * 5. Obtain plain text and encrypt
 * Execute the doFinal() method of the password to encrypt, and the encryption result is saved in the byte array ctext
 * */
//Password encryption operation methodpublic byte[] cmdEncryptionOperation(String encryptionStr,String pwdStr) throws Exception
{
//Read password//Convert password into character arraychar[] pwd = ();
//Storage the encrypted array to the PBEKeySpec objectPBEKeySpec pbeKeySpec = new PBEKeySpec(pwd);
//Create a secret key from the password//Create the SecretKeyFactory object through the getinstance method of the SecretKeyFactory, and the constructor parameter is an encryption typeSecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance("PBEWithMD5AndDES");//Exception of keyword not found//Create password through generateSecretSecretKey key = (pbeKeySpec);
//Generate random number (salt)// Create a salt of a byte array with 8 elementsbyte[] salt = new byte[8];
//Create random numbers through the nextbyte method of Random class and assign random numbers to the byte array, with the parameter being byte arrayRandom random = new Random();
(salt);
//Create and initialize the passwordCipher cipher = ("PBEWithMD5AndDES");
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, 1000);
(Cipher.ENCRYPT_MODE, key,parameterSpec);
//Get plain text and encryptbyte[] ptext = ("UTF-8");
byte[] ctext = (ptext);//cipher's dofinal method for encryptionreturn ctext;
}
}

Using encryption method:

public static void main(String[] args) throws Exception {
Token token = new Token();
byte[] ctext = ("Add to QQ group 499092562 to communicate!!","2016/4/5");
FileOutputStream os = new FileOutputStream("");
(ctext);
for (int i = 0; i < ; i++) {
(ctext[i]);
}
}

The above content is a related introduction to Android password encryption, and I hope it will be helpful to everyone!