encryption - AES decrypt for j2me -
I have read on this link how to encrypt the string using AUS, as it is by Buny Castle has given. Encryption method code works fine but decryption does not work.
Can anyone suggest how can I decrypt the encrypted string?
I have used the following code for testing:
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 Checker {Fixed String strEnc = "Hi This Is My String"; Last fixed string strPassword = "password12345678"; Private stationary byte [] cefdata (paddfblock seafar, byte [] data throws exceptions {int minSize = cipher.getOutputSize (data.length); Byte [] outBuf = New Byte [Min Sissy]; Int length 1 = cipher Processbits (data, 0, data length, outbuff, 0); Int length 2 = cipher .dogonal (outbreak, length 1); Int actual length = length 1 + length 2; Byte [] results = new byte [genuine lang]; System.arraycopy (outBuf, 0, results, 0, result length); Return result; } Private static byte [] decrypt (byte [] cipher, byte [] key, byte [] iv) throws an exception {PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher (new CBS blockkipper (new AESGN ()); Cypherpermators ivAndKey = new parameters (new keymapeters (key), iv); Aes.init (wrong, ivAndKey); Return Cefdata (AES, Cipher); } Private static byte [] Encrypt (byte [] plain, byte [] key, byte [] iv) Exceptions thrown {PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher (new CBCBlockCipher (new AESEngine ()); Cypherpermators ivAndKey = new parameters (new keymapeters (key), iv); Aes.init (true, ivAndKey); Return Cipher Data (AES, Plain); } Public static zero principal (string [] args) Exception {Byte [] Enc = Encrypt (strEnc.getBytes (), "password12345678" .getBytes (), "password12345678" .getBytes ()) throws; String Encrypted = Base64. encode (ANC); System.out.println ("is encrypted:" + encrypted); Byte [] dec = decrypt (encrypted. GetBytes (), "password12345678" .getBytes (), "password12345678" .getBytes ()); System.out.println ("Decrypted file is:" + dec); }}
Output is:
is encrypted: sw0SrUIKe0DmS7sRd9 + XMgtYg + BUiAfiOsdMw / Lo2RA =
and the exception Staktrres is:
exception in thread "main" org.bouncycastle.crypto.DataLengthException: last block decryption on org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.do incomplete (PaddedBufferedBlockCipher.java: 281) at com.mycompany.myapp.Tester.cipherData (tester.java28) at com.mycompany.myapp.Tester.decrypt (tester.java:40) at com.mycompany.myapp.Tester.main (tester.java ): 57)
If you have forgotten based on the decoded 64 ciphertext before decryption May include. You should also create a new string with decrypted bytes.
When encoding / decoding character strings, always clearly specify the encoding, otherwise the platform will use the default, which platform it is not the same one.
use for example new String (dec, "UTF-8") to create
again to plain text, and "UTF-8" Specify
and toBytes ()
method.
Comments
Post a Comment