Triple DES encryption

Hi there,

Im trying to work on DES encryption. I have C++  code provided. Im trying to convert this code to JAVA. I am able to convert most of it but, the encrpted key doesnot match, I mean, there is definetly something missin on my java side. Please help. Attached working  JAVA code which needs some correction .

Summary:

Triple DES encryption parameters:
The following parameters should be used as part of the encryption process.
Key Size : 64 bytes
Padding Mode : PKCS7
Intialization Vector : An 8- byte array
Block Size : 128 bytes
Key : <key_phrase>
CipherMode : CBC  (Cipher Block Chaining)


(OP and Comments edited by  _alias99)
(OP and Comments edited by SouthMod 10/01/09)
import org.apache.commons.codec.binary.*;
import java.security.spec.AlgorithmParameterSpec; 
 
public class Multiply{
private long expireTimeInMilliseconds;
 
        public static void main(String[] args) throws Exception {
               
 
                String s = integerObj + "0";
                integerObj = Integer.parseInt(s);
 
               if(args.length != 2){
								System.out.println("Usage: java Multiply <rows> <columns>");
								System.exit(0);
								}
								int rows = 0, columns = 0;
								try{
								rows = Integer.parseInt(args[0]);
								columns = Integer.parseInt(args[1]);
								}catch(NumberFormatException e){
								System.out.println("Wrong parameter(s)");
								System.exit(0);
								}
								System.out.println("Multiplication Table ("+rows+" X "+columns+")");
								for(int i = 1;i <= rows;i++){
								System.out.print(i+"\t>");
								for(int j = 1;j <= columns;j++)
								System.out.print("\t"+(i*j));
								System.out.println("");
        }
 
    public void setExpireTimeInDays(int expireTimeInDays)
{
   expireTimeInMilliseconds = ((long) expireTimeInDays) * 24 * 60 * 60 * 1000;
}

Open in new window

hameedsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>byte[] input =INPUT_TEXT.getBytes();

You should specify the encoding there, as you do with SHARED_KEY
0
CEHJCommented:
You're not using TripleDES, but DES, on the Java side. In Java, it's "DESede":

http://www.java2s.com/Code/Java/Security/TripleDES.htm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hameedsAuthor Commented:
Thank you.. I changed the algorithm to DESede. That was very helpful.

I get this error:
Exception in thread "main" java.security.InvalidKeyException: Wrong key size
at javax.crypto.spec.DESedeKeySpec.<init>(DashoA12275)
at javax.crypto.spec.DESedeKeySpec.<init>(DashoA12275)
at TripleDES.main(TripleDES.java:23)

Looks like my key is 16 bytes, but DESede needs 24 bytes.. How do I generate 24 bytes..

import org.apache.commons.codec.binary.*;
import java.security.spec.AlgorithmParameterSpec; 
 
public class Multiply{
private long expireTimeInMilliseconds;
 
        public static void main(String[] args) throws Exception {
               
 
                String s = integerObj + "0";
                integerObj = Integer.parseInt(s);
 
               if(args.length != 2){
								System.out.println("Usage: java Multiply <rows> <columns>");
								System.exit(0);
								}
								int rows = 0, columns = 0;
								try{
								rows = Integer.parseInt(args[0]);
								columns = Integer.parseInt(args[1]);
								}catch(NumberFormatException e){
								System.out.println("Wrong parameter(s)");
								System.exit(0);
								}
								System.out.println("Multiplication Table ("+rows+" X "+columns+")");
								for(int i = 1;i <= rows;i++){
								System.out.print(i+"\t>");
								for(int j = 1;j <= columns;j++)
								System.out.print("\t"+(i*j));
								System.out.println("");
        }
 
    public void setExpireTimeInDays(int expireTimeInDays)
{
   expireTimeInMilliseconds = ((long) expireTimeInDays) * 24 * 60 * 60 * 1000;
}

Open in new window

0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

CEHJCommented:
Well it's going to be better to find out how the .NET side is doing this, but you could try experimenting. Try null - filling a byte[24] and putting the 16 at the beginning
0
Mick BarryJava DeveloperCommented:
Key Size : 16 bytes
Key : This is an MD5 hash of the shared key (eg: MD5 hash of 'some shared key comes here' in this case)

they appear to conflict
0
hameedsAuthor Commented:
Thank you for your support. I  was recently in trouble for posting some  Security Aglorithm which is sensitive and proprietary information. Please REPLACE  the post code snippet with the following code snippet.
import org.apache.commons.codec.binary.*;
import java.security.spec.AlgorithmParameterSpec; 
 
public class Multiply{
private long expireTimeInMilliseconds;
 
        public static void main(String[] args) throws Exception {
               
 
                String s = integerObj + "0";
                integerObj = Integer.parseInt(s);
 
               if(args.length != 2){
								System.out.println("Usage: java Multiply <rows> <columns>");
								System.exit(0);
								}
								int rows = 0, columns = 0;
								try{
								rows = Integer.parseInt(args[0]);
								columns = Integer.parseInt(args[1]);
								}catch(NumberFormatException e){
								System.out.println("Wrong parameter(s)");
								System.exit(0);
								}
								System.out.println("Multiplication Table ("+rows+" X "+columns+")");
								for(int i = 1;i <= rows;i++){
								System.out.print(i+"\t>");
								for(int j = 1;j <= columns;j++)
								System.out.print("\t"+(i*j));
								System.out.println("");
        }
 
    public void setExpireTimeInDays(int expireTimeInDays)
{
   expireTimeInMilliseconds = ((long) expireTimeInDays) * 24 * 60 * 60 * 1000;
}

Open in new window

0
hameedsAuthor Commented:
Please replace the code snippets in question and comments with the following code snippet.

ID:24425851
ID:24443064
import org.apache.commons.codec.binary.*;
import java.security.spec.AlgorithmParameterSpec; 
 
public class Multiply{
private long expireTimeInMilliseconds;
 
        public static void main(String[] args) throws Exception {
               
 
                String s = integerObj + "0";
                integerObj = Integer.parseInt(s);
 
               if(args.length != 2){
								System.out.println("Usage: java Multiply <rows> <columns>");
								System.exit(0);
								}
								int rows = 0, columns = 0;
								try{
								rows = Integer.parseInt(args[0]);
								columns = Integer.parseInt(args[1]);
								}catch(NumberFormatException e){
								System.out.println("Wrong parameter(s)");
								System.exit(0);
								}
								System.out.println("Multiplication Table ("+rows+" X "+columns+")");
								for(int i = 1;i <= rows;i++){
								System.out.print(i+"\t>");
								for(int j = 1;j <= columns;j++)
								System.out.print("\t"+(i*j));
								System.out.println("");
        }
 
    public void setExpireTimeInDays(int expireTimeInDays)
{
   expireTimeInMilliseconds = ((long) expireTimeInDays) * 24 * 60 * 60 * 1000;
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.