Showing posts with label Encryption. Show all posts
Showing posts with label Encryption. Show all posts

Monday, September 1, 2014

Encrypting and Decrypting using Public Private Key in Java

Let's see how we can encrypt and decrypt information in Java using Public and Private Key. This tutorial is done in Java 8 so you may not find Base64 encoding API's in older version of Java. You can replace them with apache commons library. To generate public and private key follow the tutorial here. 

Let's assume we have public and private keystore sitting at E:/temp directory. First let's read the private key store and get the private key out of it. You need to know the password of keystore and key. I am assuming lalit123 for both.


// Private Keystore
FileInputStream privateKeyFile = new FileInputStream(
"E:/temp/lalit_private_keystore");

Generating Private and Public Key using keytool in Java

In this post. let's see how we can generate private and public key using keytool command line interface which comes bundled with Java. The various options of keytool can be seen with following command

keytool -help

If the command does not works, make sure you have Java Installed and the bin directory is in the path.

First let's generate the private key. The key is kept in a keystore. The yellow highlighted text should be replaced with your information.. Also I am using RSA as the key algorithm and the keys size as 1024. You can change them as well.

Friday, July 25, 2014

Encryption in Java

Java provides cryptographic hash function to encrypt. Let's look a way to encrypt a text using SHA-1 algorithm. By just changing the encryption algorithm, we can generate different kind of hash.

//Any implementation of Java need to provide following algorithms
// MD5
// SHA-1
// SHA-256
String encryptionAlgorithm = "SHA-1";

try {   
 MessageDigest md = MessageDigest.getInstance(encryptionAlgorithm);
 
 String clearPassword = "password123";
 
 md.reset();
 md.update(clearPassword.getBytes("UTF-8"));

 byte[] digestedPassword = md.digest();
 String hexStr = "";
 for (int i = 0; i < digestedPassword.length; i++) {
  hexStr += Integer.toString

  ((digestedPassword[i] & 0xff) + 0x100, 16).substring(1);
 }
 
 //Print the encrypted password
 System.out.println(hexStr);
 
 // Many code snippets in web show the following way of generating
 // the hex
 // code. The right way is the above way and not the way below which
 // is
 // commented out. The following code generates hex code with
 // negative sign
 // for certain combination for example for "password123" . Still do
 // not know the mathematics behind it.
 // Will probe sometime in future, right now
 // has to fix the issue. :(
 //String encryptedPAssword = String.format("%x", new BigInteger(
 //  digestedPassword));
 //System.out.println(encryptedPAssword);

} catch (java.security.NoSuchAlgorithmException e) {
 System.out.println("Rats, " + encryptionAlgorithm + " doesn't exist");
}

Adding the salt

To add the salt, change the following code, after defining a salt string

String salt = "TATASalt";
....
md.update((clearPassword+salt).getBytes("UTF-8"));