473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4663
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.