Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to encrypt a string

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic