banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

Using AES encryption in JDK6

title: Using AES Encryption in JDK 6
date: 2021-11-16 17:53:00
toc: true
category:

  • Java
    tags:
  • Java
  • JDK
  • jdk
  • JDK1.6
  • AES
  • aes
  • Encryption
  • Algorithm
  • Usage

Recently, I encountered an old project that required encrypting plain text. However, I couldn't find any relevant algorithms. Later, I found the existing encryption algorithm in the project and recorded it here.

Snipaste20211116175447.png

The code is as follows:

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Date;

public class AES {

    public AES() {
    }

    static {
        if (Security.getProvider("BC") == null) {
            Security.addProvider(new BouncyCastleProvider());
        }

    }

    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            if (sKey == null) {
                System.out.print("16-bit skey cannot be empty");
                return null;
            } else if (sKey.length() != 16) {
                System.out.print("skey length is not 16 bits");
                return null;
            } else {
                byte[] raw = sKey.getBytes("utf-8");
                SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(2, skeySpec);
                byte[] encrypted1 = (new Base64()).decode(sSrc);

                try {
                    byte[] original = cipher.doFinal(encrypted1);
                    return new String(original, "utf-8");
                } catch (Exception var8) {
                    System.out.println(var8.toString());
                    return null;
                }
            }
        } catch (Exception var9) {
            System.out.println(var9.toString());
            return null;
        }
    }

    private static Key getKey(String str) throws Exception {
        DESKeySpec dks = new DESKeySpec(str.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES", "BC");
        return keyFactory.generateSecret(dks);
    }

    private static AlgorithmParameterSpec getIv(String iv) throws Exception {
        return new IvParameterSpec(iv.getBytes("UTF-8"));
    }

    public static String encrypt(String key, String iv, String str) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", "BC");
        cipher.init(1, getKey(key), getIv(iv));
        byte[] data = cipher.doFinal(str.getBytes("UTF-8"));
        return Base64encode(data);
    }

    public static String decrypt(String key, String iv, String str) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", "BC");
        cipher.init(2, getKey(key), getIv(iv));
        byte[] sss = cipher.doFinal(Base64decode(str));
        return new String(sss, "UTF-8");
    }

    public static String aesDecrypt(String key, String iv, String str) throws Exception {
        str = str.replace("_", "+");
        byte[] encrypted1 = Base64decode(str);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
        cipher.init(2, new SecretKeySpec(key.getBytes("UTF-8"), "AES"), getIv(iv));
        byte[] original = cipher.doFinal(encrypted1);
        return new String(original, "UTF-8");
    }

    public static String aesEncrypt(String key, String iv, String str) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
        cipher.init(1, new SecretKeySpec(key.getBytes("UTF-8"), "AES"), getIv(iv));
        byte[] encrypted = cipher.doFinal(str.getBytes("UTF-8"));
        String s = Base64encode(encrypted);
        s = s.replace("+", "_");
        return s;
    }

    private static String Base64encode(byte[] data) throws Exception {
        return new String(Base64.encodeBase64(data), "UTF-8");
    }

    private static byte[] Base64decode(String str) throws Exception {
        return Base64.decodeBase64(str.getBytes("UTF-8"));
    }

    public static String getTime() {
        return String.valueOf(new Date().getTime());
    }

    public static void main(String[] args) throws Exception {
        // String enString = "9HEhlcxA4eZk0Q6c42z6ZA==";
        // String deString = enString.replace("InC9ReO0SE", "+");
        // String sdeString = Decrypt(deString, "MTRjNzOANWJjYWEz");
        // System.out.println("Decrypted string is: " + sdeString);
        // for (int i = 0; i < 20; i++) {
        Date now = new Date(1637117714679L);
        System.out.println(now.getTime());
        System.out.println(aesEncrypt("3tcac4konflddsrh", "xapsk6al0opqq4eo", String.format("%s,%s", "313015", now.getTime())));
        System.out.println(aesDecrypt("3tcac4konflddsrh", "xapsk6al0opqq4eo", "yKTNKqAcPD6cFTso4Qqbs_W7aA3RNKBu3viOx7ESvBc="));
        System.out.println("------------------------------ Separator ------------------------------ ");
        // }

        // String plain = decrypt("3tcac4konflddsrh", "xapsk6al0opqq4eo", "i3pGlVlwnTpSsfeBssi1BbWvCy+d7NL0E1R7/lZkmVc=");
        // String[] split = plain.split(",");
        // System.out.println(plain);
        // System.out.println(split[0]);
        // System.out.println(split[1]);
        // Date expireTime = new Date(Long.parseLong(split[1]));
        // System.out.println(expireTime);
        //
        // System.out.println(DateUtil.parse("2021-11-17 12:00:21", DateUtil.YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_PATTERN).getTime());
        // System.out.println(DateUtil.parse("2021-11-17 12:00:31", DateUtil.YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_PATTERN).getTime());
        //
        // System.out.println(System.currentTimeMillis() - Long.parseLong(split[1]) <= 10000);
    }
    
}

Refer to the usage example in the main method for running results:

Snipaste20211116175726.png

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.