AES解密为j2me

时间:2014-07-27 00:56:18

标签: encryption encoding java-me aes bouncycastle

我在这个链接上看到了 AES Encryption/Decryption with Bouncycastle Example in J2ME  关于如何使用Bouncy Castle提供的AES加密字符串。加密方法代码工作正常,但解密不起作用。

有人可以建议我如何解密加密的字符串吗?

我使用以下代码进行测试:

import com.codename1.util.Base64;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;

/**
 *
 * @author SAMUEL
 */
public class Tester {
    static  String strEnc = "Hi This is my String";
    final static String strPassword = "password12345678";

    private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)throws Exception
{
    int minSize = cipher.getOutputSize(data.length);
    byte[] outBuf = new byte[minSize];
    int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
    int length2 = cipher.doFinal(outBuf, length1);
    int actualLength = length1 + length2;
    byte[] result = new byte[actualLength];
    System.arraycopy(outBuf, 0, result, 0, result.length);
    return result; 
}

private static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(false, ivAndKey);
    return cipherData(aes, cipher);
}

private static byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher  aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(true, ivAndKey);
    return cipherData(aes, plain);
}


public static void main(String [] args) throws Exception{

    byte[] enc= encrypt(strEnc.getBytes(),"password12345678".getBytes(), "password12345678".getBytes());
    String encrypted =   Base64.encode(enc);
    System.out.println("Encrypted is:"+encrypted);
    byte[] dec= decrypt(encrypted.getBytes(),"password12345678".getBytes() , "password12345678".getBytes());        
    System.out.println("Decrypted file is:"+dec);
 }
}

输出是:

Encrypted is:sw0SrUIKe0DmS7sRd9+XMgtYg+BUiAfiOsdMw/Lo2RA=

异常堆栈跟踪是:

Exception in thread "main" org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption
    at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(PaddedBufferedBlockCipher.java:281)
    at com.mycompany.myapp.Tester.cipherData(Tester.java:28)
    at com.mycompany.myapp.Tester.decrypt(Tester.java:40)
    at com.mycompany.myapp.Tester.main(Tester.java:57)

1 个答案:

答案 0 :(得分:3)

您忘记在解密之前对密文进行64解码。您还应该从解密的字节创建一个新的字符串。

编码/解码字符串时,请始终明确指定编码,否则您将使用平台默认值,这在每个平台上都不相同。

因此,请使用例如new String(dec, "UTF-8")重新创建明文,并为每个"UTF-8"new String()方法指定toBytes()