Siva Siva - 1 month ago 6
C Question

Cryptography : AES/CTR Decryption

I have built an server and client application which is used to encrypt and decrypt on AES/CTR algorithm.The client side is built in C language and the server side is built in Java language.But,the encrypted ciphertext cant be decrypted in server and vice versa.

Here is the code :
(Client)

$ Client(C) :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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, "ctr", 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, "ctr", 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;
}


$ Server(Java) :

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;
import java.io.*;
import java.util.*;
import java.net.*;

public class AESEnc {
static String IV = "AAAAAAAAAAAAAAAA";
static String encryptionKey = "Hello World nexg";
public static void main(String [] args) {
try {
InetAddress IPAddress = InetAddress.getByName("192.168.1.7");
DatagramSocket clientSocket = new DatagramSocket(5038,IPAddress);
byte[] data=new byte[2048];
DatagramPacket packet = new DatagramPacket(data, 2048);
packet.setLength(2048);
System.out.println("Waiting for packet");
clientSocket.receive(packet);
String strMessage=new String(packet.getData(),0,packet.getLength());
System.out.println("Recieved packet");
System.out.println("==Java==");
System.out.println("plain: " + strMessage);
String decrypted = decrypt(strMessage.getBytes(),encryptionKey);

} catch (Exception e) {
e.printStackTrace();
}
}

public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding", "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/CTR/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");
}
}


These are my server and client source code,Please let me know if you find an error in it.

Thanks in Advance,

P.S : I am getting some junk characters after decrypting the messages sent from the client side.I am not getting the actual message which is encrypted.

Answer Source

Your client and server looks good,but there is an minor mistake in your UDP packet processing while fetching the data from the packet and decrypting it.Try to change it and i think your code will work. :)