如何克服javax.crypto.badpaddingexception .. pad块损坏

时间:2014-06-12 10:42:07

标签: android encryption

我正在尝试加密某些内容并对其进行解密。我没有解密 - 我得到了上面的例外。我尝试改变ctLength和ptLength,但无济于事。我究竟做错了什么?

这是我的logcat错误:

06-12 15:56:43.184: W/System.err(9445): javax.crypto.BadPaddingException: pad block corrupted
06-12 15:56:43.194: W/System.err(9445):     at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:701)
06-12 15:56:43.194: W/System.err(9445):     at javax.crypto.Cipher.doFinal(Cipher.java:1106)
06-12 15:56:43.194: W/System.err(9445):     at com.androidhive.androidsqlite.SimpleCrypto.decrypt(SimpleCrypto.java:33)
06-12 15:56:43.194: W/System.err(9445):     at com.androidhive.androidsqlite.SimpleCrypto.decrypt(SimpleCrypto.java:23)
06-12 15:56:43.194: W/System.err(9445):     at com.androidhive.androidsqlite.AndroidSQLiteTutorialActivity.readFile(AndroidSQLiteTutorialActivity.java:406)
06-12 15:56:43.194: W/System.err(9445):     at com.androidhive.androidsqlite.AndroidSQLiteTutorialActivity.checkEpiery(AndroidSQLiteTutorialActivity.java:355)
06-12 15:56:43.194: W/System.err(9445):     at com.androidhive.androidsqlite.AndroidSQLiteTutorialActivity.onCreate(AndroidSQLiteTutorialActivity.java:123)
06-12 15:56:43.194: W/System.err(9445):     at android.app.Activity.performCreate(Activity.java:4465)
06-12 15:56:43.194: W/System.err(9445):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-12 15:56:43.194: W/System.err(9445):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
06-12 15:56:43.194: W/System.err(9445):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
06-12 15:56:43.194: W/System.err(9445):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-12 15:56:43.194: W/System.err(9445):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
06-12 15:56:43.194: W/System.err(9445):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 15:56:43.194: W/System.err(9445):     at android.os.Looper.loop(Looper.java:137)
06-12 15:56:43.194: W/System.err(9445):     at android.app.ActivityThread.main(ActivityThread.java:4441)
06-12 15:56:43.194: W/System.err(9445):     at java.lang.reflect.Method.invokeNative(Native Method)
06-12 15:56:43.194: W/System.err(9445):     at java.lang.reflect.Method.invoke(Method.java:511)
06-12 15:56:43.194: W/System.err(9445):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-12 15:56:43.204: W/System.err(9445):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-12 15:56:43.204: W/System.err(9445):     at dalvik.system.NativeStart.main(Native Method)
06-12 15:56:43.204: W/System.err(9445): javax.crypto.BadPaddingException: pad block corrupted
06-12 15:56:43.204: W/System.err(9445):     at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:701)
06-12 15:56:43.204: W/System.err(9445):     at javax.crypto.Cipher.doFinal(Cipher.java:1106)
06-12 15:56:43.204: W/System.err(9445):     at com.androidhive.androidsqlite.SimpleCrypto.decrypt(SimpleCrypto.java:33)
06-12 15:56:43.204: W/System.err(9445):     at com.androidhive.androidsqlite.SimpleCrypto.decrypt(SimpleCrypto.java:23)

这是我的simplecrypto类:

package com.commonsware.android.syssvc.alarm;

import java.security.SecureRandom;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

public class SimpleCrypto
{

    public SimpleCrypto()
    {
    }

    private static void appendHex(StringBuffer stringbuffer, byte byte0)
    {
        stringbuffer.append("0123456789ABCDEF".charAt(0xf & byte0 >> 4)).append("0123456789ABCDEF".charAt(byte0 & 0xf));
    }

    public static String decrypt(String s, String s1)
        throws Exception
    {
        return new String(decrypt(getRawKey(s.getBytes()), toByte(s1)));
    }

    private static byte[] decrypt(byte abyte0[], byte abyte1[])
        throws Exception
    {
        SecretKeySpec secretkeyspec = new SecretKeySpec(abyte0, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(2, secretkeyspec);
        return cipher.doFinal(abyte1);
    }

    public static String encrypt(String s, String s1)
        throws Exception
    {
        return toHex(encrypt(getRawKey(s.getBytes()), s1.getBytes()));
    }

    private static byte[] encrypt(byte abyte0[], byte abyte1[])
        throws Exception
    {
        SecretKeySpec secretkeyspec = new SecretKeySpec(abyte0, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(1, secretkeyspec);
        return cipher.doFinal(abyte1);
    }

    public static String fromHex(String s)
    {
        return new String(toByte(s));
    }

    private static byte[] getRawKey(byte abyte0[])
        throws Exception
    {
        KeyGenerator keygenerator = KeyGenerator.getInstance("AES");
        SecureRandom securerandom = SecureRandom.getInstance("SHA1PRNG","Crypto");
        securerandom.setSeed(abyte0);
        keygenerator.init(128, securerandom);
        return keygenerator.generateKey().getEncoded();
    }

    public static byte[] toByte(String s)
    {
        int i = s.length() / 2;
        byte abyte0[] = new byte[i];
        int j = 0;
        do
        {
            if(j >= i)
                return abyte0;
            abyte0[j] = Integer.valueOf(s.substring(j * 2, 2 + j * 2), 16).byteValue();
            j++;
        } while(true);
    }

    public static String toHex(String s)
    {
        return toHex(s.getBytes());
    }

    public static String toHex(byte abyte0[])
    {
        if(abyte0 == null)
            return "";
        StringBuffer stringbuffer = new StringBuffer(2 * abyte0.length);
        int i = 0;
        do
        {
            if(i >= abyte0.length)
                return stringbuffer.toString();
            appendHex(stringbuffer, abyte0[i]);
            i++;
        } while(true);
    }

    private static final String HEX = "0123456789ABCDEF";
}

0 个答案:

没有答案