aspose file tools*
The moose likes JSP and the fly likes Java Cryptography Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Java Cryptography" Watch "Java Cryptography" New topic
Author

Java Cryptography

Eduardo Mathias
Greenhorn

Joined: Oct 24, 2002
Posts: 17
Hi !
Can anybody suggest me how to implement a simple encrypt / decrypt class using javax.crypto or java.security ? A need a very simple process and I want the generated encrypted string to have no special characters, such as "ESC" or "ENTER".
Thanx !
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Usually the encrypted bytes are encoded with base64 encoding to avoid the problem of special characters.
Eduardo Mathias
Greenhorn

Joined: Oct 24, 2002
Posts: 17
And how can I do it ?
How can I set this encode ?
That's my code:
public static String getEncrypted(String password)
{
String encrypted = "";
try
{
byte[] encrypt = password.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(encrypt);
encrypted = new String(md.digest());
return encrypted;
}
catch(Exception e)
{
System.out.println("Unexpected error while encrypting.");
System.out.println(e.toString());
return null;
}
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
First of all, if you want to be able of decrypt your string, you can not use a digest, you need a symetric algorithm(blowfish, DES,...).
Here you have 2 methods for encrypt/decrypt.
I use blowfish and as william said, bytes are base64 encoded(using the sun package, you may use any other..).

HTH
Juanjo
Eduardo Mathias
Greenhorn

Joined: Oct 24, 2002
Posts: 17
Thanx a lot Juanjo !
But it didn't solved my problem.
The encrypted string generated has special characters, and this means a lot of trouble to me, as I have to insert this into the database and worry about enconding and other boring stuff. Is there a way to "limit" the encrypt output, then it will only have alphanumeric "regular" characters ?
Thanx in advance !
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
OK. Take a look at this method:

... you can see how to use the base64 encoder/decoder.
Once you encrypt your string, base64-encode it.
You will get a base64 encoded string you can insert in a database without problems.
BTW I wrote it fast, I hope theres no typos...
HTH
[ December 16, 2002: Message edited by: Juanjo Bazan ]
Eduardo Mathias
Greenhorn

Joined: Oct 24, 2002
Posts: 17
Thanx Juanjo !
But now I have another problem !
I've tested your code with J2SDK 1.4.0 and it worked very nice !
But I'm developing a project with Oracle 9iAS, J2EE and EJB. When I use your solution in this project, i get the "No Such Algorithm" exception !
How can I specify the correct provider ?
What can be wrong ?
Thanx in advance !
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
All that you need to do is register your providers under the java.security fil, found in jdk/jre/lib/security.
Existing string will be
security.provider.1=sun.security.provider.Sun
security.provider.2=com.sun.rsajca.Provider
add your provider, say,
security.provider.1=providerpkg.blah.blah.YourProvider
and change the priority for other as 2 and 3. Keeping your provider 1.
Have the required jars and classes in the classpath and you shant get the error, if your provider supports the alogrithm, it will work.
Lupo


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
Eduardo Mathias
Greenhorn

Joined: Oct 24, 2002
Posts: 17
Thanx for the tipe, but it's still not working.
Take a look ate this slice of my code :

What's wrong ?
Any help is welcome !
Eduardo Mathias
Greenhorn

Joined: Oct 24, 2002
Posts: 17
Finally !
I solved the problem !!!
I had to grant the correct permissions in the policy file !
Now it's working very well !
Thanx a lot for the help !!!
Eduardo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Cryptography