Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Encrypt and Decrypt password

MSCDec 9 2009 — edited Dec 10 2009
Hi ,

I have this pices of code , The encrypt method is fine but i am looking to write the Decrypt method , Any body can help me and give me some refrence .

The following code is test and work fine .
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//import org.myorg.SystemUnavailableException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public final class PasswordService {

    private static PasswordService instance;

    public PasswordService() {
    }

    public static synchronized PasswordService getInstance() //step 1
    {
        if (instance == null) {
            instance = new PasswordService();
        }
        return instance;
    }

    public synchronized String encrypt(String plaintext) throws Exception {

        MessageDigest md = null;

        try {
            md = MessageDigest.getInstance("SHA"); //step 2
        } catch (NoSuchAlgorithmException e) {
            throw new Exception(e.getMessage());
        }

        try {
            md.update(plaintext.getBytes("UTF-8")); //step 3
        } catch (UnsupportedEncodingException e) {
            throw new Exception(e.getMessage());
        }
        byte raw[] = md.digest(); //step 4
        String hash = (new BASE64Encoder()).encode(raw); //step 5
        return hash; //step 6
    }
     public synchronized String decryption(String plaintext) throws Exception {
        // I need to write decryption code here
        return null;
    }

    public static void main(String a[]) throws Exception {
        PasswordService ps = new PasswordService();
        System.out.println(ps.encrypt("Ali Jamali"));
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2010
Added on Dec 9 2009
18 comments
989 views