AES (ECB) encryption

Online AES encryption and decryption tool

Commonly used encryption methods in Java (JDK)

JAVA implements AES encryption

Symmetric encryption----AES and DES encryption, decryption

Why is the encryption result of the AES encryption tool written by myself inconsistent with the encryption result of the online AES encryption website?

Error: java.security.InvalidKeyException: Illegal key size or default parameters

Java Security: Illegal key size or default parameters?

The solution when Illegal key size or default parameters appears when using AES

 

Tools:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.SecureRandom;

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

import org.apache.commons.lang3.StringUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AES {

	public static void main(String[] args) throws Exception {  
        String content = "beginTime=-1&endTime=1462032000&partner_id=10&status=1,2,3,4,5,50";  
  
        String key = "d5e774de46f53ffeae8623ac47ca3334";  
        //System.out.println("Encryption key and decryption key: " + key);  
          
        String encrypt = aesEncrypt(content, key);  
        encrypt = encrypt.replaceAll("\\s|\\r|\\n|\\t", "");
        System.out.println("After encryption: " + encrypt);  
          
        String decrypt = aesDecrypt(encrypt, key);  
        System.out.println("After decryption: " + decrypt);  
        System.out.println(System.currentTimeMillis() / 1000);
    }  
      
    /**
     * Convert byte[] to string of various bases
     * @param bytes byte[]
     * @param radix can convert the range of hexadecimal, from Character.MIN_RADIX to Character.MAX_RADIX, it becomes decimal after it exceeds the range
     * @return converted string
     */  
    public static String binary(byte[] bytes, int radix){  
        return new BigInteger(1, bytes).toString(radix);// 1 here represents a positive number  
    }  
      
    /**
     * base 64 encode
     * @param bytes byte[] to be encoded
     * @return encoded base 64 code
     */  
    public static String base64Encode(byte[] bytes){  
        return new BASE64Encoder().encode(bytes);  
    }  
      
    /**
     * base 64 decode
     * @param base64Code base 64 code to be decoded
     * @return decoded byte[]
     * @throws Exception
     */  
    public static byte[] base64Decode(String base64Code) throws Exception{  
        return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);  
    }  
      
    /**
     * Get the md5 value of byte[]
     * @param bytes byte[]
     * @return md5
     * @throws Exception
     */  
    public static byte[] md5(byte[] bytes) throws Exception {  
        MessageDigest md = MessageDigest.getInstance("MD5");  
        md.update(bytes);  
          
        return md.digest();  
    }  
      
    /**
     * Get string md5 value
     * @param msg  
     * @return md5
     * @throws Exception
     */  
    public static byte[] md5(String msg) throws Exception {  
        return StringUtils.isEmpty(msg) ? null : md5(msg.getBytes());  
    }  
      
    /**
     * Combined with base64 to achieve md5 encryption
     * @param msg String to be encrypted
     * @return convert to base64 after getting md5
     * @throws Exception
     */  
    public static String md5Encrypt(String msg) throws Exception{  
        return StringUtils.isEmpty(msg) ? null : base64Encode(md5(msg));  
    }  
      
    /**
     * AES encryption
     * @param content the content to be encrypted
     * @param encryptKey encryption key
     * @return encrypted byte[]
     * @throws Exception
     */  
    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {  
        KeyGenerator kgen = KeyGenerator.getInstance("AES");  
        kgen.init(128, new SecureRandom(encryptKey.getBytes()));  
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");  
        //cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
        //cipher.init(Cipher.ENCRYPT_MODE, new IvParameterSpec(encryptKey.getBytes("UTF-8")));
        
        //KeyGenerator kgen = KeyGenerator.getInstance("AES");  
        //kgen.init(128, new SecureRandom(encryptKey.getBytes()));          
        //Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");  
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes("UTF-8"), "AES"));  
        
        return cipher.doFinal(content.getBytes("utf-8"));  
    }  
      
    /**
     * AES encryption is base 64 code
     * @param content the content to be encrypted
     * @param encryptKey encryption key
     * @return encrypted base 64 code
     * @throws Exception
     */  
    public static String aesEncrypt(String content, String encryptKey) throws Exception {  
        return base64Encode(aesEncryptToBytes(content, encryptKey));  
    }  
      
    /**
     * AES decryption
     * @param encryptBytes byte[] to be decrypted
     * @param decryptKey decryption key
     * @return decrypted String
     * @throws Exception
     */  
    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {  
        KeyGenerator kgen = KeyGenerator.getInstance("AES");  
        kgen.init(128, new SecureRandom(decryptKey.getBytes()));  
          
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");  
        //cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes("UTF-8"), "AES"));  
        
        byte[] decryptBytes = cipher.doFinal(encryptBytes);  
          
        return new String(decryptBytes);  
    }  
      
    /**
     * Decrypt base 64 code AES
     * @param encryptStr the base 64 code to be decrypted
     * @param decryptKey decryption key
     * @return decrypted string
     * @throws Exception
     */  
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {  
        return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);  
    }
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803398&siteId=291194637