Matthias Hoste Matthias Hoste - 17 days ago 5
Java Question

C# server with java/c# clients, aes256 encryption and decryption

So I have my c# server program and a working c# client, now I want to use java for linux and mac clients.
So when the application starts it will request the current encryption/decryption key from the server, which will be used for communication encryption.
But since I'm very new to java, I dont really know how to get it working, code is very different, I have the following code for my java client:



public class CryptoClass
{
static String IV = "AAAAAAAAAAAAAAAA";

public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
return cipher.doFinal(plainText.getBytes("UTF-8"));
}

public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
return new String(cipher.doFinal(cipherText),"UTF-8");
}
}




and this is the code for the c# client

public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CFB;
AES.Padding = PaddingMode.PKCS7;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CFB;
AES.Padding = PaddingMode.PKCS7;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}


Thanks in advance

Answer

I fixed it, I changed my java code to this:

public class CryptoClass {
private static final String AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5PADDING";
private static final int KEY_SIZE = 256;
public String DecryptText(String input) throws NoSuchAlgorithmException
    {
        input = input.replace("\0", "");
        byte[] bytesToBeDecrypted = input.getBytes();
        byte[] passwordBytes = Config.ServerKey.getBytes();
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        passwordBytes = md.digest(passwordBytes);
        byte[] bytesDecrypted = null;
    try {
        bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidAlgorithmParameterException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    }
        return new String(bytesDecrypted);
    }
    public String EncryptText(String input) throws NoSuchAlgorithmException
    {
        byte[] bytesToBeEncrypted = input.getBytes();
        byte[] passwordBytes = Config.ServerKey.getBytes();
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        passwordBytes = md.digest(passwordBytes);
        byte[] bytesEncrypted = null;
    try {
        bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidAlgorithmParameterException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex);
    }
        return Base64.getEncoder().encodeToString(bytesEncrypted);
    }
    public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException
    {
        byte[] decryptedBytes = null;
        byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
        final byte[] keyData = Arrays.copyOf(passwordBytes, KEY_SIZE
                / Byte.SIZE);
        final byte[] ivBytes = Arrays.copyOf(keyData, cipher.getBlockSize());
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyData, "AES"),
                new IvParameterSpec(ivBytes));
        decryptedBytes = cipher.doFinal(bytesToBeDecrypted);
        return decryptedBytes;
    }
    public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
        byte[] encryptedBytes = null;
        byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
        final byte[] keyData = Arrays.copyOf(passwordBytes, KEY_SIZE
                / Byte.SIZE);
        final byte[] ivBytes = Arrays.copyOf(keyData, cipher.getBlockSize());
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyData, "AES"),
                new IvParameterSpec(ivBytes));
        encryptedBytes = cipher.doFinal(bytesToBeEncrypted);
        return encryptedBytes;
    }