This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to encrypt a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to encrypt a string" Watch "How to encrypt a string" New topic
Author

How to encrypt a string

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hi,
I need to encyrpt a string(a password string). I dont want to use any complex algorithms for this. Any simple algorith will do.
One more thing. Does the method String.hashCode() generate a unique integer?. I mean will the generated hashCode be the same for some other string too???
Regards
Saj
Rene Marot
Greenhorn

Joined: Jun 27, 2001
Posts: 9
The simpliest way I know to emcrypt is to make an exclusive or on the char code with an arbitrary long char string.
XOR table
True xor true => false
true xor false => true
false xor true => true
fals xor false => false
string_1 xor entrypting_string = encrypt_string
encrypt_sting xor encrypting_string = string_1
Don't know how to write this in Java but I hope it will help.
This is not a very strong encryptation because the key is allways the same en write in the code but for simple purpose it's enought.
Bye.
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
By definition a hash code does not guarantee uniqueness.
Equal strings will generate equal hash codes but equal hash codes does not guarantee equal strings.
From the String class in the API the HashCode() is calculated
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
I haven't worked it out but I imagine there are plenty of different strings with the same hash code.
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Originally posted by Rene Marot:
The simpliest way I know to emcrypt is to make an exclusive or on the char code with an arbitrary long char string.
....
This is not a very strong encryptation because the key is allways the same en write in the code but for simple purpose it's enought.

Actually, this is the strongest encryption of encryption we know of, and is known as a one-time-pad. The catch is, first to use a randomly generated string of bits with which to encrypt, and second, to never resuse this encryption string.
--Mark
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to encrypt a string
 
Similar Threads
key with combinations
Ordering Lists of Lists (Table) by more than one column
Resolving built-in entities
Unable to send mail to multiple receipients....
How to find a particular substring in a string without using any builtin methods of string