Skip to content Skip to sidebar Skip to footer

How To Encrypt In Java And Decrypt In Android And Ios

I have a Linux server running a Java-jar file that encrypts several files. The Android and iPhone App download that file and shall decrypt it. What algorithm I have to use to do so

Solution 1:

iOS:

I use NSString+AESCrypt (https://github.com/Gurpartap/AESCrypt-ObjC)

Sample:

NSString* encrypted = [plainText AES256EncryptWithKey:@"MyEncryptionKey"];
NSString* decrypted = [encrypted AES256DecryptWithKey:@"MyEncryptionKey"];

Android (AES256Cipher - https://gist.github.com/dealforest/1949873):

Encrypt:

String base64Text="";
try {
    Stringkey="MyEncryptionKey";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] cipherData;

    //############## Request(crypt) ##############
    cipherData = AES256Cipher.encrypt(ivBytes, keyBytes, passval1.getBytes("UTF-8"));
    base64Text = Base64.encodeToString(cipherData, Base64.DEFAULT);
}
catch ( Exception e ) {
    e.printStackTrace();
}        

Decrypt:

String base64Text="";
String plainText="";
try {
    String key = "MyEncryptionKey";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] cipherData;

    //############## Response(decrypt) ##############
base64Text = User.__currentUser.getPasscode();
    cipherData = AES256Cipher.decrypt(ivBytes, keyBytes, Base64.decode(base64Text.getBytes("UTF-8"), Base64.DEFAULT));
    plainText = newString(cipherData, "UTF-8");            
}
catch ( Exception e )
{
    e.printStackTrace();
}

Solution 2:

The link below gives a nice example of encryption and decryption using Symmetric key encryption.

The symmetric key used is a custom plain text. This helps if we need to to decrypt using a IOS device. The example uses a AES 128 bit encryption. Note that it uses IV parameters. Since the encryption is 128 bit the length of the key should be 16.

On Android side the same method implementations can be used since the language is Java. In IOS CommonCryptor.h can be used for encryption decryption.

http://www.java-redefined.com/2015/06/symmetric-key-encryption-ios-java.html

Post a Comment for "How To Encrypt In Java And Decrypt In Android And Ios"