Let’s decrypt

AES encrypt in java and decrypt in java, flex, python, C#.

Encrypt: java

public static void encrypt(InputStream is, OutputStream out, String secret) throws Exception {
	SecretKey secretKey = new SecretKeySpec(Hex.decodeHex(secret.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        out.write(cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV());
        CipherOutputStream cipherOutputStream = new CipherOutputStream(out, cipher);
        int bufLength = KEY_LENGTH/8 * 100;
        byte buf[] = new byte[bufLength];
        int bytesRead = 0;
	while((bytesRead = is.read(buf)) != -1 ) {
		cipherOutputStream.write(buf, 0, bytesRead);
	}
	cipherOutputStream.close();
}

Decrypt: java

public static void decrypt(InputStream is, OutputStream os, String secret) throws Exception{
	SecretKey secretKey = new SecretKeySpec(Hex.decodeHex(secret.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        int ivLenght = KEY_LENGTH/8; //in bytes
        byte iv[] = new byte[ivLenght];
        is.read(iv, 0, ivLenght);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
        CipherOutputStream cipherOutputStream = new CipherOutputStream(os, cipher);
 
        int bufLength = KEY_LENGTH/8 * 100; //this should be 1600 bytes for a 128 bit key
        byte buf[] = new byte[bufLength];
        int bytesRead = 0;
	while((bytesRead = is.read(buf)) != -1 ) {
		cipherOutputStream.write(buf, 0, bytesRead);
	}
	cipherOutputStream.close();
}

Decrypt: flex

var data:ByteArray = body as ByteArray;
var kdata:ByteArray = Hex.toArray(Hex.fromString(key));
var pad:IPad = new PKCS5();
var mode:ICipher = Crypto.getCipher("simple-aes-cbc", kdata, pad);
var blockSize:int = mode.getBlockSize();
pad.setBlockSize(blockSize);
mode.decrypt(data);

Decrypt: python

from Crypto.Cipher import AES
def decrypt(data, secret):
	key = bytearray.fromhex(secret)
	key = str(key)
	iv = data.read(128/8)
	aes = AES.new(key, AES.MODE_CBC, iv)
	return aes.decrypt(data.read())

Decrypt: C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
 
namespace Project1
{
    class AES
    {
        public static byte[] HexStringToBytes(string hex)
        {
            byte[] data = new byte[hex.Length / 2];
            int j = 0;
            for (int i = 0; i < hex.Length; i += 2)
            {
                data[j] = Convert.ToByte(hex.Substring(i, 2), 16);
                ++j;
            }
            return data;
        }
 
        static void Main()
        {
            byte[] iv = new byte[16];
            byte[] data;
            String keyString = "...";
            byte[] key = HexStringToBytes(keyString);
            string plaintext = null;
            int numBytes;
            string fileName = "e:\\xdisk\\in.e.dat";
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                fs.Read(iv, 0, 16);
                numBytes = (int)new FileInfo(fileName).Length - 16;
                data = new byte[numBytes];
                fs.Read(data, 0, numBytes);
                using (MemoryStream msDecrypt = new MemoryStream(data))
                {
                    // Create a RijndaelManaged object
                    // with the specified key and IV.
                    RijndaelManaged aesAlg = null;
                    aesAlg = new RijndaelManaged();
                    aesAlg.Key = key;
                    aesAlg.IV = iv;
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
 
                            plaintext = srDecrypt.ReadToEnd();
                            Console.WriteLine(plaintext);
                    }
                }
            }
        }
    }
}

Leave a Reply

*