Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2334 | Replies: 8
![]() |
•
•
Join Date: Sep 2005
Location: london
Posts: 16
Reputation:
Rep Power: 4
Solved Threads: 1
Hi everyone, i am trying to use some java code to encrypt passwords entering a database on the system i am building, please could someone explain what the code below is doing and if it would be useful for encrypting passwords. Thanks
import java.util.StringTokenizer;
public class Encryption {
public static String encrypt(String password) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i<password.length(); i++) {
char c = password.charAt(i);
int j = c;
sb.append(String.valueOf(j) + " ");
}
return sb.toString().trim();
}
public static String decrypt(String encryptedPassword) {
StringTokenizer st = new StringTokenizer(encryptedPassword, " ");
//A StringTokenizer takes a string as input and breaks it upto tokens, seperated by " "
StringBuffer sb = new StringBuffer();
while (st.hasMoreTokens()) {
int c = Integer.parseInt(st.nextToken());
char chr = (char) c;
sb.append(chr);
}
return sb.toString();
}
} Last edited by cscgal : Apr 24th, 2006 at 11:46 am.
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
I don't see where you're encrypting it... You get the char value from the int value, which is from the same char value, so you're really doing nothing there. You need to get the ascii value and add least add something to it.
>if it would be useful for encrypting passwords?
What, for real databases? Probably not, the encryption system looks weak.
Isn't there some java API for this anyway?
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
What, for real databases? Probably not, the encryption system looks weak.
Isn't there some java API for this anyway?
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
•
•
Join Date: Sep 2005
Location: london
Posts: 16
Reputation:
Rep Power: 4
Solved Threads: 1
•
•
•
•
Originally Posted by server_crash
I don't see where you're encrypting it... You get the char value from the int value, which is from the same char value, so you're really doing nothing there. You need to get the ascii value and add least add something to it.
Do you know any kind of java class i can use to encrypt passwords goin into a Mysql database.
Perhaps this?
http://java.sun.com/j2se/1.4.2/docs/...ryptoSpec.html
If you're going to do this yourself, I'd plump for xor encryption. So long as the key is sufficiently long and randomised.
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
http://java.sun.com/j2se/1.4.2/docs/...ryptoSpec.html
If you're going to do this yourself, I'd plump for xor encryption. So long as the key is sufficiently long and randomised.
http://img476.imageshack.us/img476/5171/cut20ln.png
Piworld ™
[Tis simple as Pie]
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
Java has a crypto and security package. It will do everything for you, but probably the same amount of time will be taken because you'll need to learn it.
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,703
Reputation:
Rep Power: 12
Solved Threads: 320
Here is alink to 1.5 version if you want http://java.sun.com/j2se/1.5.0/docs/...ryptoSpec.html
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
•
•
Join Date: Aug 2005
Location: Socialist Republic of Boulder
Posts: 216
Reputation:
Rep Power: 4
Solved Threads: 6
I wouldn't recommend using java to do any encryption to the database. DBs have their own encryption built in. Just add the DBs encryption on your jdbc call.
UPDATE users SET password = AES_ENCRYPT(`users password`, `your encryption key` WHERE id=`101`;
This makes it so you don't have to have encryption in each and every java application that touches the database. Much nicer in my opinion.
The above example is for a mysql database.
UPDATE users SET password = AES_ENCRYPT(`users password`, `your encryption key` WHERE id=`101`;
This makes it so you don't have to have encryption in each and every java application that touches the database. Much nicer in my opinion.
The above example is for a mysql database.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode