• 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

Encrypting and Decrypting Strings.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>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
 
this is supposed to be a surprise, but it smells like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic