Two Laptop Bag*
The moose likes Beginning Java and the fly likes Encrypting and Decrypting Strings. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Encrypting and Decrypting Strings." Watch "Encrypting and Decrypting Strings." New topic
Author

Encrypting and Decrypting Strings.

Vinod Kumar
Greenhorn

Joined: Jan 22, 2002
Posts: 10
Hello,
I am brand new to cryptography, and cannot find user friendly tutorials anywhere. Anyway, I have a class that exposes two methods for Encryption and Decryption and here is the the partial code
// "BC" is the name of the BouncyCastle provider
keyGen = KeyGenerator.getInstance("DES", "BC");
keyGen.init(new SecureRandom());
key = keyGen.generateKey();

encrypt = Cipher.getInstance("DES", "BC");
public ByteArrayOutputStream EncryptIt(String text) throws InvalidKeyException, IOException
{
encrypt.init(Cipher.ENCRYPT_MODE, key);
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
CipherOutputStream cipherOutput = new CipherOutputStream(byteArray, encrypt);

String out;
cipherOutput.write(text.getBytes());
cipherOutput.close();
return byteArray;
}
That's the code. And anyway when I write a program that uses this class to encrypt and decrypt it works fine, except for one problem, the encryption is random, it's not always the same text that is resulting from the encryption. How can I change that.
And the second thing, what is a key, and how do I specify my own key, or password so that it only decrypts if I use this password?
And, lastly, how can I write the encrypted data to a file?
One last question, what is RSA algorithm, and how is it different from DES, which leads me to another point. What are Symmetric and Asymmetric Algorithms?
I know I've asked a lot of questions, I hope someone can answer them.
Thanks, Vinu.
[ February 09, 2002: Message edited by: Vinu Murugesan ]
Rene Marot
Greenhorn

Joined: Jun 27, 2001
Posts: 9
Hello
Just one answer
Symetric means you use the same key to crypt en read your text.
Assymetric means you use one kay for crypting and a different one for reading.
Bye.
tarun matai
Greenhorn

Joined: Dec 08, 2001
Posts: 14
>>the encryption is random, it's not always the
>>same text that is resulting from the encryption.
This is true if you use a different key which you are doing as you are generating the key by a random number the encryption will always be different. As the underlined cipher will use the blocks of the key material and do operations on the data and produce the cipher text.

>> How can I change that.
if you use a fixed key (constant symmetric key)you can change that
>>what is a key
to be in simple its the lock and key mechanism as you have the key and you can lock and use same key to unlock. same is the case with cipher class for eg if your cipher class is instantiated for DES in CBC mode the key size would be for e.g. 64 bytes the data will be broken into the chunks of 64 bytes and various reversible operations will be carried out with the data and the key to produce un intelligible data called cipher text.
and since it is reversible same steps are done in reverse order to get the original text back from the cipher text with the help of a key.
>>how do I specify my own key or password so that >>it only decrypts if I use this password?
you can use PBE password based encryption to generate the key which has 3 parameters
1) salt (random byte[8] )
2) iteration count any number
3) password.
if these parameters are constant one can gnerate the key which is constant and hence the cipher text using the cipher class.

>>how can I write the encrypted data to a file?
suerly you can get the cipher data in a byte[] by calling Cipher.doFinal(originaldata[]);
and you can write this data to the fila by opening file out put stream.

what is RSA algorithm, and how is it different from DES, which leads me to another point.
>>RSA algorithm is used for asymmetric key encryption while DES is used for symmetric key encryption.
>>What are Symmetric and Asymmetric Algorithms?
Symmetric key is used whith the Symmetric key algorithm e.g. DES where same key is used for encryption and decryption.
while asymmetric key involves two key pairs which are complementary to each other one is called private key and the other the public key.
public key is known to all but the private key as the name suggests is private to a user. if one encrypts the data with public key only private key can decrypt the data and vice versa.
for e.g.
sender A encrypts data for reciver B using B's public Key
then only B can open the data that too with his Private key.
you should be very clear with the mechanism and note the difference between symmetric key and the asymmeric keys.

I hope this satisfies your queries


SCJP
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Encrypting and Decrypting Strings.
 
Similar Threads
How to encrypt & decrypt Random DES key with DES?
How to create SecretKey for AES 128 Encryption based on user's password??
Caesar cipher
JCE problem URGENT HELP Needed!!!
encrypt data in database