Encrypt in java and Decrypt in IOS For AES 128 bit

I have to Encrypt in java and Decrypt in IOS Objective-C using AES 128 bit

I have this Java program



import java.util.Base64;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Encoder;

import sun.misc.BASE64Decoder;

import javax.crypto.spec.IvParameterSpec;



public class EncryptionDecryptionAES {

static Cipher cipher;



public static void main(String[] args) throws Exception {

String plainText = "AES Symmetric Encryption Decryption";

String Enc = Encrypt(plainText,"3d090e3f7a72d51a

");

System.out.println("Enc :" + Enc+":");

}



public static String Decrypt(String text, String key) throws Exception {

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte[] keyBytes = new byte[16];

byte[] b = key.getBytes("UTF-8");

int len = b.length;

if (len > keyBytes.length)

len = keyBytes.length;

System.arraycopy(b, 0, keyBytes, 0, len);

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);

cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

byte[] results = new byte[text.length()];

BASE64Decoder decoder = new BASE64Decoder();

try {

results = cipher.doFinal(decoder.decodeBuffer(text));

} catch (Exception e) {

System.out.print("Erron in Decryption");

}

return new String(results, "UTF-8");

}



public static String Encrypt(String text, String key) throws Exception {

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte[] keyBytes = new byte[16];

byte[] b = key.getBytes("UTF-8");

int len = b.length;

if (len > keyBytes.length)

len = keyBytes.length;

System.arraycopy(b, 0, keyBytes, 0, len);

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);

System.out.println(keyBytes);

System.out.println(keySpec);

System.out.println(ivSpec);

cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);



byte[] results = cipher.doFinal(text.getBytes("UTF-8"));

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(results);

}

}


I have Objective-C code

- (void)viewDidLoad {

[super viewDidLoad];

NSLog(@"Hello");

NSString* str = @"JMK+Jgtaj0DBm2bSuLlamKHCdEm3MKYHaX6AhUIagpjcDhfGK9Z/11pvvgPueLmJ";

NSData* data = [str dataUsingEncoding:NSUnicodeStringEncoding];

NSLog(@"%@",[data description]);

NSString* theData = [self Decrypt: data WithKey:@"testkey"];

NSLog(@"%@", theData);

}


- (NSData *)Encrypt:(NSString *)data WithKey:(NSString *)key {

return [self AESOperation:kCCEncrypt OnData:[data dataUsingEncoding:NSUTF8StringEncoding] key:key];

}


- (NSString *)Decrypt:(NSData *)data WithKey:(NSString *)key {

return [[NSString alloc] initWithData:[self AESOperation:kCCDecrypt OnData:data key:key] encoding:NSUTF8StringEncoding];

}



- (NSData *)AESOperation:(CCOperation)operation OnData:(NSData *)data key:(NSString *)key {

char keyPtr[kCCKeySizeAES128];

bzero(keyPtr, sizeof(keyPtr));

[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];


NSUInteger dataLength = [data length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;

void *buffer = malloc(bufferSize);


size_t numBytesEncrypted = 0;

CCCryptorStatus cryptStatus = CCCrypt(operation,

kCCAlgorithmAES128,

kCCOptionPKCS7Padding,

keyPtr,

kCCBlockSizeAES128,

keyPtr,

[data bytes],

dataLength,

buffer,

bufferSize,

&numBytesEncrypted);

if (cryptStatus == kCCSuccess) {

NSLog(@"Success");

return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

}



free(buffer);

return nil;

}


- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


When I am running the code in iOS

2016-09-25 22:03:21.346 Test[15603:1297217] Hello 2016-09-25 22:03:21.346 Test[15603:1297217] 2016-09-25 22:03:21.346 Test[15603:1297217] Success 2016-09-25 22:03:21.347 Test[15603:1297217] (null)

I am excepting here the output for as "AES Symmetric Encryption Decryption" but getting null.

What am I doing wrong ?

Replies

You should take a look at the CryptoCompatibility sample code, which shows how to do common crypto operations in a way that’s compatible with standard third-party crypto toolkits, including Java.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi ShibiNs,


Did you find any thing relavant to this,Any solution?


Any help/information appriciate.


Thanks