Java语言AES解密Demo

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AESDecryptDemo {
    
    /**
     * 十六进制字符串转字节数组
     */
    private static byte[] hexToBytes(String hex) {
        int len = hex.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return data;
    }
    
    /**
     * AES ECB 解密
     * @param hexCipherText 十六进制密文
     * @param key 密钥(16字节)
     * @return 解密后的明文
     */
    public static String decrypt(String hexCipherText, String key) {
        try {
            // 将十六进制密文转换为字节数组
            byte[] encryptedBytes = hexToBytes(hexCipherText);
            
            // 创建密钥规格
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            
            // 创建密码器,使用AES/ECB/PKCS5Padding
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            
            // 解密
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            
            // 转换为UTF-8字符串
            return new String(decryptedBytes, StandardCharsets.UTF_8);
            
        } catch (Exception e) {
            throw new RuntimeException("AES解密失败", e);
        }
    }
    
    public static void main(String[] args) {
        // 示例:解密演示
        String key = "1234567890123456"; // 16字节密钥
        String hexCipherText = "e3d6f7a8c2b4e5d6f7a8c2b4e5d6f7a8"; // 示例密文
        
        try {
            String decryptedText = decrypt(hexCipherText, key);
            System.out.println("解密结果: " + decryptedText);
        } catch (Exception e) {
            System.out.println("解密失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Go语言AES解密Demo

package main

import (
	"crypto/aes"
	"encoding/hex"
	"fmt"
)

// GoAESDecryptDemo AES ECB 解密
func GoAESDecryptDemo(hexCipherText string, key string) (string, error) {
	// 将十六进制密文转换为字节数组
	encryptedBytes, err := hex.DecodeString(hexCipherText)
	if err != nil {
		return "", fmt.Errorf("hex解码失败: %v", err)
	}

	// 创建AES密码器
	block, err := aes.NewCipher([]byte(key))
	if err != nil {
		return "", fmt.Errorf("创建AES密码器失败: %v", err)
	}

	// ECB模式解密
	decryptedBytes := make([]byte, len(encryptedBytes))
	for i := 0; i < len(encryptedBytes); i += block.BlockSize() {
		block.Decrypt(decryptedBytes[i:], encryptedBytes[i:])
	}

	// PKCS5Padding去除
	padLength := int(decryptedBytes[len(decryptedBytes)-1])
	if padLength > block.BlockSize() {
		return "", fmt.Errorf("填充长度无效")
	}
	decryptedBytes = decryptedBytes[:len(decryptedBytes)-padLength]

	return string(decryptedBytes), nil
}

func main() {
	key := "1234567890123456" // 16字节密钥
	hexCipherText := "e3d6f7a8c2b4e5d6f7a8c2b4e5d6f7a8" // 示例密文

	decryptedText, err := GoAESDecryptDemo(hexCipherText, key)
	if err != nil {
		fmt.Printf("解密失败: %v\\n", err)
		return
	}
	fmt.Printf("解密结果: %s\\n", decryptedText)
}

Php语言AES解密Demo

<?php

class PhpAESDecryptDemo {
    
    /**
     * AES ECB 解密
     * @param string $hexCipherText 十六进制密文
     * @param string $key 密钥(16字节)
     * @return string 解密后的明文
     */
    public static function decrypt($hexCipherText, $key) {
        // 将十六进制密文转换为字节数组
        $encryptedBytes = hex2bin($hexCipherText);
        if ($encryptedBytes === false) {
            throw new Exception("hex解码失败");
        }
        
        // 使用openssl解密
        $decryptedBytes = openssl_decrypt(
            $encryptedBytes, 
            'AES-128-ECB', 
            $key, 
            OPENSSL_RAW_DATA
        );
        
        if ($decryptedBytes === false) {
            throw new Exception("AES解密失败");
        }
        
        // PKCS5Padding去除(openssl自动处理)
        return $decryptedBytes;
    }
}

// 使用示例
$key = "1234567890123456"; // 16字节密钥
$hexCipherText = "e3d6f7a8c2b4e5d6f7a8c2b4e5d6f7a8"; // 示例密文

try {
    $decryptedText = PhpAESDecryptDemo::decrypt($hexCipherText, $key);
    echo "解密结果: " . $decryptedText . "\\n";
} catch (Exception $e) {
    echo "解密失败: " . $e->getMessage() . "\\n";
}

?>

Python语言AES解密Demo

import binascii
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad

class PythonAESDecryptDemo:
    
    @staticmethod
    def decrypt(hex_cipher_text: str, key: str) -> str:
        """
        AES ECB 解密
        :param hex_cipher_text: 十六进制密文
        :param key: 密钥(16字节)
        :return: 解密后的明文
        """
        try:
            # 将十六进制密文转换为字节数组
            encrypted_bytes = binascii.unhexlify(hex_cipher_text)
            
            # 创建AES密码器
            cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
            
            # 解密并去除PKCS5Padding
            decrypted_bytes = cipher.decrypt(encrypted_bytes)
            decrypted_bytes = unpad(decrypted_bytes, AES.block_size)
            
            # 转换为UTF-8字符串
            return decrypted_bytes.decode('utf-8')
            
        except Exception as e:
            raise Exception(f"AES解密失败: {e}")

# 使用示例
if __name__ == "__main__":
    key = "1234567890123456"  # 16字节密钥
    hex_cipher_text = "e3d6f7a8c2b4e5d6f7a8c2b4e5d6f7a8"  # 示例密文
    
    try:
        decrypted_text = PythonAESDecryptDemo.decrypt(hex_cipher_text, key)
        print(f"解密结果: {decrypted_text}")
    except Exception as e:
        print(f"解密失败: {e}")