Java Cryptography

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 !

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

posted Dec 12, 2002 11:28:00

0

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

posted Dec 14, 2002 09:48:00

0

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

posted Dec 18, 2002 18:49:00

0

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 !

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