Android字符串加密在cipher.init()方法之前停止

时间:2014-01-01 12:42:35

标签: java android encryption jce

我正在创建一个使用用户选择的密码加密字符串的Android应用程序。系统将在编辑文本中显示加密的字符串。

这是我尝试加密的方法。任何人都可以告诉我哪个错误不允许系统显示结果?

系统在行cipher.init()之前停止。

记录猫

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

代码

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;
}

1 个答案:

答案 0 :(得分:0)

您似乎想要执行基于密码的加密。如果是这种情况,则可能是因为"AES"对象中使用Cipher作为算法而导致错误。试试这个:

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

请注意,现代标准PBEWithMD5AndDES非常弱。如果您对所使用的算法有任何控制权,请切换到更强大的功能。例如,您可以使用PBKDF2WithHmacSHA1生成一个密钥,然后将其与AES一起使用以加密明文。

请参阅我的项目the code,JNCryptor,以获取如何在Java中完成此操作的示例。