Alex Rivera | Logout

Encryption and Decryption between Android, PHP & node.js

Asked 2012-06-25T13:15:16.397
10

I'm trying to encrypt a string in Java on Android, and then decrypt it using PHP and/or node.js

I've tried http://www.androidsnippets.com/encrypt-decrypt-between-android-and-php, but only the first half the returned hex string works.

I want the encryption type of be AES with a 128 bit key (higher if possible).

The encryption key will also need to be configurable, so my issue might be related to how I padded the strings.

Java

String.format("%1$-" + n + "s", s)

PHP

$key = str_pad($key,16);

Full Java Code:

    package com.giggsey.test;


    import java.security.NoSuchAlgorithmException;

    import javax.crypto.Cipher;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;

    import android.util.Log;

    
    public class SimpleCrypto {

            private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!)
            private IvParameterSpec ivspec;
            private SecretKeySpec keyspec;
            private Cipher cipher;
            
            private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)
            
            public void doKey(String key)
            {
                    ivspec = new IvParameterSpec(iv.getBytes());

                    key = padRight(key,16);
                    
                    Log.d("hi",key);
                    
                    keyspec = new SecretKeySpec(key.getBytes(), "AES");
                    
                    try {
                            cipher = Cipher.getInstance("AES/CBC/NoPadding");
                    } catch (NoSuchAlgorithmException e) {
                            // TODO Auto-generated catch block
          
Edit
Report

1 Answer

2

Hi Please Check this link.

Here Check Last Comment Said That "This doesn’t work with UNICODE characters"

This is work on UTF-8 characters

This is what I do with my string before encryption:

myUtf8String = new String(myString.getBytes("UTF-8"));
answered 2012-06-28T04:39:04.260

Your Answer