gpt4 book ai didi

android - base64 无效 key 异常 : Key length not 128/192/256 bits

转载 作者:行者123 更新时间:2023-11-29 16:10:58 25 4
gpt4 key购买 nike

我正在尝试使用 Base64 加密 editText 字符串,wnich 将保存在 sharedpreferences 中,但是一旦字符串被编码并保存,我的 .xml 文件就是空白的。我是一名新手程序员,对加密非常陌生,所以我迷路了,不知道如何正确分析我的代码以确定可能非常简单的问题。我收到了一些建议,但没有任何内容可以清楚地说明问题出在哪里、为什么不起作用以及如何解决。我怀疑我的加密方法但不确定在哪里。对于试图保护用户名和密码条目的编码人员来说,这一定是一项常见的任务。本教程用于此加密项目 Click Here

这里是编码、加密、保存EditText字符串的类:

public void onClick(View arg0) {
user=rName.getText().toString().trim();
pass=rPwd.getText().toString().trim();

if(arg0==regBttn){
if((user.length()!=0))
{
if((pass.length()!=0))
{

sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);
Editor myEditor=sp.edit();

byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };

try {
String encryptedUser = encrypt(user, key);
myEditor.putString("USERNAME_KEY", encryptedUser);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String encryptedPass = encrypt(pass, key);
myEditor.putString("PASSWORD_KEY", encryptedPass);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

myEditor.commit();
Toast.makeText(this, "Registration is successfull",10000).show();
i=new Intent(this,AccessApp.class);
startActivity(i);
}
else
{
Toast.makeText(this, "Please Enter password", 10000).show();
}}

else{
Toast.makeText(this,"Please Enter Username",10000).show();
}
}

else if(arg0==rtnBttn){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Exit");
builder.setMessage("Do you want to exit");
builder.setCancelable(false);
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}

public String encrypt(String toEncrypt, byte key[]) throws Exception {
SecretKeySpec secret = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());
String encrypted = Base64.encodeBytes(encryptedBytes);
return encrypted;

}

}

日志:

 11-11 23:41:59.873: W/System.err(11404): java.security.InvalidKeyException: Key length not 128/192/256 bits.
11-11 23:41:59.873: W/System.err(11404): at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineInit(JCEBlockCipher.java:570)
11-11 23:41:59.894: W/System.err(11404): at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineInit(JCEBlockCipher.java:617)
11-11 23:41:59.903: W/System.err(11404): at javax.crypto.Cipher.init(Cipher.java:519)
11-11 23:41:59.928: W/System.err(11404): at javax.crypto.Cipher.init(Cipher.java:479)
11-11 23:41:59.943: W/System.err(11404): at com.SharedPreferences.Login.SharedPrefLoginActivity.encrypt(SharedPrefLoginActivity.java:139)
11-11 23:41:59.953: W/System.err(11404): at com.SharedPreferences.Login.SharedPrefLoginActivity.onClick(SharedPrefLoginActivity.java:82)
11-11 23:41:59.953: W/System.err(11404): at android.view.View.performClick(View.java:3511)
11-11 23:41:59.953: W/System.err(11404): at android.view.View$PerformClick.run(View.java:14105)
11-11 23:41:59.973: W/System.err(11404): at android.os.Handler.handleCallback(Handler.java:605)
11-11 23:41:59.973: W/System.err(11404): at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 23:42:00.034: W/System.err(11404): at android.os.Looper.loop(Looper.java:137)
11-11 23:42:00.034: W/System.err(11404): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-11 23:42:00.034: W/System.err(11404): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 23:42:00.103: W/System.err(11404): at java.lang.reflect.Method.invoke(Method.java:511)
11-11 23:42:00.103: W/System.err(11404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-11 23:42:00.124: W/System.err(11404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-11 23:42:00.124: W/System.err(11404): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您似乎误算了字节数:

byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };

您需要一个 128 位的 key (16 个字节),但您在这里声明了 17 个字节。将 key 长度减一,我认为它应该有效。

关于android - base64 无效 key 异常 : Key length not 128/192/256 bits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13363754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com