0

I am creating an Android application that encrypts a string using a user-chosen password. The system will display the encrypted string in the edit text.

This is the method that I try to encrypt with. Can anyone tell me where is the error that do not allow the system to show the result?

The system stops before the line cipher.init().

Log cat

01-01 12:37:37.756: D/libEGL(2810): loaded /system/lib/egl/libEGL_genymotion.so
01-01 12:37:37.756: D/(2810): HostConnection::get() New Host Connection established 0xb8d1bce8, tid 2810
01-01 12:37:37.872: D/libEGL(2810): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
01-01 12:37:37.872: D/libEGL(2810): loaded /system/lib/egl/libGLESv2_genymotion.so
01-01 12:37:38.112: W/EGL_genymotion(2810): eglSurfaceAttrib not implemented
01-01 12:37:38.112: E/OpenGLRenderer(2810): Getting MAX_TEXTURE_SIZE from GradienCache
01-01 12:37:38.180: E/OpenGLRenderer(2810): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
01-01 12:37:38.196: D/OpenGLRenderer(2810): Enabling debug mode 0
01-01 12:37:47.508: W/EGL_genymotion(2810): eglSurfaceAttrib not implemented
01-01 12:37:52.344: W/EGL_genymotion(2810): eglSurfaceAttrib not implemented
01-01 12:37:52.452: D/dalvikvm(2810): GC_FOR_ALLOC freed 125K, 1% free 16924K/17072K, paused 14ms, total 16ms
01-01 12:37:56.420: E/PBEkEYsPEC(2810): javax.crypto.spec.PBEKeySpec@52de0e0c
01-01 12:37:56.492: E/PBEkEYsPEC(2810): com.android.org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey@52e0076c
01-01 12:37:56.492: E/PBEkEYsPEC(2810): javax.crypto.spec.PBEParameterSpec@52e00df8

Code

public String Padding_key() {

  try {

    PBEKeySpec pbeKeySpec = new PBEKeySpec(STReditTxtPass.toCharArray());
    Log.e("PBEkEYsPEC", pbeKeySpec.toString());
    Toast.makeText(this, "step 1", Toast.LENGTH_SHORT).show();
    Cipher cipher = Cipher.getInstance("AES");
    Toast.makeText(this, "after ciphering", Toast.LENGTH_SHORT).show();
    SecretKeyFactory keyFactory = SecretKeyFactory
    .getInstance("PBEWithMD5AndDES");
    Toast.makeText(this, "after keyFactory", Toast.LENGTH_SHORT).show();

    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
    Log.e("PBEkEYsPEC", pbeKey.toString());
    Toast.makeText(this, "after SecreteKey", Toast.LENGTH_SHORT).show();

    PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iterations);
    Log.e("PBEkEYsPEC", pbeSpec.toString());
    Toast.makeText(this, "after PBEParameterSpec", Toast.LENGTH_SHORT).show();
    cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeSpec);

    Toast.makeText(this, "after cypher.init", Toast.LENGTH_SHORT).show();

    byte[] cipherText = cipher.doFinal(PlainText.getBytes("UTF-8"));
    Toast.makeText(this, "after byte[]", Toast.LENGTH_SHORT).show();

    cyphertext = String.format("%s%s%s", toBase64(salt), "]",
    toBase64(cipherText));
    Toast.makeText(this, "after cypherText.format", Toast.LENGTH_SHORT).show();

    edit_txt_enc_string.setText(cyphertext);

    strPaddingencryption = edit_txt_enc_string.getText().toString();

  } catch (Exception e) {

  }
  return strPaddingencryption;
}
reixa
  • 6,903
  • 6
  • 49
  • 68
user3006788
  • 165
  • 1
  • 14
  • 30

1 Answers1

0

It seems like you want to perform password-based encryption. If that is the case, your error is likely caused by using "AES" as the algorithm in your Cipher object. Try this instead:

Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");

Please also note that PBEWithMD5AndDES is very weak by modern standards. If you have any control over the algorithm used, please switch to something stronger. For instance, you could use PBKDF2WithHmacSHA1 to produce a key that you then use with AES to encrypt the plaintext.

See the code for my project, JNCryptor, for an example of how this is done in Java.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254