Google Answers Logo
View Question
 
Q: Need a program to encrypt and decrypt a string in java. ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: Need a program to encrypt and decrypt a string in java.
Category: Computers > Programming
Asked by: jag1978-ga
List Price: $60.00
Posted: 30 Oct 2004 12:35 PDT
Expires: 29 Nov 2004 11:35 PST
Question ID: 422152
I need a program to encrypt a text string and decrypt a text string.
The input would also be a seed or a key or a pass string using which
the encryption should be performed. The decryption should also recieve
the same key to decrypt. Can use any of the standard api that comes
with jdk1.4. If using any other API must provide the api jar file.
Also any information regarding which algorithm used would be helpful.
Also if the length of the encrypted string can be controlled would
give a nice tip. Need this program by beginning of work week, if
delayed please contact me before working on the question. If provided
with a seed or key generator would be great.

Clarification of Question by jag1978-ga on 30 Oct 2004 12:36 PDT
The program should be a java program.

Clarification of Question by jag1978-ga on 30 Oct 2004 13:37 PDT
The encrypted string should be able to be serialized.

Request for Question Clarification by endo-ga on 30 Oct 2004 16:59 PDT
Hi,

Do you have any preference for a standard API or an external algorithm?

I don't think there are any secure algorithms that let you control the
outputted encrypted string (it also probably depends on the input).

Usually encryption uses two keys: a public one is used to encrypt and
the private one to decrypt. This is to avoid the problem of
"transmitting the password" securely.

Would such an implementation fit your needs?

Thanks.
endo

Clarification of Question by jag1978-ga on 30 Oct 2004 19:14 PDT
Yes, that would be fine. But can you tell me if I could use the same
set of encryption keys i.e. same encryption key over and over again
for a bunch of strings and same decryption key(that goes with this
encryption key) for decryting the strings that were encrypted. If this
is achievable please go ahead and solve it.
I would prefer the standard API. If an external algorithm is used I
would need the files i.e. the API files that go with it.

Thanks,
Jag.

Request for Question Clarification by endo-ga on 30 Oct 2004 19:23 PDT
Hi,

I'm working on it now, I'll get back to you in a couple of hours.

Thanks.
endo

Clarification of Question by jag1978-ga on 30 Oct 2004 19:46 PDT
I will check back in a couple of hours then.

Thanks,
Jag.
Answer  
Subject: Re: Need a program to encrypt and decrypt a string in java.
Answered By: endo-ga on 30 Oct 2004 19:53 PDT
Rated:4 out of 5 stars
 
Hi,

Thank you for your question.

Here is a solution built using link 1. The source code and class file
are linked at the bottom of the explanations.

This uses the same passphrase to encrypt and decrypt.
The packages used are included as standard with Java 1.4.

DesEncrypter is the class responsible for encoding and decoding.

The constructor sets up and configures the encoder/decoder.

The encrypt method encrypts the string passed as a parameter.
The decrypt method decrypts the string passed as a parameter.

The main function is an example of how it's used.

You first create a new instance of the DesEncrypter class with your
chosen password. You then use the encrypt method to encrypt your given
string and retrieve an encrypted string. At the other end you do the
same thing, except you use the decrypt function to get back your
original string.

If you require any more help, features or explanations, please let me
know before rating my answer by using the 'Request for Clarification'
button and I will be more than happy to help.

Thanks.
endo


Files:

Source code
ftp://dbky:googleanswers@ftp.membres.lycos.fr/crypto/DesEncrypter.java

Compiled class file
ftp://dbky:googleanswers@ftp.membres.lycos.fr/crypto/DesEncrypter.class



Search Strategy:

encryption java
://www.google.com/search?q=encryption+java



Links:

1) Encrypting with DES Using a Pass Phrase
http://javaalmanac.com/egs/javax.crypto/PassKey.html

2) Password-Based Cryptography Standard
http://mirror.switch.ch/ftp/doc/standard/pkcs/99workshop/pkcs5_v2.0.ppt

3) Public-Key Cryptography Standards
http://www.rsasecurity.com/rsalabs/node.asp?id=2124

Clarification of Answer by endo-ga on 30 Oct 2004 20:04 PDT
Here is some more information on the encryption algorithm, if you want
to do some reading:

Java Cryptography Extension
http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html#AppA

Class SecretKeyFactory
http://java.sun.com/j2se/1.4.2/docs/api/javax/crypto/SecretKeyFactory.html#getInstance(java.lang.String)

PKCS #5: Password-Based Cryptography Standard
http://www.rsasecurity.com/rsalabs/node.asp?id=2127



Thanks.
endo

Request for Answer Clarification by jag1978-ga on 30 Oct 2004 20:13 PDT
Cannot download program. Can you please confirm the link is working.

Thanks,
Jag.

Clarification of Answer by endo-ga on 30 Oct 2004 20:17 PDT
Hi,

The links work, but I've uploaded the files to another site, I hope
these work for you.

Source code
http://www.afrotechmods.com/files/endo/ga/DesEncrypter.java

Class file
http://www.afrotechmods.com/files/endo/ga/DesEncrypter.class

Please let me know if there are any problems.

Thanks.
endo

Request for Answer Clarification by jag1978-ga on 30 Oct 2004 20:24 PDT
Is there anyway you could provide me an example with a public key and
private key encryption just as u mentioned?

Thanks,
Jag.

Clarification of Answer by endo-ga on 30 Oct 2004 20:27 PDT
Hi,

Sure, just give me a little bit of time.

