• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Cryptography

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually the encrypted bytes are encoded with base64 encoding to avoid the problem of special characters.
 
Eduardo Mathias
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Eduardo Mathias
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic