?
Solved

Java Cryptography Under Linux (Weird Bug)

Posted on 2004-11-08
4
Medium Priority
?
292 Views
Last Modified: 2012-06-27
Hi,
   Does anyone know why the following simple code does not work under Linux? It works perfectly fine under Windows but I am no security expert.

The actual crypto functions:

import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;


public class SecurityAgent
{
    public static Cipher PBECipher;
    public static SecretKey PBEKey;
    public static PBEKeySpec PBEKeySpecification;
    public static PBEParameterSpec PBEParamSpec;
    public static final byte[] Salt = {(byte)0xc7, (byte)0x73,
        (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99};
    public static final int Count = 20;
   
    public static void InitiateCipher(int mode, String idString)
    {
        try
        {
            PBEParamSpec = new PBEParameterSpec(Salt, Count);
            PBEKeySpecification = new PBEKeySpec(idString.toCharArray());
            SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            PBEKey = keyFac.generateSecret(PBEKeySpecification);
            PBECipher = Cipher.getInstance("PBEWithMD5AndDES");
            SecurityAgent.PBECipher.init(mode, SecurityAgent.PBEKey, SecurityAgent.PBEParamSpec);
        }
        catch(Exception e) {}
    }
   
    public static void oneWayHash(InputStream incoming, OutputStream outgoing,
        String idString, boolean isEncrypting)
    {
        try
        {
            if(isEncrypting)
            {
                InitiateCipher(Cipher.ENCRYPT_MODE, idString);
                InputStream In = new BufferedInputStream(incoming);
                CipherOutputStream Out = new CipherOutputStream(outgoing, SecurityAgent.PBECipher);
                int i;
                while ((i = In.read()) != -1)
                    Out.write(i);
                Out.flush();
                Out.close();
                In.close();
            }
            else
            {
                InitiateCipher(Cipher.DECRYPT_MODE, idString);
               
                CipherInputStream In = new CipherInputStream(incoming, SecurityAgent.PBECipher);
               
                OutputStream Out = new BufferedOutputStream(outgoing);
                int i;
                while ((i = In.read()) != -1)
                    Out.write(i);
                Out.flush();
                Out.close();
                In.close();
            }
        }
        catch(Exception e) {}
    }
}

The testing program:



import java.security.*;
import java.io.*;

// testing code
public class Tester {
   
   
    public static void main(String[] args) throws Exception
   
    {
       
        // Testing in put password
        String password = "myPassword";
       
       
        // Create the encrypted password
        ByteArrayInputStream password_clear = new ByteArrayInputStream(password.getBytes());
        ByteArrayOutputStream password_encrypted = new ByteArrayOutputStream();
        SecurityAgent.oneWayHash(password_clear, password_encrypted, "myIdString", true);
       
        // Decode the encrypted password
        ByteArrayInputStream password_recrypted =
        new ByteArrayInputStream(password_encrypted.toString().getBytes());
        ByteArrayOutputStream password_reclear = new ByteArrayOutputStream();
        SecurityAgent.oneWayHash(password_recrypted, password_reclear, "myIdString", false);
       
       
       
        // Print out results
        System.out.println("Clear password=" + password);
        System.out.println("Encrypted password=" + password_encrypted.toString());
        System.out.println("Decrypted value of Encrypted password=" + password_reclear.toString());
       
    } // end main
   
} // end class

Any help is appreciated!
0
Comment
Question by:NGInterface
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 12529702
>        catch(Exception e) {}

if you ignore exceptions it makes it hard to know the reason

        catch(Exception e) { e.printStackTrace(); }

Probable reason is that the security provider is not available to your linux jre


0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 12529708
>>does not work

Meaning?

>>catch(Exception e) {}

Ignoring exception won't help diagnosis ;-)
0
 

Author Comment

by:NGInterface
ID: 12607198
Geez, never mind...the problem went away by itself :-P
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12608793
8-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question