Implementing AES encryption in Android


MSR

I found this Java AES encryption on the internet. Can I modify and use this Java code in an Android application? Actually I don't know if it will fit my android device. If it can be used, does it mean that this Java code can be modified?

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
Aes encryption
*/
public class AES
{

    private static SecretKeySpec secretKey ;
    private static byte[] key ;

    private static String decryptedString;
    private static String encryptedString;

    public static void setKey(String myKey){


        MessageDigest sha = null;
        try {
            key = myKey.getBytes("UTF-8");
            System.out.println(key.length);
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); // use only first 128 bit
            System.out.println(key.length);
            System.out.println(new String(key,"UTF-8"));
            secretKey = new SecretKeySpec(key, "AES");


        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    public static String getDecryptedString() {
        return decryptedString;
    }
    public static void setDecryptedString(String decryptedString) {
        AES.decryptedString = decryptedString;
    }
    public static String getEncryptedString() {
        return encryptedString;
    }
    public static void setEncryptedString(String encryptedString) {
        AES.encryptedString = encryptedString;
    }
    public static String encrypt(String strToEncrypt)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

            cipher.init(Cipher.ENCRYPT_MODE, secretKey);


            setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));

        }
        catch (Exception e)
        {

            System.out.println("Error while encrypting: "+e.toString());
        }
        return null;
    }
    public static String decrypt(String strToDecrypt)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");

            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))));

        }
        catch (Exception e)
        {

            System.out.println("Error while decrypting: "+e.toString());
        }
        return null;
    }
    public static void main(String args[])
    {
                final String strToEncrypt = "My text to encrypt";
                final String strPssword = "encryptor key";
                AES.setKey(strPssword);

                AES.encrypt(strToEncrypt.trim());

                System.out.println("String to Encrypt: " + strToEncrypt); 
                System.out.println("Encrypted: " + AES.getEncryptedString());

                final String strToDecrypt =  AES.getEncryptedString();
                AES.decrypt(strToDecrypt.trim());

                System.out.println("String To Decrypt : " + strToDecrypt);
                System.out.println("Decrypted : " + AES.getDecryptedString());

    }

}

Source : http://aesencryption.net/ _

Shatney

When I implement AES encryption in an android application, I have to specify the provider BouncyCastle when instantiating the cipher.

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding","BC");

You should consider using cbc mode as ecb mode is not really secure.

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

With a slight modification to your code, this should work:

import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
Aes encryption
*/
public class AES
{

    private static SecretKeySpec secretKey ;
    private static byte[] key ;
    private static byte[] iv;
    private static String decryptedString;
    private static String encryptedString;

    public static void setKey(String myKey){


        MessageDigest sha = null;
        try {
            key = myKey.getBytes("UTF-8");
            System.out.println(key.length);
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); // use only first 128 bit
            secretKey = new SecretKeySpec(key, "AES");
            iv = new byte[]{11,53,63,87,11,69,63,28,0,9,18,99,95,23,45,8};

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    public static String getDecryptedString() {
        return decryptedString;
    }
    public static void setDecryptedString(String decryptedString) {
        AES.decryptedString = decryptedString;
    }
    public static String getEncryptedString() {
        return encryptedString;
    }
    public static void setEncryptedString(String encryptedString) {
        AES.encryptedString = encryptedString;
    }
    public static String encrypt(String strToEncrypt)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey,new IvParameterSpec(iv));
            setEncryptedString(Base64.encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")),Base64.DEFAULT));

        }
        catch (Exception e)
        {

            System.out.println("Error while encrypting: "+e.toString());
        }
        return null;
    }
    public static String decrypt(String strToDecrypt)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING","BC");
            cipher.init(Cipher.DECRYPT_MODE, secretKey,new IvParameterSpec(iv));
            String decoded = new String(cipher.doFinal(Base64.decode(strToDecrypt,Base64.DEFAULT)), Charset.forName("UTF-8"));
            setDecryptedString(decoded);

        }
        catch (Exception e)
        {

            System.out.println("Error while decrypting: "+e.toString());
        }
        return null;
    }

}

Related


Implementing AES encryption in Android

MSR I found this Java AES encryption on the internet. Can I modify and use this Java code in an Android application? Actually I don't know if it will fit my android device. If it can be used, does it mean that this Java code can be modified? import java.io.Uns

AES encryption test (android)

Sergey 136 I have developed an encryption program for Android that uses a symmetric key to encrypt and decrypt data (AES algorithm). So I was asked to verify that not only does reverse engineering work, but also that the data can be decrypted using only one ke

AES encryption testing (android)

sergey136 I have developed an encryption program for android which is using a symmetric key to both encrypt and decrypt the data (AES algorithm). So I have been asked to verify that not only a reverse engineering is working correctly but also that only one key

AES encryption on Android

Buddha Gabriel I have to encrypt and decrypt text on android device. I found some solutions, but when I encrypt the text again, the result is different. Can anyone tell me why? Here is my code: public class AESDemo { private static final String password

Android AES encryption key

David R. I am developing an android application and I want to use AES-128 for encryption. I use hex for the key and text. I use this website: http://testprotect.com/appendix/AEScalc , calculate the AES encryption with the following key: "c4dcc3c6ce0acaec4327b6

Android Java AES encryption

Bright I'm currently developing an Android app that includes encrypting a string using AES. But for some reason my app doesn't decrypt correctly. I tried to change the Base64 format but couldn't solve it. The code is similar to the Android Cryptography example

Implementing AES256 encryption in iOS

Askarc Ali | Here is my java code. Now, I want to implement the same in Objective-C. Cipher encryptCipher; IvParameterSpec iv = new IvParameterSpec(key); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); encryptCipher = Cipher.getInstance("AES/CBC/PKCS5P

Implementing AES256 encryption in iOS

Askarc Ali | Here is my java code. Now, I want to implement the same in Objective-C. Cipher encryptCipher; IvParameterSpec iv = new IvParameterSpec(key); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); encryptCipher = Cipher.getInstance("AES/CBC/PKCS5P

Implementing AES256 encryption in iOS

Askarc Ali | Here is my java code. Now, I want to implement the same in Objective-C. Cipher encryptCipher; IvParameterSpec iv = new IvParameterSpec(key); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); encryptCipher = Cipher.getInstance("AES/CBC/PKCS5P

Implementing AES256 encryption in iOS

Askarc Ali | Here is my java code. Now, I want to implement the same in Objective-C. Cipher encryptCipher; IvParameterSpec iv = new IvParameterSpec(key); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); encryptCipher = Cipher.getInstance("AES/CBC/PKCS5P

Implementing AES256 encryption in iOS

Askarc Ali | Here is my java code. Now, I want to implement the same in Objective-C. Cipher encryptCipher; IvParameterSpec iv = new IvParameterSpec(key); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); encryptCipher = Cipher.getInstance("AES/CBC/PKCS5P

Android encryption/decryption using AES

h4rd4r7c0r3: Is there a good example of how to encrypt and decrypt images and other files using AES on Android ? Nacho L.: Warning: this answer contains code you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mode) I

.NET AES encryption and Android decryption

Daniel Warding I am using the following code to encrypt a GUID string using AES in .NET. // Decode the challenge bytes from base 64 byte[] challengeBytes = Base64.decode(challenge, Base64.NO_WRAP); ICryptoTransform encryptor = aes.Crea

Android encryption/decryption using AES

h4rd4r7c0r3: Is there a good example of how to encrypt and decrypt images and other files using AES on Android ? Nacho L.: Warning: this answer contains code that you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mo

Improve the speed of encryption AES Android

Flo354 I have developed an android application for encrypting files on my phone. By searching, I found this thread: How to encrypt files from SD card using AES in Android? The method works fine, but encrypting the file is very slow... in this line: byte[] d =

Improve the speed of encryption AES Android

Flo354 I have developed an android application for encrypting files on my phone. By searching, I found this thread: How to encrypt files from SD card using AES in Android? The method works fine, but encrypting the file is very slow... in this line: byte[] d =

.NET AES encryption and Android decryption

Daniel Warding I am using the following code to encrypt a GUID string using AES in .NET. // Decode the challenge bytes from base 64 byte[] challengeBytes = Base64.decode(challenge, Base64.NO_WRAP); ICryptoTransform encryptor = aes.Crea

Android encryption/decryption using AES

h4rd4r7c0r3: Is there a good example of how to encrypt and decrypt images and other files using AES on Android ? Nacho L.: Warning: this answer contains code you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mode) I

AES encryption and decryption algorithm in Android

username I am trying to encrypt and decrypt my data using AES algorithm in Android. this is my code package com.example.aesandroidsecurity; import java.security.NoSuchAlgorithmException; //import java.security.spec.AlgorithmParameterSpec; impor

AES encryption for iOS and Android (UINT not in Java)

Eric: all, I'm new to crypto, so I don't know all the info I need to share to get help; but it's good that I know more about how to ask this question, and I'll revise the question :) I have AES encryption in both iOS and Android apps delivered to the device vi

AES encryption for iOS and Android (UINT not in Java)

Eric: all, I'm new to crypto so I don't know all the info I need to share to get help; but it's good that I know more about how to ask this question and I'll revise the question :) I have AES encryption in both iOS and Android apps delivered to the device via

AES encryption/decryption from Android to server

Berhag I have a server and two clients. The server runs Java and Jersey (Rest). One client is a Java client and the other is an Android client. I want to send a message encrypted with AES. So I have the following code (on server and client): cipher = Cipher.ge

AES encryption in android and decryption in php and vice versa

Pihu I am trying to learn AES by testing my code against https://aesencryption.net . I've had a bug before and also . So I somehow manipulated the Base64 to resolve the error. I think, now in my application the text is encrypted and decrypted correctly. Howeve

AES encryption in Android and decryption in Node.js

Nelson Joseph: I am trying to encrypt in android and decrypt in nodejs server. I generated an AES 128 bit key, encrypted it using the AES algorithm, then encrypted this generated key using the RSA algorithm. Then send both to the server. But when decrypting on

AES encryption in android and decryption in php and vice versa

Pihu I am trying to learn AES by testing my code against https://aesencryption.net . I've had a bug before and also . So I somehow manipulated the Base64 to resolve the error. I think, now in my application the text is encrypted and decrypted correctly. Howeve

AES encryption in android and decryption in php and vice versa

Pihu I am trying to learn AES by testing my code against https://aesencryption.net . I've had a bug before and also . So I somehow manipulated the Base64 to resolve the error. I think, now in my application the text is encrypted and decrypted correctly. Howeve

AES encryption in android and decryption in php and vice versa

Pihu I am trying to learn AES by testing my code against https://aesencryption.net . I've had a bug before and also . So I somehow manipulated the Base64 to resolve the error. I think, now in my application the text is encrypted and decrypted correctly. Howeve

Android log encryption using AES and RSA

Akinka Patil I want to mail encrypted log files from my application. Since the log can be larger, I encrypt the data with AES and encrypt the key with RSA. Since the AES key is required to decrypt the logs, I will send the encrypted key and log in the same fil

AES encryption/decryption from Android to server

Berhag I have a server and two clients. The server runs Java and Jersey (Rest). One client is a Java client and the other is an Android client. I want to send a message encrypted with AES. So I have the following code (on server and client): cipher = Cipher.ge