Skip to content

@bricef /AES.c
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A simple example of using AES encryption in Java and C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* MCrypt API available online:
* http://linux.die.net/man/3/mcrypt
*/
#include <mcrypt.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
int encrypt(
void* buffer,
int buffer_len, /* Because the plaintext could include null bytes*/
char* IV,
char* key,
int key_len
){
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
if( buffer_len % blocksize != 0 ){return 1;}
mcrypt_generic_init(td, key, key_len, IV);
mcrypt_generic(td, buffer, buffer_len);
mcrypt_generic_deinit (td);
mcrypt_module_close(td);
return 0;
}
int decrypt(
void* buffer,
int buffer_len,
char* IV,
char* key,
int key_len
){
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
if( buffer_len % blocksize != 0 ){return 1;}
mcrypt_generic_init(td, key, key_len, IV);
mdecrypt_generic(td, buffer, buffer_len);
mcrypt_generic_deinit (td);
mcrypt_module_close(td);
return 0;
}
void display(char* ciphertext, int len){
int v;
for (v=0; v<len; v++){
printf("%d ", ciphertext[v]);
}
printf("\n");
}
int main()
{
MCRYPT td, td2;
char * plaintext = "test text 123";
char* IV = "AAAAAAAAAAAAAAAA";
char *key = "0123456789abcdef";
int keysize = 16; /* 128 bits */
char* buffer;
int buffer_len = 16;
buffer = calloc(1, buffer_len);
strncpy(buffer, plaintext, buffer_len);
printf("==C==\n");
printf("plain: %s\n", plaintext);
encrypt(buffer, buffer_len, IV, key, keysize);
printf("cipher: "); display(buffer , buffer_len);
decrypt(buffer, buffer_len, IV, key, keysize);
printf("decrypt: %s\n", buffer);
return 0;
}
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AES {
static String IV = "AAAAAAAAAAAAAAAA";
static String plaintext = "test text 123\0\0\0"; /*Note null padding*/
static String encryptionKey = "0123456789abcdef";
public static void main(String [] args) {
try {
System.out.println("==Java==");
System.out.println("plain: " + plaintext);
byte[] cipher = encrypt(plaintext, encryptionKey);
System.out.print("cipher: ");
for (int i=0; i<cipher.length; i++)
System.out.print(new Integer(cipher[i])+" ");
System.out.println("");
String decrypted = decrypt(cipher, encryptionKey);
System.out.println("decrypt: " + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
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");
}
}
@rwst

Defining full 8-bit byte data like the key and IV as ASCII strings, hoping they get magically converted by the compiler, will lead to unexpected results. Secondly, the buffer, when decrypted, certainly won't have a 0 byte at the end, so printing it as is, even if it is printable, will get another unexpected result.

The reason I'm writing this contrary to simply patching is people with homework will find this page and learn nothing from it otherwise.

@ritwikgoud

thanx alot!!

@vandervidi

thanx! just what i need

@hhhhhhitman47

good work.... thanks, what about Aes serpant, Aes twofish method..

@doomboss

Thank you so much for sharing this! :D

@manimuttu

after getting the cipher, which is encrypted byte[]. am converting it into
cipherString = new String(cipher, "UTF-8");
and again am sending it as
decrypt(cipherString.getBytes("UTF-8"), encryptionKey);
but its giving javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes
exception please give a solution.

Note: In any cost i want cipher in String format first, then i need to pass it as getBytes();
please suggest.

@PRKarthik007

@ manimuttu..Use PKCS5Padding instead Nopadding..

@PRKarthik007

Hi,,

Am getting an error when i am Decrypting a capital letter(am not sure..)..
error..

javax.crypto.BadPaddingException: Given final block not properly padded

@krishna81m

Curious, if you have set all those UTF-8 encoding explicitly or would a -Dfile.encoding=UTF8 system argument suffice?

@Robsoares00

Hi, I am interested in that sample because i need for my thesis. But i can't dowload the correct lib. I need the mcrypt.c and mcrypt.h right? and i don't know. The files that i downloaded do not match...

Can you help me?

I really appreciate some help..

Thank's

@caoyang1024

Thank you for this. I have 2 questions
1) I got an error says: Input length not multiple of 16 bytes. Can I change the padding to 'AES/CBC/PKCS5Padding' ?

2) Should I change the IV ' static String IV = "AAAAAAAAAAAAAAAA";' to another value?

@jouini1311

Thank you for this post

@Shardss

I tried using this example but it doesn't work. I read the first comment and started doing corrections: the key and the IV seem fine to me (he's doing a .getBytes() call, where's the problem? Is this not a conversion?) and for adding the 0 byte at the end of the buffer, I altered the decryption function to:

public String decrypt(byte[] cipherText, String encryptionKey, String IV) 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")));

byte[] buff = cipher.doFinal(cipherText);
byte[] res = new byte[buff.length +1];
for(byte i=0; i<buff.length; i++)
{res[i] = buff[i];}
res[res.length-1] = 0;

return new String(res,"UTF-8");
}

It still doesn't work and returns gibberish. Can someone please help :S?

@NitinLnt

Please tell me the procedure of compilation. I tried but could not include mcrypt file
getting this error:
root@sysadmin-OptiPlex-7010:/home/sysadmin/Workspace/g# gcc -o a AES.c
AES.c:9:20: fatal error: mcrypt.h: No such file or directory
#include
^
compilation terminated.

@gurudath

Please see my blog , you can find step by step implementation of aes-cipher-encryption-decryption-algorithms https://gurudathbn.wordpress.com/2015/02/04/aes-cipher-encryption-decryption-algorithms-using-ruby-on-rails/ for clear solution for ruby on rails

@xeno6696

Please note that this implementation isn't secure... its simple and quick. It has a weakness:

https://cwe.mitre.org/data/definitions/329.html

@xeno6696

Java doesn't use "\0" for null bytes either.

static String plaintext = "test text 123\0\0\0"; /*Note null padding*/
should be
static String plaintext = "test text 123\u0000\u0000\u0000"; /*Note null padding*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.