Implementation of java 32 bit Encryption and Decryption Scheme for AES

  • 2021-12-11 07:31:11
  • OfStack

Directory 1, Common Encryption 32-bit Reason 2, Solution 3, AES Tool Class

1. 32-bit reasons for common encryption

Many decryption encryption on the Internet is 16 bits, and encryption with 32 bits key will report java.security.InvalidKeyException: Illegal key size or default parameters Exception error, because of the export restrictions in the United States, Sun has made corresponding restrictions through permission files (local_policy. jar, US_export_policy. jar). Therefore, there are the following 1 problems:

The key length cannot meet the demand (e.g. java. security. InvalidKeyException: Illegal key size or default parameters); Some algorithms can not be supported, such as MD4, SHA-224 and so on. API is not very convenient to use; 1 Some commonly used binary conversion auxiliary tools are not available, such as Base64 encoding conversion, 106 binary encoding conversion and other tools.

2. Solutions

Oracle provides a policy-free permission file on its official website (Unlimited Strength Jurisdiction Policy Files). We only need to deploy it in JRE environment to solve the restriction problem, paying special attention to: 两个目录都要替换

JDK8 jar packet download address
JDK7 jar packet download address

jdk8 jar package Baidu network disk address:

Link: https://pan.baidu.com/s/1wy6If0WBjRjOgRyXYD06UA
Extraction code: xcti

%JDK_Home%\jre\lib\security Directory, correspondingly overwrite local_policy. jar and US_export_policy. jar
%JRE_Home%\lib\security Directory, you also need to overwrite these two files correspondingly.

3. AES tool class


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * AES Common decryption and encryption tool classes 
 * https://github.com/ourlang
 * @author  Kobayashi 
 */
public class AesUtil {

    /**
     *  Default character encoding 
     */
    private static final String DEFAULT_CHARSET = "utf-8";

    /**
     *  Algorithm 
     */
    private static String ALGORITHM = "AES";


    /**
     *  Algorithm / Mode / Padding 
     **/
    private static final String CipherMode = "AES/ECB/PKCS5Padding";


    /**
     *  Record a log 
     **/
    private final static Logger logger = LoggerFactory.getLogger(AesUtil.class);

    private AesUtil() {
    }

    /**
     *  Decryption AES 32 Bit 
     *
     * @param sSrc       Decrypted content 
     * @param secretKey  Secret key 
     * @return  Decrypted plaintext   Data 
     */
    public static String decrypt(String sSrc, String secretKey) {

        if (secretKey == null) {
            logger.error(" The secret key to be encrypted is empty ");
            return null;
        }
        try {
            byte[] raw = secretKey.getBytes(DEFAULT_CHARSET);
            SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            //  First use base64 Decryption 
            byte[] encryptedArr = Base64.getDecoder().decode(sSrc);
            byte[] original = cipher.doFinal(encryptedArr);
            return new String(original, DEFAULT_CHARSET);
        } catch (Exception ex) {
            logger.error("AES Decryption failed ", ex);
            return null;
        }
    }


    /**
     *  Encryption 32 Bit 
     *
     * @param sSrc  Content to be encrypted 
     * @param sKey  Secret key 
     * @return  Encrypted content 
     */
    public static String encrypt(String sSrc, String sKey) {
        if (sKey == null) {
            logger.error(" The secret key to be encrypted is empty ");
            return null;
        }
        try {
            byte[] raw = sKey.getBytes(DEFAULT_CHARSET);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes(DEFAULT_CHARSET));

            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception ex) {
            logger.error("AES Encryption failed ", ex);
            return null;
        }
    }

}


Related articles: