Showing posts with label encryption. Show all posts

A Password oneway encryption utility  

Posted by Kishore in

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.w3c.tools.codec.Base64FormatException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class PasswordUtil {
private static PasswordUtil instance = null;

public static PasswordUtil getInstance() {
if (instance == null) {
instance = new PasswordUtil();
}

return instance;
}

public synchronized String encrypt(String password) {
MessageDigest messageDigest = null;

try {
messageDigest = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

try {
messageDigest.update(password.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}

byte raw[] = messageDigest.digest();
String hash = (new BASE64Encoder()).encode(raw);
return hash;
}

public boolean verifyPassword(String passwordBytes, String passwordHash)
throws NoSuchAlgorithmException {
return encrypt(passwordBytes).equals(passwordHash);
}

}

Encryption  

Posted by Kishore in

The basic thing to follow while handling the sensitive information like password and credit card number, is to keep them encrypted everywhere in the program.

So here are some methods which gives proper encryption and decryption methods.

public static String encrypt(String msg) {
String ns = "2131232132132324234";
String es = "2132123213321321341241";
BigInteger n = new BigInteger(ns);
BigInteger e = new BigInteger(es);
BigInteger bi = new BigInteger(msg.getBytes());
BigInteger x = bi.modPow(e, n);
return (x.toString());
}

Do you really need a decryption method? I guess you may not. You can use the same for the reverse also. If still unclear, ask me...