encryption in java

The SHA hash functions are a set of cryptographic hash functions designed by the National Security Agency (NSA) and published by the NIST as a U.S. Federal Information Processing Standard.
SHA stands for Secure Hash Algorithm.
The three SHA algorithms are structured differently and are distinguished as SHA-0, SHA-1, and SHA-2. The SHA-2 family uses an identical algorithm with a variable digest size which is distinguished as SHA-224, SHA-256, SHA-384, and SHA-512. 

SHA-1: The Secure Hash Algorithm (SHA) was developed by NIST and is specified in the Secure Hash Standard (SHS, FIPS 180). SHA-1 is a revision to this version and was published in 1994. It is also described in the ANSI X9.30 (part 2) standard. SHA-1 produces a 160-bit (20 byte) message digest. Although slower than MD5, this larger digest size makes it stronger against brute force attacks.


MD5: MD5 was developed by Professor Ronald L. Rivest in 1994. Its 128 bit (16 byte) message digest makes it a faster implementation than SHA-1.

Data Encryption using SHA - 512


import java.security.MessageDigest;
import java.math.BigInteger.*;
public class Encryption
{

public static String Password(String data)
    {
    StringBuffer sb = new StringBuffer();
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
        messageDigest.update(data.getBytes("UTF-8"));
        byte[] digestBytes = messageDigest.digest();

        String hex = null;
        for (int i = 0; i < digestBytes.length; i++)
            {
            hex = Integer.toHexString(0xFF & digestBytes[i]);
            if (hex.length() < 2)
            sb.append("0");
            sb.append(hex);
            }
        String psw = sb.toString();
        }
    catch (Exception ex)
        {
        System.out.println(ex.getMessage());
        }
    return new String(sb);
    }
public static void main(String[] args)
    {
    Encryption e = new Encryption();
    String s1 = e.Password("ankit");
    System.out.println("Encrypted String is "+s1);
    }
}






Data Encryption using MD5

import java.math.*;
import java.security.*;
import java.security.MessageDigest;
class encrypt1
{
    public static void main(String[] arr)
    {
        try {
      String toEnc = "ankit"; // Value to encrypt
      MessageDigest mdEnc = MessageDigest.getInstance("MD5"); // Encryption algorithm
      mdEnc.update(toEnc.getBytes(), 0, toEnc.length());
      String md5 = new BigInteger(1, mdEnc.digest()).toString(16); // Encrypted string
      System.out.println(md5);
        }
        catch (Exception ex)
        {
        System.out.println(ex.getMessage());
        }
    }
}
 



Comments

Popular posts from this blog

ubuntu package installation

Drupal Bootstrap Database

How to fix 500 internal privoxy error