前cipher.init()方法Android的加密字符串停止 [英] Android string encryption stops before cipher.init() method

查看:3417
本文介绍了前cipher.init()方法Android的加密字符串停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我尝试用加密方法。谁能告诉我哪里是不允许系统显示结果错误?

该系统前行停止 cipher.init()

登录猫

 十二月一日至一日:37:37.756:D / libEGL(2810):加载/system/lib/egl/libEGL_genymotion.so
12月1日至1日:37:37.756:D /(2810):HostConnection ::得到()新主机连接建立0xb8d1bce8,TID 2810
12月1日至1日:37:37.872:D / libEGL(2810):加载/system/lib/egl/libGLESv1_CM_genymotion.so
12月1日至1日:37:37.872:D / libEGL(2810):加载/system/lib/egl/libGLESv2_genymotion.so
12月1日至1日:37:38.112:W / EGL_genymotion(2810):eglSurfaceAttrib没有实现
12月1日至1日:37:38.112:E / OpenGLRenderer(2810):获得MAX_TEXTURE_SIZE从GradienCache
12月1日至1日:37:38.180:E / OpenGLRenderer(2810):从缓存获取MAX_TEXTURE_SIZE :: initConstraints()
12月1日至1日:37:38.196:D / OpenGLRenderer(2810):启用调试模式0
12月1日至1日:37:47.508:W / EGL_genymotion(2810):eglSurfaceAttrib没有实现
12月1日至1日:37:52.344:W / EGL_genymotion(2810):eglSurfaceAttrib没有实现
12月1日至1日:37:52.452:D / dalvikvm(2810):GC_FOR_ALLOC释放125K,1%的自由16924K / 17072K,暂停14毫秒,16毫秒总
12月1日至1日:37:56.420:E / PBEkEYsPEC(2810):javax.crypto.spec.PBEKeySpec@52de0e0c
12月1日至1日:37:56.492:E / PBEkEYsPEC(2810):com.android.org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey@52e0076c
12月1日至1日:37:56.492:E / PBEkEYsPEC(2810):javax.crypto.spec.PBEParameterSpec@52e00df8

code

 公共字符串Padding_key(){  尝试{    PBEKeySpec pbeKeySpec =新PBEKeySpec(STReditTxtPass.toCharArray());
    Log.e(PBEkEYsPEC,pbeKeySpec.toString());
    Toast.makeText(这一点,第1步,Toast.LENGTH_SHORT).show();
    密密码= Cipher.getInstance(AES);
    Toast.makeText(这一点,后加密,Toast.LENGTH_SHORT).show();
    SecretKeyFactory的KeyFactory = SecretKeyFactory
    .getInstance(PBEWithMD5AndDES);
    Toast.makeText(这一点,之后的KeyFactory,Toast.LENGTH_SHORT).show();    SecretKey的PBEKey调用= keyFactory.generateSecret(pbeKeySpec);
    Log.e(PBEkEYsPEC,pbeKey.toString());
    Toast.makeText(这一点,之后SecreteKey,Toast.LENGTH_SHORT).show();    PBEParameterSpec pbeSpec =新PBEParameterSpec(盐,迭代);
    Log.e(PBEkEYsPEC,pbeSpec.toString());
    Toast.makeText(这一点,之后PBEParameterSpec,Toast.LENGTH_SHORT).show();
    cipher.init(Cipher.ENCRYPT_MODE,PBEKey调用,pbeSpec);    Toast.makeText(这一点,之后cypher.init,Toast.LENGTH_SHORT).show();    字节[] =密文cipher.doFinal(PlainText.getBytes(UTF-8));
    Toast.makeText(这一点,之后的byte [],Toast.LENGTH_SHORT).show();    密文=的String.format(%s%s%S,toBase64(盐)],
    toBase64(密文));
    Toast.makeText(这一点,之后cypherText.format,Toast.LENGTH_SHORT).show();    edit_txt_enc_string.setText(密文);    。strPaddingencryption = edit_txt_enc_string.getText()的toString();  }赶上(例外五){  }
  返回strPaddingencryption;
}


解决方案

好像要执行基于密码的加密。如果是这样的话,你的错误可能是由使用AES引发作为算法在密码对象。试试这个:

 密码加密= Cipher.getInstance(PBEWithMD5AndDES);

另外请注意, PBEWithMD5AndDES 是按现代标准非常薄弱。如果您对所使用的算法任何控制,请切换到更强烈的东西。例如,你可以使用 PBKDF2WithHmacSHA1 来产生一个关键,你再与AES使用加密明文。

请参阅在code 我的项目,JNCryptor,对于如何在Java中做一个例子。

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

解决方案

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.

这篇关于前cipher.init()方法Android的加密字符串停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