Skip to content

Instantly share code, notes, and snippets.

@omeryildirim01
Created March 9, 2020 09:16
Show Gist options
  • Save omeryildirim01/fe974a0b33042720d3009103230fb5b9 to your computer and use it in GitHub Desktop.
Save omeryildirim01/fe974a0b33042720d3009103230fb5b9 to your computer and use it in GitHub Desktop.
AES-128 (Compatability C# & Java)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
/**
*
* @author fguaman
*/
public abstract class AES
{
public static String doEncryptAES(String plainText, String key)
{
var plainBytes = Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(Encrypt(plainBytes, getRijndaelManaged(key)));
}
public static String doDecryptAES(String encryptedText, String key)
{
var encryptedBytes = Convert.FromBase64String(encryptedText);
return Encoding.UTF8.GetString(Decrypt(encryptedBytes, getRijndaelManaged(key)));
}
private static RijndaelManaged getRijndaelManaged(String secretKey)
{
var keyBytes = new byte[16];
var secretKeyBytes = Encoding.UTF8.GetBytes(secretKey);
Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length));
return new RijndaelManaged
{
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128,
Key = keyBytes,
IV = keyBytes
};
}
private static byte[] Encrypt(byte[] plainBytes, RijndaelManaged rijndaelManaged)
{
return rijndaelManaged.CreateEncryptor()
.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
}
private static byte[] Decrypt(byte[] encryptedData, RijndaelManaged rijndaelManaged)
{
return rijndaelManaged.CreateDecryptor()
.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
}
}
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
*
* @author fguaman
*/
public abstract class AES {
static private final String ENCODING = "UTF-8";
static private final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
static private final String AES = "AES";
public static String doEncryptedAES(String msj, String key) {
String msjEncrypted = "error_encrypted";
byte[] msjEncryptedbyte = null;
byte[] keyByte = null;
Cipher cp;
SecretKeySpec sks = null;
IvParameterSpec ips = null;
try {
msjEncryptedbyte = msj.getBytes(ENCODING);
keyByte = getKeyBytes(key);
} catch (NullPointerException | UnsupportedEncodingException e) {
System.out.println(e.getMessage());
return msjEncrypted;
}
sks = new SecretKeySpec(keyByte, AES);
ips = new IvParameterSpec(keyByte);
try {
cp = Cipher.getInstance(TRANSFORMATION);
cp.init(Cipher.ENCRYPT_MODE, sks, ips);
msjEncryptedbyte = cp.doFinal(msjEncryptedbyte);
msjEncrypted = new String(Base64.encodeBase64(msjEncryptedbyte));
return msjEncrypted;
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
System.out.println(e.getMessage());
return msjEncrypted;
}
}
public static String doDecryptedAES(String msjEncrypted, String key) {
String msjDecrypted = "error_decrypted";
byte[] msjEncryptedByte;
byte[] keyByte;
try {
msjEncryptedByte = Base64.decodeBase64(msjEncrypted.getBytes("UTF8"));
keyByte = getKeyBytes(key);
} catch (NullPointerException | UnsupportedEncodingException e) {
System.out.println(e.getMessage());
return msjDecrypted;
}
SecretKeySpec sks = new SecretKeySpec(keyByte, AES);
IvParameterSpec ips = new IvParameterSpec(keyByte);
try {
Cipher cp = Cipher.getInstance(TRANSFORMATION);
cp.init(Cipher.DECRYPT_MODE, sks, ips);
msjEncryptedByte = cp.doFinal(msjEncryptedByte);
msjDecrypted = new String(msjEncryptedByte, ENCODING);
return msjDecrypted;
} catch (UnsupportedEncodingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
System.out.println(e.getMessage());
return msjDecrypted;
}
}
private static byte[] getKeyBytes(String key) {
byte[] keyBytes = new byte[16];
try {
byte[] parameterKeyBytes = key.getBytes(ENCODING);
System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length));
} catch (UnsupportedEncodingException e) {
System.out.println("[Error][AES][getKeyBytes][0]: " + e.getMessage());
}
return keyBytes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment