Link to home
Start Free TrialLog in
Avatar of Raftor
Raftor

asked on

encrypt password

Is it possibe to encrypt a password before sending it to a mysql database. If so how would u do so?  Would it be possible to use assembler or what would work with jsp?
Cheers, Raftor
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

I would use SSL

http://java.sun.com/webservices/docs/1.0/tutorial/doc/WebAppSecurity6.html

Then the browser will encrypt anything sent to the server, and the server will encrypt responses...

If you are just looking to encrypt the password in the database, then MD5 is what you want:

http://javaalmanac.com/egs/java.security/Digest.html
SOLUTION
Avatar of nishit4all
nishit4all

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Raftor
Raftor

ASKER

Thats very good ill try that and come back to you with any questions later.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Raftor

ASKER

That site looks Objects very good but i can get a java file with the example that uses the class to compile. i am importing the
javax.crypto classes but what am i leaving out?
Avatar of Raftor

ASKER

Its says to generate a temporary key.  How do i do this?
what errors are you getting?
Avatar of Raftor

ASKER


C:\Project>javac EncDec.java
EncDec.java:15: cannot resolve symbol
symbol  : class DesEncrypter
location: class Project.EncDec
                DesEncrypter encrypter = new DesEncrypter(key);
                ^
EncDec.java:15: cannot resolve symbol
symbol  : class DesEncrypter
location: class Project.EncDec
                DesEncrypter encrypter = new DesEncrypter(key);
                                                            ^
These are the errors but the other file DesEncrypter compiles fine!
Avatar of Raftor

ASKER

import javax.crypto.*;

public class EncDec{
      public static void main (String[] args)      {
            try{
              SecretKey key = KeyGenerator.getInstance("DES").generateKey();
              DesEncrypter encrypter = new DesEncrypter(key);
              String encrypted = encrypter.encrypt("Don't tell anybody!");
              String decrypted = encrypter.decrypt(encrypted);
          } catch (Exception e) {        }
    }
}
this is EncDec.java
Add the DesEncrypter class to your Project package.
Avatar of Raftor

ASKER

Damn that still didnt work. This is the exact code i am using. Any more suggestions?

////////////////////////////////////EncDec.java/////////////////////////
package Project;
import javax.crypto.*;

public class EncDec{
      public static void main (String args[])      {
            try      {
              SecretKey key = KeyGenerator.getInstance("DES").generateKey();
              // Create encrypter/decrypter class
              DesEncrypter encrypter = new DesEncrypter(key);

              // Encrypt
              String encrypted = encrypter.encrypt("Don't tell anybody!");

              // Decrypt
              String decrypted = encrypter.decrypt(encrypted);
          } catch (Exception e) {        }
    }
}

////////////////////////////////////////////DesEncrypter.java/////////////////////
package Project;
import javax.crypto.*;

public class DesEncrypter {
        Cipher ecipher;
        Cipher dcipher;
   
        DesEncrypter(SecretKey key) {
            try {
                ecipher = Cipher.getInstance("DES");
                dcipher = Cipher.getInstance("DES");
                ecipher.init(Cipher.ENCRYPT_MODE, key);
                dcipher.init(Cipher.DECRYPT_MODE, key);
   
            } catch (javax.crypto.NoSuchPaddingException e) {
            } catch (java.security.NoSuchAlgorithmException e) {
            } catch (java.security.InvalidKeyException e) {
            }
        }
   
        public String encrypt(String str) {
            try {
                // Encode the string into bytes using utf-8
                byte[] utf8 = str.getBytes("UTF8");
   
                // Encrypt
                byte[] enc = ecipher.doFinal(utf8);
   
                // Encode bytes to base64 to get a string
                return new sun.misc.BASE64Encoder().encode(enc);
            } catch (javax.crypto.BadPaddingException e) {
            } catch (IllegalBlockSizeException e) {
            } catch (java.io.IOException e) {
            }
            return null;
        }
   
        public String decrypt(String str) {
            try {
                // Decode base64 to get bytes
                byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
   
                // Decrypt
                byte[] utf8 = dcipher.doFinal(dec);
   
                // Decode using utf-8
                return new String(utf8, "UTF8");
            } catch (javax.crypto.BadPaddingException e) {
            } catch (IllegalBlockSizeException e) {
            } catch (java.io.IOException e) {
            }
            return null;
        }
    }
are both java files in same directory (Project)
what errors now?
Avatar of Raftor

ASKER

Ya they are in the same directory and im still getting the same errors,

C:\Project>javac EncDec.java
EncDec.java:15: cannot resolve symbol
symbol  : class DesEncrypter
location: class Project.EncDec
                DesEncrypter encrypter = new DesEncrypter(key);
                ^
EncDec.java:15: cannot resolve symbol
symbol  : class DesEncrypter
location: class Project.EncDec
                DesEncrypter encrypter = new DesEncrypter(key);
                                                              ^
is the parent directory of Project in your classpath?
Avatar of Raftor

ASKER

Uhh ohhh
Avatar of Raftor

ASKER

Thats it alright, sometimes when i spend too long looking at a problem i cant see the wood from the trees.
Thanks Experts!!
Raftor.
Just to ask why you are using DES?  That would mean that passwords can be decrypted, which isn't usually what you want...

That's why I suggested MD5

*shrug*

Oh well :)

Glad you got it sorted :)
Also,  nishit4all's Assisted answer would mean the encryption method is in the HTML source for the page!!?!  And it is not a recognised secure method of encryption...
glad u got the sol'n Raftor,
    would like to tell TimYates, that javascript code can be converted to java code and then can be implemented. But still it wouln't have been that secured, well that was just a small encryption tech in which cracking password wont be that easy.
thanks,
nishit