Archive for the ‘Java’ Category
Password Encryption Using SHA1 (MD5) - JAVA
Here’s is a program where the password stored in the code is encoded by SHA1, It accepts the input from user computes the SHA1 digest and check if it is the same as the set Password. I have set the Password to - password
import java.io.Console;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA1Pwd {
public static void main(String[] args) {
char[] passwordChar = null;
Console console = System.console();
MessageDigest md = null;
String encodedSetPwdInHexString = “5:BA:A6:1E:4C:9B:93:F3:F0:68:22:50:B6:CF:83:31:B7:EE:68:FD:8″;
//digest value of the string - password. change it to your required password digest.
try {
md = MessageDigest.getInstance(”SHA1″); // can be replaced with MD5
// SHA1 has fewer collisions in comparison with MD5.
Simple Password Encryption Technique - Java
Here’s a simple Password Encryption technique using hashCode(), It is just a basic style that does not expose your password in the source code. The Password I have set is - password
import java.io.Console;
public class pwd {
public static void main(String[] args) {
char[] passwordChar = null;
Console console = System.console();
int hashInt = 1216985755; // value of the string “password”
try {
passwordChar = console.readPassword(”Enter Password : “);
} catch (NullPointerException e) {
System.out.println(”You Might Be Running The Program From An IDE , Try In Terminal”);
System.exit(1);
}
String passwordString = new String(passwordChar);
System.out.println(”The Hash value of the Entered String : ”
+ passwordString.hashCode());
System.out.println(”The Hash Value Required : ” + hashInt);
if (passwordString.hashCode() != hashInt) { // hash coded check
System.out.println(”Wrong password”);
System.exit(2);
}
System.out.println(”Correct Password”);
}
}
There are better ways to do it than hashcode() method which is fragile, md5 or sha1 would be better techniques. The ONE TIME PADDING or encrypting the password using a secret key and storing it, but just for the program access I dont think it is that necessary, but the latter techniques will be useful while transmitting messages.
Password Masking in CLI using Java
Java CLI password masking was appended to Java 6. Wonder why it took them so long to add it. It comes with the Console Class. This class has several features for the ones developing CLI programs. I always hated the method to mask the letters with another thread.
Here is a sample,
import java.io.Console;
class pass {
public static void main (String[] args) {
char passwd[];
Console cons = System.console();
passwd = cons.readPassword(" Enter Your Password : ");
System.out.println("The Password is : " +passwd);
}
}
Here is the Documentation Link http://java.sun.com/javase/6/docs/api/java/io/Console.html.
In my next post I’ll describe a simple Encryption and Decryption of this password.

