各语言AES加密解密

Informations

cipher key: 0123456789abcdef0123456789abcdef
IV: 0123456789ABCDEF
padding character: fs (ASCII=34 -> FILE SEPARATOR)

Expected output

secret message: this information is confidential, for your eyes only
    encrypted > K7aToyGnmyxFF+GHzMOsArYMibOqtWn3J9DxJGN2CczFIwc12/dAty4eV5tRtvPeLEC2cSiRtrtGBHuET8buSQ==
    decrypted > this information is confidential, for your eyes only
SUCCESS
 
AES.java
 
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;

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

public class AES {
    static byte[] CIPHER_KEY = "0123456789abcdef0123456789abcdef".getBytes(StandardCharsets.UTF_8);
    static byte[] IV = "1234567890ABCDEF".getBytes(StandardCharsets.UTF_8);
    static char PADDING_CHAR = '\034';

    public static String encrypt(String text) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(CIPHER_KEY, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));
        int paddingSize = 16 - text.length() % 16;
        String padding = String.format("%0" + paddingSize + "d", 0).replace('0', PADDING_CHAR);
        String padded = text + padding;
        byte[] encrypted = cipher.doFinal(padded.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String data) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(CIPHER_KEY, "AES");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));
        byte[] encrypted = Base64.getDecoder().decode(data);
        String padded = new String(cipher.doFinal(encrypted), StandardCharsets.UTF_8);
        return padded.replaceAll(PADDING_CHAR + "+$", "");
    }

    public static void main(String[] args) {
        try {
            String message = "this information is confidential, for your eyes only";
            System.out.println("secret message: " + message);
            String encrypted_message = encrypt(message);
            System.out.println("    encrypted > " + encrypted_message);
            String decrypted_message = decrypt(encrypted_message);
            System.out.println("    decrypted > " + decrypted_message);
            if (message.equals(decrypted_message)) {
                System.out.println("SUCCESS");
            } else {
                System.out.println("FAILED");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
 
aes.php
 
<?php
define('CIPHER_KEY', "0123456789abcdef0123456789abcdef");
define('IV', "1234567890ABCDEF");
define('PADDING_CHAR', "\034");

function encrypt($text)
{
    $padded = $text . str_repeat(PADDING_CHAR, (16 - strlen($text) % 16));
    return openssl_encrypt($padded, "AES-256-CBC", CIPHER_KEY, OPENSSL_ZERO_PADDING, IV);
}

function decrypt($data)
{
    $padded = openssl_decrypt($data, "AES-256-CBC", CIPHER_KEY, OPENSSL_ZERO_PADDING, IV);
    return rtrim($padded, PADDING_CHAR);
}


$message = "this information is confidential, for your eyes only";
echo "secret message: " . $message . "\n";
$encrypted_message = encrypt($message);
echo "    encrypted > " . $encrypted_message . "\n";
$decrypted_message = decrypt($encrypted_message);
echo "    decrypted > " . $decrypted_message . "\n";
echo $message === $decrypted_message ? "SUCCESS" : "FAILED" . "\n";
?>
 
 
aes.py
 
# Dependencies: PyCrypto -> `pip install pycrypto`

from base64 import b64decode, b64encode
from Crypto.Cipher import AES

CIPHER_KEY = "0123456789abcdef0123456789abcdef"
IV = b"1234567890ABCDEF"
PADDING_CHAR = b"\034"


def encrypt(text):
    aes = AES.new(CIPHER_KEY, AES.MODE_CBC, IV)
    padded = text + PADDING_CHAR * (16 - len(text) % 16)
    encrypted = aes.encrypt(padded)
    return b64encode(encrypted)


def decrypt(data):
    aes = AES.new(CIPHER_KEY, AES.MODE_CBC, IV)
    encrypted = b64decode(data)
    padded = aes.decrypt(encrypted)
    return padded.rstrip(PADDING_CHAR)


message = "this information is confidential, for your eyes only"
print("secret message: {}".format(message))
encrypted_message = encrypt(message)
print("    encrypted > {}".format(encrypted_message))
decrypted_message = decrypt(encrypted_message)
print("    decrypted > {}".format(decrypted_message))
if message == decrypted_message:
    print("SUCCESS")
else:
    print("FAILED")

分类: 默认 标签: 发布于: 2022-06-23 12:41:11, 更新于: 2022-06-23 12:41:28