473,471 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Java DES encryption

3 New Member
I'm trying to do a simple encryption/decryption program. The code below works fine if I never convert the encrypted text into a string. My problem occurs when i convert it to a string and then I convert it back into bytes. I want to eventually create a GUI, so I figured that if there is a encrypted text it will be assigned to a variable from a text field via a string. Please help if you can. Sorry for any confusion.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.security.InvalidKeyException;
  3. import java.security.NoSuchAlgorithmException;
  4.  
  5. import javax.crypto.BadPaddingException;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.IllegalBlockSizeException;
  8. import javax.crypto.KeyGenerator;
  9. import javax.crypto.NoSuchPaddingException;
  10. import javax.crypto.SecretKey;
  11.  
  12.  
  13. public class Main {
  14.  
  15.     public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
  16.         KeyGenerator keyGen = KeyGenerator.getInstance("DES");
  17.         SecretKey myDesKey = keyGen.generateKey();
  18.  
  19.         Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  20.         cipher.init(Cipher.ENCRYPT_MODE, myDesKey);
  21.         String message = "TEST";
  22.         byte[] input = message.getBytes();
  23.         System.out.println(input);
  24.  
  25.  
  26.         // Encrypt
  27.         byte[] messageEncrypted = cipher.doFinal(input);
  28.  
  29.         // Decrypt
  30.         String text = messageEncrypted.toString();
  31.         System.out.println(text);
  32.         System.out.println(messageEncrypted);
  33.         byte[] textMessageEncrypted;
  34.         textMessageEncrypted = text.getBytes();
  35.         System.out.println(textMessageEncrypted);
  36.         cipher.init(Cipher.DECRYPT_MODE, myDesKey);
  37.         byte[] messageDecrypted = cipher.doFinal(textMessageEncrypted);
  38.         System.out.println("Text Decrypted: " + new String(messageDecrypted));
  39.     }
  40.  
  41. }
  42.  
  43.  
Here is the error:
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
  2.     at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
  3.     at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
  4.     at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA13*..)[B@4bdb699b
  5.  
Feb 25 '12 #1
0 2009

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

Similar topics

2
by: Hal Vaughan | last post by:
I have no background in encryption, so I'm working with samples I've found in various places and patching them together. I know Blowfish can use a 56 byte key. The version of this program in Perl...
2
by: SonofonoS | last post by:
Hi NG. How do I encrypt a string from a C# application and then be able to read that string on a java applikation. Im figuring that I should use something with a public and a private key,...
4
by: Daniel Orner | last post by:
Hello, I'm working on a project that consists of a Java application on the desktop, and a server with Python CGI code. I'm trying to find an encryption algorithm, which would let me send...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: Ravi | last post by:
H I have a requirement where i need to encrypt the data using 3DES in C# and pass on the data to the Application server where i need to decrypt the data. The application server is a Unix Box...
2
by: Carolyn Vo | last post by:
We have code in Java that encrypts a string using DES. However, when we encrypt in C# the string is encrypted differently. The code to encrypt in Java is: DESKeySpec desKeySpec = new...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
4
by: Robert Blass | last post by:
I am looking to get my feet wet with encryption. When I say encryption program I am talking about something to get me off to a quick start. Something very simple, far less than the 40+ bit...
1
by: qiss | last post by:
Essentially my problem is that I have a java application that uses SHA-1 encryption and I have a .Net 2.0 WebService that needs to encrypt the same way for user authentication (passwords are...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.