IMPLEMENTATION OF SHA-1 ALGORITHM using JAVA

Image
AIM: To   write a java program to calculate the message digest of a text using the SHA-1 algorithm ALGORITHM: Step 1: Input the plain text Step 2: Append padding bits Step 3: Append length Step 4:Initialize   buffer. Step 5: Process message in 512-bit (16word)blocks Step 6: Encrypted in hexa decimal format Step 7: Display the encrypted text. PROGRAM: importjava.security.*; public class SHA1 { public static void main(String[] a) { try { MessageDigest md = MessageDigest.getInstance("SHA1"); System.out.println("Message digest object info: "); System.out.println(" Algorithm = " +md.getAlgorithm()); System.out.println(" Provider = " +md.getProvider()); System.out.println(" ToString = " +md.toString());   String input = ""; md.update(input.getBytes()); byte[] output = md.digest(); System.out.println(); System.out.println("SHA1(\""+...

IMPLEMENTATION OF DES using java


AIM:

Towrite a program  toimplement DES  encryption and decryption in java.



ALGORITHM:

Step 1:Enter the plain text and key

Step 2: Initial permutation (IP) will be done for the plain text and key.

Step 3: 16 rounds of a complex key dependent calculation f

Function f  is

L(i) = R(i-1)

R(i) = L(i-1) Å P(S( E(R(i-1)) Å K(i) ))

Step 4: A final permutation, being the inverse of IP will be done to get cipher text.

Step 5:  The above steps  4, 3, 2 are repeated in reverse order to get decrypted text.





PROGRAM:

import java.util.*;

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

importjava.security.spec.KeySpec;

importjavax.crypto.Cipher;

importjavax.crypto.SecretKey;

importjavax.crypto.SecretKeyFactory;

importjavax.crypto.spec.DESedeKeySpec;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class DES {

private static final String UNICODE_FORMAT = "UTF8";

public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";

private KeySpecmyKeySpec;

private SecretKeyFactorymySecretKeyFactory;

private Cipher cipher;

byte[] keyAsBytes;

private String myEncryptionKey;

private String myEncryptionScheme;

SecretKey key;

staticBufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));

public DES() throws Exception {

myEncryptionKey = "ThisIsSecretEncryptionKey";

myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;

keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);

myKeySpec = new DESedeKeySpec(keyAsBytes);

mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);

cipher = Cipher.getInstance(myEncryptionScheme);

key = mySecretKeyFactory.generateSecret(myKeySpec);

 }

public String encrypt(String unencryptedString) {

 String encryptedString = null;

try {

cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);

byte[] encryptedText = cipher.doFinal(plainText);

 BASE64Encoder base64encoder = new BASE64Encoder();

encryptedString = base64encoder.encode(encryptedText); }

catch (Exception e) {

e.printStackTrace(); }

returnencryptedString; }

public String decrypt(String encryptedString) {

 String decryptedText=null;



try {

cipher.init(Cipher.DECRYPT_MODE, key);

 BASE64Decoder base64decoder = new BASE64Decoder();

byte[] encryptedText = base64decoder.decodeBuffer(encryptedString);

byte[] plainText = cipher.doFinal(encryptedText);

decryptedText= bytes2String(plainText); }

catch (Exception e) {

e.printStackTrace(); }

returndecryptedText; }

private static String bytes2String(byte[] bytes) {

StringBufferstringBuffer = new StringBuffer();

for (int i = 0; i <bytes.length; i++) {

stringBuffer.append((char) bytes[i]); }

returnstringBuffer.toString(); }

public static void main(String args []) throws Exception {

System.out.print("Enter the string: ");

 DES myEncryptor= new DES();



String stringToEncrypt = br.readLine();

 String encrypted = myEncryptor.encrypt(stringToEncrypt);

String decrypted = myEncryptor.decrypt(encrypted);

System.out.println("\nString To Encrypt: " +stringToEncrypt);

System.out.println("\nEncrypted Value : " +encrypted);

System.out.println("\nDecrypted Value : " +decrypted);

System.out.println("");

 }

}

Comments

Popular posts from this blog

IMPLEMENTATION OF SHA-1 ALGORITHM using JAVA

IMPLEMENTATION OF RSA ALGORITHM using c++

IMPLEMENTATION OF DIFFIE HELLMAN KEY EXCHANGE ALGORITHM