Saturday, 10 August 2013

Password Encryption

Encrypt Password in MD5,SHA,SHA-256 Format

if you want to encrypt password in different different format just you need to change accordingly in MessageDigest.getInstance('here is your Encryption Algorithm')


public String encryptPassword(){
MessageDigest md = MessageDigest.getInstance("MD5");
FileOutputStream fio = new FileOutputStream("c:\\loging.log");
fio.write(password.getBytes());
fio.close();
FileInputStream fis = new FileInputStream("c:\\loging.log");
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
;
byte[] mdbytes = md.digest();
// convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16)
.substring(1));
}
fio.close();
System.out.println(sb.toString());
return sb.toString();
}