944,147 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4192
  • Java RSS
Apr 23rd, 2006
0

encryption

Expand Post »
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

Java Syntax (Toggle Plain Text)
  1. import java.util.StringTokenizer;
  2.  
  3. public class Encryption {
  4.  
  5.  
  6. public static String encrypt(String password) {
  7. StringBuffer sb = new StringBuffer();
  8.  
  9. for (int i = 0; i<password.length(); i++) {
  10.  
  11. char c = password.charAt(i);
  12. int j = c;
  13. sb.append(String.valueOf(j) + " ");
  14. }
  15. return sb.toString().trim();
  16. }
  17.  
  18.  
  19. public static String decrypt(String encryptedPassword) {
  20. StringTokenizer st = new StringTokenizer(encryptedPassword, " ");
  21. //A StringTokenizer takes a string as input and breaks it upto tokens, seperated by " "
  22. StringBuffer sb = new StringBuffer();
  23. while (st.hasMoreTokens()) {
  24. int c = Integer.parseInt(st.nextToken());
  25. char chr = (char) c;
  26. sb.append(chr);
  27. }
  28. return sb.toString();
  29. }
  30. }
Last edited by cscgal; Apr 24th, 2006 at 11:46 am.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bondito is offline Offline
17 posts
since Sep 2005
Apr 24th, 2006
0

Re: encryption

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 24th, 2006
0

Re: encryption

>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]
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 24th, 2006
0

Re: encryption

Quote 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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bondito is offline Offline
17 posts
since Sep 2005
Apr 24th, 2006
0

Re: encryption

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]
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 24th, 2006
0

Re: encryption

There's always MD5, if you never have to recover the readable password again.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Apr 24th, 2006
0

Re: encryption

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 30th, 2006
0

Re: encryption

Here is alink to 1.5 version if you want http://java.sun.com/j2se/1.5.0/docs/...ryptoSpec.html
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Apr 30th, 2006
0

Re: encryption

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.
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java multithreading
Next Thread in Java Forum Timeline: Converting Java to C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC