AES encrypt in java and decrypt in java, flex, python, C#.
Encrypt: java
<pre lang="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
<pre lang="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
<pre lang="actionScript">
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
<pre lang="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#
<pre lang="csharp">
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