常见语言AES签名(ecb模式)

PHP

class AES
{
    public static function encrypt($data, $key) {
        $data =  openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
        return base64_encode($data);
    }
    public static function decrypt($data, $key) {
        $encrypted = base64_decode($data);
        return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
    }
}
//获取秘钥的前16位,不够16位,后面补足0
$key        =    '4%YkW!@g5LGcf9Ut';
$oriData    =    '123456';
$encrypted    =    AES::encrypt($oriData, $key);

var_dump($encrypted);

$decrypt    =    AES::decrypt('xxxxx', $key);

var_dump($decrypt);

JAVA

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class aes {

    /**
     * 加解密密钥(获取秘钥的前16位,不够16位,后面补足0)
     */
    private static final String AES_DATA_SECURITY_KEY = "4%YkW!@g5LGcf9Ut";
    /**
     * 算法/加密模式/填充方式
     */
    private static final String AES_PKCS5P = "AES/ECB/PKCS5Padding";

    public static void main(String[] args) {
        try
        {
            String encrypt  =   aesEncrypt("123456", AES_DATA_SECURITY_KEY);
            System.out.println(encrypt);

            String decrypt  =   aesDecrypt(encrypt, AES_DATA_SECURITY_KEY);
            System.out.println(decrypt);
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }

    /**
     * AES加密
     * @param str  加密字符串
     * @param key 加密秘钥
     * @return 加密后字符串
     * @throws Exception
     */
    private static String aesEncrypt(String str, String key) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance(AES_PKCS5P);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
        return new BASE64Encoder().encode(bytes);
    }

    /**
     * AES解密
     * @param str
     * @param key
     * @return
     * @throws Exception
     */
    private static String aesDecrypt(String str, String key) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance(AES_PKCS5P);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = new BASE64Decoder().decodeBuffer(str);
        bytes = cipher.doFinal(bytes);
        return new String(bytes, "utf-8");
    }
}

Go


func aesEncrypt(data string, myKey string) (string, error) {
    key            := []byte(myKey)
    origData    :=    []byte(data)
    cipher, err    := aes.NewCipher(key)

    if err != nil {
        return "", err
    }

    length         := (len(origData) + aes.BlockSize) / aes.BlockSize
    plain         := make([]byte, length*aes.BlockSize)
    copy(plain, origData)
    pad := byte(len(plain) - len(origData))
    for i := len(origData); i < len(plain); i++ {
        plain[i] = pad
    }
    encrypted := make([]byte, len(plain))
    for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
        cipher.Encrypt(encrypted[bs:be], plain[bs:be])
    }

    return base64.StdEncoding.EncodeToString(encrypted), nil
}

Python


from Crypto.Cipher import AES
import base64
from Crypto.Util.Padding import pad

def __aesEncrypt(self, text, key):
    aes         =   AES.new(key.encode('utf-8'), AES.MODE_ECB)
    pad_pkcs7   =   pad(text.encode('utf-8'), AES.block_size)
    encrypt_data =  aes.encrypt(pad_pkcs7)
    return str(base64.b64encode(encrypt_data), encoding='utf-8')

results matching ""

    No results matching ""