Reputation: 137
I have a Java class that encrypts a string and then converts it to Base64, which is used in an Android Application: (this code was used from this example (note the spaces in the hyperlink): http ://stackoverflow.com/ questions/2090765/encryption-compatable-between-android-and-c)
public class encryption {
public static final String TAG = "smsfwd"
private static Cipher aesCipher;
private static SecretKey secretKey;
private static IvParameterSpec ivParameterSpec;
private static String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
private static String CIPHER_ALGORITHM = "AES";
//the secret key in HEX is 'secretkey'
private static byte[] rawSecretKey = {Ox73, Ox65, Ox63, Ox72, Ox65, Ox74, Ox6B, Ox65, Ox79};
private static String MESSAGEDIGEST_ALGORITHM = "MD5";
public encryption(String passphrase) {
byte[] passwordKey = encodeDigest(passphrase);
try {
aesCipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "No such algorithm " + CIPHER_ALGORITHM, e);
} catch (NoSuchPaddingException e) {
Log.e(TAG, "No such padding PKCS5", e);
}
secretKey = new SecretKeySpec(passwordKey, CIPHER_ALGORITHM);
ivParameterSpec = new IvParameterSpec(rawSecretKey);
}
//base 64 encryption
public String encryptAsBase64(byte[] clearData) {
byte[] encryptedData = encrypt(clearData);
return base64.encodeBytes(encryptedData);
}
public byte[] encrypt(byte[] clearData) {
try {
aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
} catch (InvalidKeyException e) {
Log.e(TAG, "Invalid key", e);
return null;
} catch (InvalidAlgorithmParameterException e) {
Log.e(TAG, "Invalid algorithm " + CIPHER_ALGORITHM, e);
return null;
}
byte[] encryptedData;
try {
encryptedData = aesCipher.doFinal(clearData);
} catch (IllegalBlockSizeException e) {
Log.e(TAG, "Illegal block size", e);
return null;
} catch (BadPaddingException e) {
Log.e(TAG, "Bad padding", e);
return null;
}
return encryptedData;
}
private byte[] encodeDigest(String text) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(MESSAGEDIGEST_ALGORITHM);
return digest.digest(text.getBytes());
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "No such algorithm " + MESSAGEDIGEST_ALGORITHM, e);
}
return null;
}
}
And the base 64 encryption comes from (note the spaces in the hyperlink) http ://iharder.sourceforge.net/current/java/base64/
What happens is that this gets passed to a C#/.net server for decryption and everything works great on the Android. The issue is now converting this to Objective-C for use on the iPhone.
I have done a lot of research and the answer that has come closest is http://dotmac.rationalmind.net/2009/02/aes-interoperability-between-net-and-iphone/
However, when I use that example (code taken straight from the downloaded zip), I'm getting a 'Padding is invalid and cannot be removed.' error server side.
My server side code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;
namespace test.Business
{
public class Crypto
{
private ICryptoTransform rijndaelDecryptor;
// Replace me with a 16-byte key, share between Java and C#
private static byte[] rawSecretKey = {Ox73, Ox65, Ox63, Ox72, Ox65, Ox74, Ox6B, Ox65, Ox79};
public Crypto(string passphrase, bool encrypt)
{
byte[] passwordKey = encodeDigest(passphrase);
RijndaelManaged rijndael = new RijndaelManaged();
if(encrypt)
rijndaelDecryptor = rijndael.CreateEncryptor(passwordKey, rawSecretKey);
else
rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey, rawSecretKey);
}
public Crypto(string passphrase)
{
byte[] passwordKey = encodeDigest(passphrase);
RijndaelManaged rijndael = new RijndaelManaged();
rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey, rawSecretKey);
}
private string Decrypt(byte[] encryptedData)
{
byte[] newClearData;
try
{
newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
}
catch
{
throw;
}
return Encoding.ASCII.GetString(newClearData);
}
internal string DecyptString(string token)
{
//UTF8Encoding utf8 = new UTF8Encoding();
return Decrypt(ASCIIEncoding.ASCII.GetBytes(token));
}
internal string DecryptFromBase64(string encryptedBase64)
{
return Decrypt(Convert.FromBase64String(encryptedBase64));
}
private byte[] encodeDigest(string text)
{
MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] data = Encoding.ASCII.GetBytes(text);
return x.ComputeHash(data);
}
//Encryption Code
public string EncryptAsBase64(string strData)
{
byte[] clearData = Encoding.ASCII.GetBytes(strData);
byte[] encryptedData = Encrypt(clearData);
return Convert.ToBase64String(encryptedData);
}
public byte[] Encrypt(byte[] clearData)
{
byte[] test1 = new byte[clearData.Length];
try
{
test1 = rijndaelDecryptor.TransformFinalBlock(clearData, 0, clearData.Length);
}
catch
{
throw;
}
return test1;
}
}
}
Any ideas? Thank you very very much in advance!
Upvotes: 2
Views: 1175
Reputation: 75058
You can get an NSData category to handle Base64 encoding/decoding here:
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
You can find a library and NSData category to do AES encryption here:
http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html
Between the two, you can replicate what you are doing, and probably with less code...
Upvotes: 1