我一直在寻找这个问题的答案,但我找不到的任何东西都能给我回复我可以使用的实际修正代码,除非我真的遗漏了一些东西。我有一个简单的程序,我正在测试加密和解密。我有用户输入,然后加密和解密按钮,onClick应显示加密,然后解密的数据,但我不断收到此错误:
1105-1105/io.test.encryption.app W/System.err﹕ javax.crypto.BadPaddingException: pad block corrupted
1105-1105/io.test.encryption.app W/System.err﹕ at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:739)
1105-1105/io.test.encryption.app W/System.err﹕ at javax.crypto.Cipher.doFinal(Cipher.java:1204)
1105-1105/io.test.encryption.app W/System.err﹕ at io.test.encryption.app.SimpleCrypto.decrypt(SimpleCrypto.java:61)
1105-1105/io.test.encryption.app W/System.err﹕ at io.test.encryption.app.SimpleCrypto.decrypt(SimpleCrypto.java:34)
1105-1105/io.test.encryption.app W/System.err﹕ at io.test.encryption.app.MainActivity$2.onClick(MainActivity.java:55)
1105-1105/io.test.encryption.app W/System.err﹕ at android.view.View.performClick(View.java:4438)
1105-1105/io.test.encryption.app W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
1105-1105/io.test.encryption.app W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
1105-1105/io.test.encryption.app W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
1105-1105/io.test.encryption.app W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
1105-1105/io.test.encryption.app W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
1105-1105/io.test.encryption.app W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
1105-1105/io.test.encryption.app W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
1105-1105/io.test.encryption.app W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
1105-1105/io.test.encryption.app W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
1105-1105/io.test.encryption.app W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
这是我的主要活动:
public class MainActivity extends ActionBarActivity {
String seedValue = "This is a Secret";
String encrypted, decrypted;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText inputText = (EditText) findViewById(R.id.inputText);
final TextView textView1= (TextView) findViewById(R.id.textView1);
final String input = inputText.getText().toString();
Button button1= (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
encrypted = SimpleCrypto.encrypt(seedValue, input);
} catch (Exception e) {
e.printStackTrace();
}
textView1.setText(encrypted);
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
decrypted = SimpleCrypto.decrypt(seedValue, encrypted);
} catch (Exception e) {
e.printStackTrace();
}
final TextView textView2= (TextView) findViewById(R.id.textView2);
textView2.setText(decrypted);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的Simple Crypto Class
public class SimpleCrypto {
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
final SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}