0

I read on this link AES Encryption/Decryption with Bouncycastle Example in J2ME about how to encrypt a string using AES as supplied by Bouncy Castle. The encryption method code works fine but decryption doesn't work.

Can anyone suggest how I can decrypt the encrypted string?

I've used the following code to test:

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);
 }
}

Output is:

Encrypted is:sw0SrUIKe0DmS7sRd9+XMgtYg+BUiAfiOsdMw/Lo2RA=

And the exception stacktrace is:

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)
Community
  • 1
  • 1
San Ace
  • 1
  • 3
  • First of all, this question contains way too little information. Second, I wonder if you even read the comments below the answer you're pointing to. – Maarten Bodewes Jul 27 '14 at 01:23
  • it contains link to the main issue i am having.. i did and it works fine for crypting but i am having issues with decrypting that is why i am askin – San Ace Jul 27 '14 at 03:26
  • Eh, *where* is your code again? If you can edit your question I'd be happy to retract my close vote (these votes are mainly given if we *cannot* answer the question, not so much if we don't want to answer the question). – Maarten Bodewes Jul 27 '14 at 12:53
  • i am new to this site so i tried posting my code but for unknown reasons , i couldnt. but here is my code on codenameone forum... https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/codenameone-discussions/kx4P_k5-epw/au12EhysVtYJ – San Ace Jul 27 '14 at 13:46

1 Answers1

3

You are forgetting to base 64 decode the ciphertext before decryption. You should also create a new String from the decrypted bytes.

When encoding/decoding character strings, always specify the encoding explicitly, otherwise you will be using the platform default, which is not the same on each and every platform.

So use for instance new String(dec, "UTF-8") to recreate the plaintext, and specify "UTF-8" for each and every new String() and toBytes() method.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • It displays: this is a byte array instance ( `[B` ) identified by `a62fc3`. – Maarten Bodewes Jul 27 '14 at 14:17
  • You can say thanks by accepting. I don't think we're related though, try and not use too popular language on SO, it doesn't help the readability nor the look of SO. – Maarten Bodewes Jul 27 '14 at 14:28
  • hi . this has worked and i could encrypt and decrypt but i noticed when i encrypt and decrypt a string using aes but i noticed the result is different from the usual standard aes encryption/decryption eg on device :"hello" is "01Fvdltwa2VKVPCH3ueHAA==" but on standard aes "hello" is "0tUfc3Qwod0qw1kVaI8ksw==". how can i resolve this please – San Ace Aug 05 '14 at 12:32
  • Make sure that encoding, mode of operation, key bytes and IV bytes are identical. Note that for the same key, you should use different IV's - in effect then the ciphertext would *always* differ (otherwise you would leak information; you would be able to distinguish if you encrypted the (start of) the same plaintext twice). – Maarten Bodewes Aug 05 '14 at 13:08
  • it works fine on my device. the output on my device is different from the output on http://aesencryption.net.. that is the issue i am facing now because i want to exchange data with my server even when they use the same key but i cant do this since my server aes encryption/decryption isnt same with my device aes encryption/decryption... hope u understand what i mean??? – San Ace Aug 05 '14 at 15:02
  • Eh, no, unfortunately. – Maarten Bodewes Aug 05 '14 at 15:29
  • i mean when i encrypt and decrypt on my device. it works fine and i can see result but when i encrypt same string on aesencryption.net.. the encrypted result is different from my own.. and i dont know why it is different... eg when i encrypt on my device "hello" the result is "01Fvdltwa2VKVPCH3ueHAA==" but when i encrypt "hello" on aesencryption.net the result is "0tUfc3Qwod0qw1kVaI8ksw==".. these they are different .. why is my encrypted output on device different from aesencryption.net output? .. note: this is my encryption key"re5fdh3jxlow5ncc".. pls hw can i resolve this? – San Ace Aug 06 '14 at 09:36