AES Decryption


JAVA

/** * decryption * @return */ public static byte[] decrypt(byte[] data, byte[] key) { if(key.length!=16){ throw new RuntimeException("Invalid AES key length (must be 16 bytes)"); } try { SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance(CipherConfigure.AES_ALGORITHM);// 创建密码器 cipher.init(Cipher.DECRYPT_MODE, seckey); byte[] result = cipher.doFinal(data); return result; } catch (Exception e){ throw new RuntimeException("decrypt fail!", e); } }

PHP

/* * AES decryption * */ $date = base64_decode($data['data']); $screct_key = $decrypted; $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND); $encrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $screct_key, $date, MCRYPT_MODE_ECB, $iv); $encrypt_str = preg_replace('/[\x00-\x1F]/','', $encrypt_str); $encrypt_str = json_decode($encrypt_str,true);

C#

public static string Encrypt(string plainText, string aesKey) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(aesKey); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(plainText); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); }

GOLang

// AES/ECB/PKCS7 mode to decrypt--Signature decryption mode func AesECBDecrypt(data string, key string) (string, error) { newKey := []byte(key) dataByte,err := base64.StdEncoding.DecodeString(data) if err != nil { return "", err } block, err := aes.NewCipher(newKey) if err != nil { return "", err } ecb := NewECBDecryptEr(block) retData := make([]byte, len(dataByte)) ecb.CryptBlocks(retData, dataByte) retData = PKCS7UnPadding(retData) return string(retData), nil } func PKCS7UnPadding(encrypt []byte) []byte { length := len(encrypt) unPadding := int(encrypt[length-1]) return encrypt[:(length - unPadding)] } type ecbDecryptEr ecb func NewECBDecryptEr(b cipher.Block) cipher.BlockMode { return (*ecbDecryptEr)(newECB(b)) } func (x *ecbDecryptEr) BlockSize() int { return x.blockSize } func (x *ecbDecryptEr) CryptBlocks(dst, src []byte) { if len(src)%x.blockSize != 0 { panic("crypto/cipher: input not full blocks") } if len(dst) < len(src) { panic("crypto/cipher: output smaller than input") } for len(src) > 0 { x.b.Decrypt(dst, src[:x.blockSize]) src = src[x.blockSize:] dst = dst[x.blockSize:] } }

Python

aes_de.decrypt(base64.decodebytes(bytes(encryptKeyjson, encoding='utf8'))).rstrip(b'\0')