Thanks.
endo

Request for Answer Clarification by jag1978-ga on 30 Oct 2004 21:18 PDT
Endo,

    Can I get the answer tonight. Please let me know.

Thanks,
Jag.

Clarification of Answer by endo-ga on 30 Oct 2004 21:33 PDT
Hi,

Yes, I'm writing up the answer right now.

Just give me about 10 minutes please.

Thanks.
endo

Clarification of Answer by endo-ga on 30 Oct 2004 21:50 PDT
Hi,

This new version uses PGP and the PGP API.

PGP Java API Homepage
http://www.cryptography.ch/tiki-page?pageName=pgpjava

To start off, you need the following file:

PGP Java API
http://www.cryptography.ch/projects/pgpjava/pgpjava-1.0.zip

Once you download the file, please extract the following 4 files to
the directory where you want to use it:
pgpapi.jar , pgpi.dll , secring.pgp , pubring.pgp

Here is the source code for the example, based on the example on the webpage:

Source code
http://www.afrotechmods.com/files/endo/ga/pgp.java

PGP Java API - Example
http://www.cryptography.ch/tiki-page?pageName=pgpjava_example


To compile the example, put all the aforementioned files in the same directory.

You then need to set the classpath in the command prompt:
set classpath=%classpath%;c:\code\pgpapi.jar;./

Using the appropriate directory for pgpapi.jar.

Then type:

javac pgp.java -cp pgpapi.jar

To run the program type:

java pgp

with one of the following arguments:

1  generate key
This will udpate the files secring.pgp and pubring.pgp with the data
needed to encrypt and decrypt the files for the userid given in the
file with the supplied passphrase.

2  encrypt file
You then send the pubring.pgp file to the encryption side. You can
encrypt the file specified as the second argument provided you know
the userid. The result should create an encrypted file called
input.pgp. They will need an empty secring.pgp file as well for PGP to
work.

3  decrypt file
You need the correct secring.pgp file and the passphrase. Here you
supply the input.pgp file and the passphrase and you get the decrypted
file in input.clr

If you use the extractKey method, you can get the public key in a text
file, that you can then send to people to use manually with PGP.

If you need any help with this example, please let me know.

Thanks.
endo

PGPi JavaDoc
http://www.iam.unibe.ch/~jampen/pgpjava/PGPi.html

PGP
http://www.pgpi.org/

Request for Answer Clarification by jag1978-ga on 31 Oct 2004 06:06 PST
Endo,

         Thank you for answering with PGP encryption. But this doesn't
solve my purpose, I do not need to encrypt entire files in my case. I
only need to encrypt a string and the encrypted string should be able
to be serialized. Let me know if you can help otherwise I will give
you the rating.

Thanks,
Jag.

Clarification of Answer by endo-ga on 31 Oct 2004 12:53 PST
Hi,

Sorry for the late reply, I was out all day.

I'll have another look for something. I found private/public key
encryption but that was only for verifying the authenticity of data
rather than encryption.

I'll get back to you as soon as possible.

Thanks.
endo

Request for Answer Clarification by jag1978-ga on 31 Oct 2004 13:52 PST
Hi,

Thank you, I am waiting on it. 

Jag.

Clarification of Answer by endo-ga on 31 Oct 2004 14:27 PST
Hi,

I've got it ready, I'm typing up the answer now.

Thanks.
endo

Clarification of Answer by endo-ga on 31 Oct 2004 14:37 PST
Hi,

Here is another example using public/private key encryption using the
JCE and bouncycastle provider (which despite the comical name, seems
quite popular and cropped out a few times during my research).

To get you going you need the following files:

Bouncycastle Provider JAR file
http://www.bouncycastle.org/download/bcprov-jdk14-125.jar

Example file
http://www.afrotechmods.com/files/endo/ga/RSACryptoTest.java

The example file is based on the following tutorial:

JCE offers an API to leverage asymmetric cryptography
http://builder.com.com/5100-6370-1049434.html

The only difference is that I couldn't get the original code to work,
so I changed the line that reads, to explicitly define the provider:

c = Cipher.getInstance(ALGORITHM , "BC");


To compile and run the Java file you need the linked JAR file in your classpath.

The implementation and code are pretty straightforward.

The parts you are interested in are the generateMessage() function
that returns the message to encrypt and the RSACryptoTest()
constructor, which shows how to use the provided functions.

If you need any help understanding or using this example, please let me know.

Thanks.
endo



Links:

The Legion of the Bouncy Castle
http://www.bouncycastle.org/index.html



Search Strategy:

java private public key encryption
://www.google.com/search?q=java+private+public+key+encryption

Clarification of Answer by endo-ga on 02 Nov 2004 13:25 PST
Hi,

Did you have any trouble with the updated solution?

Thanks.
endo

Clarification of Answer by endo-ga on 07 Nov 2004 17:54 PST
Hi,

Did you have any problems with the last version of the code?

Thanks.
endo

Request for Answer Clarification by jag1978-ga on 08 Nov 2004 10:56 PST
Endo,

    Thanks a lot. It worked out fine for me. Actually I implemented
the first example you sent me. But I was looking for a better version
of DES i.e. AES Encryption. But still ... Its been of great help.

Jag.

Clarification of Answer by endo-ga on 08 Nov 2004 11:24 PST
Glad it worked out. Thanks for the tip.
endo
jag1978-ga rated this answer:4 out of 5 stars and gave an additional tip of: $5.00
Good job .. provided clarifications as much as i needed.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy