java uses AES encryption and decryption AES-128-ECB encryption

Java uses AES encryption and decryption AES-128-ECB encryption, the encryption function in HIVE also uses AES-128-ECB encryption

add maven package

<dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.9</version>
</dependency>

 

 

package com.hqgf.manager;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class AESECB {
	// encrypt
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key is null");
            return null;
        }
        // Determine if the Key is 16 bits
        if (sKey.length() != 16) {
            System.out.print("Key length is not 16 bits");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"Algorithm/Mode/Complement"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return new Base64().encodeToString(encrypted);//Base64 is used here for transcoding, and it can also perform 2 encryptions.
    }

    // decrypt
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // Determine if the Key is correct
            if (sKey == null) {
                System.out.print("Key is null");
                return null;
            }
            // Determine if the Key is 16 bits
            if (sKey.length() != 16) {
                System.out.print("Key length is not 16 bits");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//Decrypt with base64 first
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        /*
         * The AES-128-ECB encryption mode is used here, and the key needs to be 16 bits.
         */
        String cKey = "1234567890123456";
        // String to be encrypted
        String cSrc = "ABC";
        System.out.println(cSrc);
        // encrypt
        String enString = AESECB.Encrypt(cSrc, cKey);
        System.out.println("The encrypted string is: " + enString);

        // decrypt
        String DeString = AESECB.Decrypt (enString, cKey);
        System.out.println("The decrypted string is: " + DeString);
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339605&siteId=291194637