Error in Decryption

Dear All,

This is my first post. Require your help in the resolving the following issue.

I have an encrypter and corresponding decrypter using DES algorithm compiled using Sun JDK. Now the encrypter is being used in IBM JRE environment. The file is getting encrypted, but when I try to decrypt it using Sun JDK, it is looking for the ibm classes (com.ibm.provider.DESKey) and is not able to decrypt the file. Can anyone help, why is the decrypter looking for the ibm classes.

The JRE versions of both IBM and Sun are 1.4.2.

Thanks in advance for the help...

[588 byte] By [akshay_albala] at [2007-11-26 19:42:26]
# 1
Hard to tell without seeing your exact code - but I suspect it's because your code climbs into the provider, instead of sticking to the JCE interfaces.Grant
ggaineya at 2007-7-9 22:24:28 > top of Java-index,Security,Cryptography...
# 2

The Encrypter Class

// Decompiled by DJ v3.2.2.67 Copyright 2002 Atanas Neshkov Date: 2/7/2007 5:53:46 PM

// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!

// Decompiler options: packimports(3)

// Source File Name:Encrypter.java

package com.aurionpro.crypto;

import java.io.*;

import java.util.Vector;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

// Referenced classes of package com.aurionpro.crypto:

//Crypter, AppConfiguration

public class Encrypter extends Crypter

{

public Encrypter()

throws Exception

{

}

public void encryptFile(String s)

{

long l = System.currentTimeMillis();

System.out.println("Started at " + l + " milliseconds");

Object obj = null;

Object obj1 = null;

Object obj2 = null;

Object obj3 = null;

Object obj4 = null;

AppConfiguration appconfiguration = AppConfiguration.getAppConfiguration();

String s1 = "";

try

{

File file = new File(s);

String s2 = appconfiguration.getDetail("baseDir") + System.getProperty("file.separator") + appconfiguration.getDetail("sendDir") + System.getProperty("file.separator") + file.getName() + ".enc";

System.out.println(s2);

if(!file.exists())

{

System.out.println("Input File : " + s + " Not Found");

System.exit(1);

}

String s3 = s.substring(s.indexOf(".") + 1, s.length());

generateKey();

Vector vector = new Vector();

super.cipher.init(1, super.secretKey);

super.fileInputStream = new FileInputStream(s);

byte abyte0[] = new byte[super.fileInputStream.available() + 20];

super.cipherInputStream = new CipherInputStream(super.fileInputStream, super.cipher);

File file1 = new File(s2);

if(file1.exists())

{

System.out.println("Encrypted File : " + s2 + " Already Exists!!!");

System.exit(1);

}

super.fileOutputStream = new FileOutputStream(file1);

super.objectOutputStream = new ObjectOutputStream(super.fileOutputStream);

super.fileOutputStream = new FileOutputStream("temp.txt");

long l1;

while((l1 = super.cipherInputStream.read()) >= 0L)

super.fileOutputStream.write((char)(int)l1);

super.fileOutputStream.close();

super.fileInputStream = new FileInputStream("temp.txt");

abyte0 = new byte[super.fileInputStream.available()];

super.fileInputStream.read(abyte0);

vector.addElement(super.secretKey);

vector.addElement(s3);

vector.addElement(abyte0);

super.objectOutputStream.writeObject(vector);

super.objectOutputStream.close();

file1.setReadOnly();

super.fileInputStream.close();

super.cipherInputStream.close();

String s4 = appconfiguration.getDetail("deleteEncFile");

if(s4 != null && s4.trim().equals("Y"))

file.delete();

System.out.println("time taken : " + (System.currentTimeMillis() - l) / 1000 + " seconds");

}

catch(Exception exception)

{

System.out.println("Error while encrypting Code");

exception.printStackTrace();

}

}

public static void main(String args[])

throws Exception

{

Encrypter encrypter = new Encrypter();

if(args.length < 1)

{

System.out.println("Usage:java com.aurionpro.crypto.Encrypter sourceFile");

System.exit(0);

} else

{

encrypter.encryptFile(args[0]);

}

}

}

The crypter class

// Decompiled by DJ v3.2.2.67 Copyright 2002 Atanas Neshkov Date: 2/7/2007 5:53:37 PM

// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!

// Decompiler options: packimports(3)

// Source File Name:Crypter.java

package com.aurionpro.crypto;

import com.sun.crypto.provider.SunJCE;

import java.io.*;

import java.security.Security;

import javax.crypto.*;

public class Crypter

{

public Crypter()

throws Exception

{

secretKey = null;

cipher = null;

objectOutputStream = null;

fileOutputStream = null;

cipherInputStream = null;

objectInputStream = null;

fileInputStream = null;

byteArrayInputStream = null;

Security.addProvider(new SunJCE());

cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

}

public void generateKey()

throws Exception

{

KeyGenerator keygenerator = KeyGenerator.getInstance("DES");

secretKey = keygenerator.generateKey();

}

protected SecretKey secretKey;

protected Cipher cipher;

protected final int ENC_MODE = 1;

protected final int DEC_MODE = 2;

protected ObjectOutputStream objectOutputStream;

protected FileOutputStream fileOutputStream;

protected CipherInputStream cipherInputStream;

protected ObjectInputStream objectInputStream;

protected FileInputStream fileInputStream;

protected ByteArrayInputStream byteArrayInputStream;

}

akshay_albala at 2007-7-9 22:24:28 > top of Java-index,Security,Cryptography...
# 3
Pretty hard to tell why the Decrypter class is doing anything when you don't show it to us.
ejpa at 2007-7-9 22:24:28 > top of Java-index,Security,Cryptography...
# 4

The decrypter class

// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.

// Jad home page: http://www.kpdus.com/jad.html

// Decompiler options: packimports(3)

// Source File Name:Decrypter.java

package icici.matrix.utils;

import java.io.FileInputStream;

import java.io.PrintStream;

import java.math.BigInteger;

import java.util.Properties;

public class Decrypter

{

static class Params

{

BigInteger p;

BigInteger q;

BigInteger n;

BigInteger d;

BigInteger e;

BigInteger phi;

BigInteger max;

Params()

{

p = new BigInteger("0");

q = new BigInteger("0");

n = new BigInteger("0");

d = new BigInteger("0");

e = new BigInteger("15");

phi = new BigInteger("0");

}

}

public Decrypter()

{

}

public String decrypt()

{

Params par = new Params();

String password = "";

String propFile = "app.properties";

try

{

Properties prop = new Properties();

FileInputStream finput = new FileInputStream(propFile);

prop.load(finput);

password = (String)prop.get("password");

System.out.println(password);

}

catch(Exception e)

{

e.printStackTrace();

}

String cipherText = password;

String cipherTextTemp = "";

String decipheredText = "";

String outputText = "";

boolean flag = cipherText.startsWith("P");

if(flag)

cipherText = cipherText.substring(1, cipherText.length());

int blockSize = 4;

if(blockSize % 2 != 0)

{

System.out.println("blockSize must be even");

System.exit(1);

}

String maxEncodedString = "";

for(int cnt = 0; cnt < blockSize; cnt += 2)

maxEncodedString = maxEncodedString + "94";

par.max = new BigInteger(maxEncodedString);

par.p = par.max.divide(new BigInteger("502"));

Decrypter obj = new Decrypter();

obj.getKeys(par, blockSize);

decipheredText = obj.doRSA(cipherText, par.d, par.n, blockSize);

System.out.println("decipheredText\n" + decipheredText);

outputText = obj.decode(decipheredText);

if(flag)

outputText = outputText.substring(0, outputText.length() - 1);

System.out.println("outputText\n" + outputText);

return outputText;

}

void getKeys(Params par, int blockSize)

{

BigInteger bigOne = new BigInteger("1");

for(; !par.p.isProbablePrime(1000); par.p = par.p.add(bigOne));

String firstQ = par.p.subtract(bigOne);

par.q = new BigInteger(firstQ);

for(par.n = par.p.multiply(par.q); par.n.toString().length() > blockSize; par.n = par.p.multiply(par.q))

par.q = par.q.divide(new BigInteger("2"));

for(par.q = par.q.divide(new BigInteger("2")); !par.q.isProbablePrime(1000); par.q = par.q.subtract(bigOne));

for(par.n = par.p.multiply(par.q); par.n.compareTo(par.max) <= 0; par.n = par.p.multiply(par.q))

for(par.q = par.q.add(bigOne); !par.q.isProbablePrime(1000); par.q = par.q.add(bigOne));

if(par.n.toString().length() > blockSize || par.n.compareTo(par.max) <= 0)

{

System.out.println("Required conditions are not met");

System.exit(1);

}

BigInteger pPrime = par.p.subtract(bigOne);

BigInteger qPrime = par.q.subtract(bigOne);

for(par.phi = pPrime.multiply(qPrime); !par.e.gcd(par.phi).equals(bigOne); par.e = par.e.add(bigOne));

par.d = par.e.modInverse(par.phi);

if(blockSize > 6)

System.exit(0);

}

String decode(String encodedText)

{

String temp = "";

String decodedText = "";

for(int cnt = 0; cnt < encodedText.length(); cnt += 2)

{

temp = encodedText.substring(cnt, cnt + 2);

int val = Integer.parseInt(temp) + 32;

decodedText = decodedText + String.valueOf((char)val);

}

return decodedText;

}

String doRSA(String inputString, BigInteger exp, BigInteger n, int blockSize)

{

String temp = "";

String outputString = "";

for(int cnt = 0; cnt < inputString.length(); cnt += blockSize)

{

temp = inputString.substring(cnt, cnt + blockSize);

BigInteger block = new BigInteger(temp);

BigInteger output = block.modPow(exp, n);

for(temp = output.toString(); temp.length() < blockSize; temp = "0" + temp);

outputString = outputString + temp;

}

return outputString;

}

public String decrypt(String password)

{

Params par = new Params();

String cipherText = password;

String cipherTextTemp = "";

String decipheredText = "";

String outputText = "";

boolean flag = cipherText.startsWith("P");

if(flag)

cipherText = cipherText.substring(1, cipherText.length());

int blockSize = 4;

if(blockSize % 2 != 0)

{

System.out.println("blockSize must be even");

System.exit(1);

}

String maxEncodedString = "";

for(int cnt = 0; cnt < blockSize; cnt += 2)

maxEncodedString = maxEncodedString + "94";

par.max = new BigInteger(maxEncodedString);

par.p = par.max.divide(new BigInteger("502"));

Decrypter obj = new Decrypter();

obj.getKeys(par, blockSize);

decipheredText = obj.doRSA(cipherText, par.d, par.n, blockSize);

System.out.println("decipheredText\n" + decipheredText);

outputText = obj.decode(decipheredText);

if(flag)

outputText = outputText.substring(0, outputText.length() - 1);

System.out.println("outputText\n" + outputText);

return outputText;

}

}

akshay_albala at 2007-7-9 22:24:28 > top of Java-index,Security,Cryptography...
# 5
You don't have to jump through all these hoops yourself. You're using CipherOutputStream to encipher. Why not use CipherInputStream to decipher?Your Decrypter could probably extend Crypter, and look very similar to Encrypter.
ejpa at 2007-7-9 22:24:28 > top of Java-index,Security,Cryptography...