472,794 Members | 3,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

Error while Decrypting the String

Hi,

I wrote a java program for Encrypt and Decrypt the given string, when i execute the program it show me an error....but the string has Encrypted, if i want to Decrypt the string it show me an error...

The error is:

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

Encoded: --> pLqxYQViK5U
Decoded: --> null



what i did wrong in this code...
i posted my code here...

Code:

import java.io.*;
import java.security.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class CryptoLibrary
{
public static void main(String[] args)

{
Cipher encryptCipher;

Cipher decryptCipher;
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());
char[] pass = "A".toCharArray();
byte[] salt = {

(byte) 0xa3, (byte) 0x21, (byte) 0x24, (byte) 0x2c,

(byte) 0xf2, (byte) 0xd2, (byte) 0x3e, (byte) 0x19};
int iterations = 3;
String encoded,decoded=null;
try
{
PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pass));
encryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, k, ps);
decryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, k, ps);
}
catch (Exception e)
{
throw new SecurityException("Could not initialize CryptoLibrary: " + e.getMessage());
}
try
{
byte[] utf8 = "Arjunan".getBytes("UTF8");
byte[] enc = encryptCipher.doFinal(utf8);
encoded=encoder.encode(enc);
}
catch (Exception e)
{
throw new SecurityException("Could not encrypt: " + e.getMessage());
}
try
{
byte[] dec = decoder.decodeBuffer("Arjunan");
byte[] utf8 = decryptCipher.doFinal(dec);
System.out.println("SSSSSSSSSSSSS ");
decoded= new String(utf8, "UTF8");
}
catch (Exception e)
{
System.out.println("Error in decrypting the string "+e);
}
try
{
System.out.println("Encoded: --> " + encoded);
System.out.println("Decoded: --> " + decoded);

}
catch (Exception e)
{
System.out.println("Error in last line of the prg "+e);
}
}
}


if anyone get the answer... Let me know...


Thanks in Advance,

V. Prasath
Aug 13 '08 #1
2 4586
cordeo
16
You feed the original string into the decoder, not the encoded string:
Expand|Select|Wrap|Line Numbers
  1.             byte[] dec = decoder.decodeBuffer("Arjunan");
  2.  
That should of course be:

Expand|Select|Wrap|Line Numbers
  1.             byte[] dec = decoder.decodeBuffer(encoded);
  2.  
which solves your problem immediately.
Aug 29 '08 #2
blazedaces
284 100+
Also from now on please post your code in code tags (look to the top right corner when you're replying for instructions).

It makes code a lot more readable (like adding indentations, etc.)

-blazed
Aug 29 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: HardBap | last post by:
I'm following the MSDN Article here: <link> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT08.asp </link> All works fine until I try an decrypt the...
4
by: Piotr Sawuk | last post by:
I'm a newbie in the world of c++, and I am used to learn a programming language simply by programming. Unfortunately I where unable to find any useful helpfile for this language, in which such...
2
by: Dave Bailey | last post by:
I have developed a web app using DPAPI to encrypt a connection string in the web.config file. The application works perfectly on the development machine but when deployed to the server when...
3
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
3
by: James | last post by:
Hi, I am developing a ActiveX Control which will be used in a web page. The control will encrypt some value then decrypt it when the web page opens next time. I tested the control in a windows...
2
by: DazedAndConfused | last post by:
I converted a C# example of using dll crypt32 to VB .NET. The converted example fails when Encypting/Decypting. I found that if instead of defining a variable as and setting the values for...
1
by: BigLuzer | last post by:
hi i am using the following setup: - .net 1.1 - 2 load-balanced iis servers - DPAPI machine store. - C# - i encrypted the connection string separately, one on each machine. the error i get...
4
by: Protoman | last post by:
Can you please help me figure out what the error is here, in this rotor cipher simulator, which I wrote to amuse myself? The runtime error is that it isn't symmetrical --decrypting a piece of...
5
by: Harb247 | last post by:
Hi Guys, First Post. Need some help decrypting a String / DB The company i manage recently had an argument with their lead programmer for their data base. However he has decided to have nothing...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.