Reference code written by someone else aes encryption, record it

package org.jimmy.autosearch2019.test;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class TestAes2019052801 {

    public static void main(String[] args) {
        try {
            String content = "123456";
            String secretKey = "123456";
            byte[] encryptedContentBytes = encrypt(content, secretKey);
            EncryptedContent String = parseBinaryToHexStr (encryptedContentBytes); 
            System.out.println ( "pre-encrypted text:" + Content); 
            System.out.println ( "encrypted text:" + encryptedContent);
             byte [] = encryptedContentToBinaryStr parseHexToBinaryStr (encryptedContent );
             byte [] = decryptedContentBytes the decrypt (encryptedContentToBinaryStr, secretKey); 
            String decryptedContent = new new String (decryptedContentBytes); 
            System.out.println ( "decrypted text:" + decryptedContent); 
        } the catch (Exception E) { 
            e.printStackTrace ();
        } 
    } 
    
    / ** 
     * @author raピDANGER su ra ri (Dawn) 
     * 2019 years. 5 @date afternoon of May 28 2:56:42 
     * @detail converted binary string of 16 hexadecimal string 
     * / 
    public  static  byte [] parseHexToBinaryStr ( hexStr String) throws Exception {
         byte [] bytes = null ;
         IF (hexStr.length () <. 1 ) {
             return bytes; 
        } 
        bytes = new new  byte [hexStr.length () / 2 ];
         for ( int0 = I; I <hexStr.length () / 2; I ++ Exception {) {
             Int High = the Integer.parseInt (hexStr.substring (I * 2, * 2 + I. 1), 16 );
             int Low = the Integer.parseInt (hexStr.substring (. 1 + 2 * I, I * 2 + 2) , 16 ); 
            bytes [I] = ( byte ) (* 16 + High Low); 
        } 
        return bytes; 
    } 
    
    / ** 
     * @author raピDANGER su ra ri (Dawn) 
     * @date dated 2019 years. 5 Day 28 2:54:56 PM 
     * @detail 2 binary string conversion hexadecimal string 
     * / 
    public  static string parseBinaryToHexStr ( byte [] bytes) throws 
        the StringBuffer SB = new new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xff);
            if(hex.length() == 1) {
                hex = "0" + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午3:30:33
     * @detail 解密
     */
    public static byte[] decrypt(byte[] content, String secretKey) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128, new SecureRandom(secretKey.getBytes()));
        SecretKey generatedSecretKey = keyGenerator.generateKey();
        byte[] encodedBytes = generatedSecretKey.getEncoded();
        SecretKeySpec secretKeySpec = new SecretKeySpec(encodedBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init (Cipher.DECRYPT_MODE, SecretKeySpec); 
        byte [] result =Cipher.doFinal (Content);  
         return Result; 
    } 
    
    / ** 
     * @author raピDANGER su ra ri (Dawn) 
     * 2019 years @date 2:55:25 PM May 28 
     * @ encryption detail aes 
     * / 
    public  static  byte [] the encrypt (Content String, String secretKey) throws Exception {
         // create producer AES Key of 
        the keyGenerator keyGenerator = KeyGenerator.getInstance ( "AES" );
         // use the random number to initialize the user password a
         // 128-bit key producer
         // encryption it does not matter, SecureRandom is to generate secure random number sequence, password.getBytes () is a seed,
         // as long as the same seed sequence is the same, so long as the decryption password on the line
        KeyGenerator.init (128, new new a SecureRandom (secretKey.getBytes ()));
         // the user password, a key to generate 
        a SecretKey generatedSecretKey = keyGenerator.generateKey ();
         // Returns the key basic encoding format, if this key does not support encoding returns 
        byte [] = encodedBytes generatedSecretKey.getEncoded ();
         // converted to AES private key 
        SecretKeySpec SecretKeySpec = new new SecretKeySpec (encodedBytes, "AES" );
         // create a password is 
        cipher cipher = Cipher.getInstance ( "the AES" );
         byte[] = ContentBytes content.getBytes ( "UTF-. 8" );
         // initialize the password encryption mode 
        cipher.init (Cipher.ENCRYPT_MODE, SecretKeySpec);
         // encrypted 
        byte [] Result = Cipher.doFinal (contentBytes);
         return Result; 
    } 

}

 

Guess you like

Origin www.cnblogs.com/JimmySeraph/p/10937699.html