kruton / android-keyczar-demo

Demo application using Keyczar on Android
28 stars 2 forks source link

IllegalArgumentException #3

Open EdmondChuiHW opened 11 years ago

EdmondChuiHW commented 11 years ago

Not sure if it is Android specific, but testing on both 2.3.6 and 4.1.1 devices causes this Exception.

E/AndroidRuntime(10087): java.lang.IllegalArgumentException: Bad limit (capacity 57): 73 E/AndroidRuntime(10087): at java.nio.Buffer.limitImpl(Buffer.java:312) E/AndroidRuntime(10087): at java.nio.Buffer.limit(Buffer.java:303) E/AndroidRuntime(10087): at org.keyczar.Encrypter.encrypt(Encrypter.java:166) E/AndroidRuntime(10087): at org.keyczar.Encrypter.encrypt(Encrypter.java:114) E/AndroidRuntime(10087): at org.keyczar.Encrypter.encrypt(Encrypter.java:186)

kruton commented 11 years ago

Do you have a test case?

EdmondChuiHW commented 11 years ago

I found out the reason while I'm testing. When the input is an empty String, the second entry after it will cause this error.

Here is a test case, after encrypting the first empty String, encrypting "Jacob" does not have any problem, however encrypting "Sophia" will cause such Exception to be thrown. And the solution is to do an empty String test.

I believe an empty String is not a valid input for the Crypter then.

Thanks!

package com.example.keyczartest;

import org.keyczar.Crypter;
import org.keyczar.exceptions.KeyczarException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.android.keyczardemo.AndroidKeyczarReader;
import com.example.android.keyczardemo.FixBrokenCipherSpiProvider;

public class MainActivity extends Activity {
    public static final String TAG = "MainActivity";
    public static String[] TEXT = {"ABCD1234", "UNKNOWN", "", "Jacob", "Sophia",
            "Mason", "Isabella", "William", "Emma", "Jayden", "Olivia", "Noah",
            "Ava", "Michael", "Emily", "Ethan", "Abigail", "", "Alexander", "",
            "Madison", "Aiden", "Mia", "Daniel", "Chloe"};

    static {
        FixBrokenCipherSpiProvider.insertIfNeeded();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView)findViewById(android.R.id.text1);

        try {
            Crypter crypter = new Crypter(new AndroidKeyczarReader(getResources(), "test_keys_public_dont_not_use"));

            for (String string : TEXT) {
                // Solution
                // if (TextUtils.isEmpty(string)) {
                // continue;
                // }
                Log.d(TAG, "trying to encrypt: " + string);
                textView.append(crypter.encrypt(string));
                textView.append("\n");
            }
        } catch (KeyczarException e) {
            Log.e(TAG, "WHYYYYYYYYYYYYYYY D:", e);
        }
    }
}