473,804 Members | 3,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error while Decrypting the String

30 New Member
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.Il legalBlockSizeE xception: 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.sp ec.*;
public class CryptoLibrary
{
public static void main(String[] args)

{
Cipher encryptCipher;

Cipher decryptCipher;
sun.misc.BASE64 Encoder encoder = new sun.misc.BASE64 Encoder();
sun.misc.BASE64 Decoder decoder = new sun.misc.BASE64 Decoder();
java.security.S ecurity.addProv ider(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
{
PBEParameterSpe c ps = new javax.crypto.sp ec.PBEParameter Spec(salt, 20);
SecretKeyFactor y kf = SecretKeyFactor y.getInstance(" PBEWithMD5AndDE S");
SecretKey k = kf.generateSecr et(new javax.crypto.sp ec.PBEKeySpec(p ass));
encryptCipher = Cipher.getInsta nce("PBEWithMD5 AndDES/CBC/PKCS5Padding");
encryptCipher.i nit(Cipher.ENCR YPT_MODE, k, ps);
decryptCipher = Cipher.getInsta nce("PBEWithMD5 AndDES/CBC/PKCS5Padding");
decryptCipher.i nit(Cipher.DECR YPT_MODE, k, ps);
}
catch (Exception e)
{
throw new SecurityExcepti on("Could not initialize CryptoLibrary: " + e.getMessage()) ;
}
try
{
byte[] utf8 = "Arjunan".getBy tes("UTF8");
byte[] enc = encryptCipher.d oFinal(utf8);
encoded=encoder .encode(enc);
}
catch (Exception e)
{
throw new SecurityExcepti on("Could not encrypt: " + e.getMessage()) ;
}
try
{
byte[] dec = decoder.decodeB uffer("Arjunan" );
byte[] utf8 = decryptCipher.d oFinal(dec);
System.out.prin tln("SSSSSSSSSS SSS ");
decoded= new String(utf8, "UTF8");
}
catch (Exception e)
{
System.out.prin tln("Error in decrypting the string "+e);
}
try
{
System.out.prin tln("Encoded: --> " + encoded);
System.out.prin tln("Decoded: --> " + decoded);

}
catch (Exception e)
{
System.out.prin tln("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 4704
cordeo
16 New Member
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 Contributor
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
412
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 string. I get this error: <error> Exception: Exception decrypting.Exception decrypting. Decryption
4
2783
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 basic things as "class", "operator ::", or even my compiler's error-messages would get explained in a quick-reference kind of way (as turbo pascal did with the pascal-language). Therefore I was forced to read some books and tutorials. However, I'm...
2
4197
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 opening the app the following wrror is generated: Exception decrypting. Decryption failed. Key not valid for use in specified state. Description: An unhandled exception occurred during the execution of the current web request. Please review the
3
29285
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 of the data to decrypt is invalid" and occurs on the csDecrypt.FlushFinalBlock.
3
14019
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 application and it works fine and no error jumps. However, when I tried to use it in a web page, problems came. There is no problem for encrypting but the decrypting can't be finished. The error jumped from the line below
2
1609
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 CRYPTPROTECT_PROMPTSTRUCT; I defined the variable as IntPtr and then set it to zero CryptProtectData/CryptUnrotectData return without error. The values between the C# and VB for CRYPTPROTECT_PROMPTSTRUCT are exact except for szPrompt C# passes null...
1
8990
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 is: - Exception decrypting. Decryption failed. Key not
4
2292
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 ciphertext w/ the encrypting key doesn't result in the original message--, which I'm assuming is linked to a coding error. I've spent hours pouring over this code, and I can't find anything. I must be missing something...something subtle. Here's the...
5
3447
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 to do with the company so i cannot contact him to get the info i require. Ive looked over his computer and accessed the .dat (I have no idea why its a .dat file) file using the password however when i open it up in Access i can view the data with...
0
9589
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9163
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.