解密时我得到
W/System.err(517): javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
我的加密和解密代码如下。我搜索了整个论坛也遇到了同样的问题,但无法让我的代码工作。
还有一个问题,或者你可以说只是好奇,当我单击按钮时,我可以看到加密的字符串,但是一次又一次地按下按钮,在相同的输入文本上给我不同的加密字符串,只有最后 4 个字符发生变化...... .我认为这将是解密后获取相同输入字符串的问题。
public class MainActivity extends Activity implements OnClickListener {
EditText ed1, ed2, ed3;
private final char[] PASSWORD = "abcdefghijklmnop".toCharArray();
private byte[] SALT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setting the SALT to android_id
SALT = Secure.getString(getContentResolver(), Secure.ANDROID_ID).getBytes();
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
}
public void onClick(View v)
{
String encrypted, decrypted, userpass = ed1.getText().toString().trim();
encrypted = encrypt(userpass);
ed2.setText(encrypted);
decrypted = decrypt(encrypted);
ed3.setText(decrypted);
}
public String encrypt(String original)
{
SecretKeyFactory secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = secretKey.generateSecret(new PBEKeySpec(PASSWORD));
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT,20));
String encrypted = cipher.doFinal(original.getBytes("UTF-8")).toString();
return encrypted;
}
public String decrypt(String original)
{
SecretKeyFactory secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = secretKey.generateSecret(new PBEKeySpec(PASSWORD));
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT,10));
String decrypted = cipher.doFinal(original.getBytes("UTF-8")).toString();
return decrypted;
}
}
最佳答案
Cipher.doFinal(byte[])
返回一个 byte[]
而不是 String
。当前代码的方式是存储 Object.toString()
的返回值,这对于 byte[]
来说不是很有意义,并且不显示其内容。如果要将字节数组存储为字符串,则需要先对其进行编码。推荐的方法是 Base64 ,但是十六进制字符串也可以工作,但不会像 Base64 那样紧凑。
如果您只是想查看byte[]
的内容以进行调试,您可以使用Arrays.toString(byte[])
来查看内容如何变化。尽管即使是为了调试,我认为查看 Base64 或十六进制字符串会更容易。
在解密方法中,您需要在解密之前将输入字符串解码回 byte[]
,即来自 Cipher.doFinal(byte[] 的结果字节数组)
解密端可以使用 new String(result, "UTF-8")
解释为 UTF-8。
关于java - JAVA(Android)解密出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19434698/