cobol

cobol

Wednesday, April 8, 2015

 Java implement Data encryption standard (DES)

Program to implement Data encryption standard (DES)




import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.xml.bind.annotation.
adapters.HexBinaryAdapter;

public class DES {

  private Cipher cipher = null;
  private DESKeySpec keySpec = null;
  private SecretKeyFactory keyFactory = null;

  public String encrypt(String inputString,
        String commonKey) throws Exception {

    String encryptedValue = "";
    SecretKey key = getSecretKey(commonKey);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] inputBytes = inputString.getBytes();
    byte[] outputBytes = cipher.doFinal(inputBytes);
    encryptedValue = new HexBinaryAdapter().marshal(outputBytes);
    return encryptedValue;
  }

  public String decrypt(String encryptedString,
        String commonKey) throws Exception {
    String decryptedValue = "";
    encryptedString = encryptedString.replace(' ', '+');
    SecretKey key = getSecretKey(commonKey);
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] recoveredBytes = null;
    try {
      recoveredBytes =
          cipher.doFinal
            (new HexBinaryAdapter().unmarshal(encryptedString));
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  
    decryptedValue = new String(recoveredBytes);
    return decryptedValue;
  }

  private SecretKey getSecretKey(String secretPassword) {
    SecretKey key = null;
    try {
      cipher = Cipher.getInstance("DES");
      keySpec = new DESKeySpec
          (secretPassword.getBytes("UTF8"));
      keyFactory = SecretKeyFactory.getInstance("DES");
      key = keyFactory.generateSecret(keySpec);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println
        ("Error in generating the secret Key");
    }
    return key;
  }

  public static void main(String[] args) {
    BufferedReader reader;
    reader = new BufferedReader
        (new InputStreamReader(System.in));
    DES des = new DES();
  
    try {
      System.out.println
      ("ENCRYPTION --------------------------------");
      System.out.print("Enter Plain Message: ");
      String input = reader.readLine();
    
      System.out.print("Enter Key: ");
      String key = reader.readLine();
      System.out.println();
    
      System.out.print("Encrypted Message: ");
      String encrypted = des.encrypt(input, key);
      System.out.println(encrypted);
      System.out.println();
      System.out.println();
    
      System.out.println
      ("DECRYPTION --------------------------------");
      System.out.print("Enter Encrypted Message: ");
      encrypted = reader.readLine();
    
      System.out.print("Enter Key: ");
      key = reader.readLine();
      System.out.println();

      System.out.print("Decrypted Message: ");
      String decrypted = des.decrypt(encrypted, key);
      System.out.println(decrypted);
      System.out.println();
    
    } catch (Exception e) {
      e.printStackTrace();
    }
  
  }

}


Output:
ENCRYPTION-----------------------------------------------------------------------------------------
enter plain message: hello world (f.e)
Enter key: DonkeyKongCountry
 Encrypted message: 18FCD0F55D04602E564E35C1BC1BC1107A

DECRYPTION:---------------------------------------------------------------------------------------
Enter Encrypted Message:
18FCD0F55D04602E564E35C1BC1BC1107A
Enter Key: DonkeyKongCountry
Decrypted Message: Hello World

Friday, April 3, 2015

Java program to implement AES (Advanced encryption standard) algorithm.




import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AES {
  BufferedReader reader;
  final String IV = "AAAAAAAAAAAAAAAA";
  public static void main(String args[]) {
    AES aes = new AES();
    aes.go();
  }
  public AES() {
    reader = new BufferedReader
        (new InputStreamReader(System.in));
  }
  void go() {
    StringBuffer message = new StringBuffer();
    try {
      System.out.print("Enter Message: ");
      message.append(reader.readLine());
    
      while (message.length() % 16 != 0)
        message.append('\u0000');
    
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
  
    StringBuffer key = new StringBuffer();
    try {
      System.out.print("Enter Key: ");
      key.append(reader.readLine());
    
      while (key.length() % 16 != 0)
        key.append('\u0000');
    
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
  
    // Encryption
    byte[] cipher =
        encrypt(message.toString(), key.toString());
  
    System.out.print("Cipher: ");
    for (int i=0; i<cipher.length; i++)
      System.out.print((int)cipher[i]);
    System.out.println();
  
    // Invalid key
    //key.replace(0, 6, "ABCDEF");

  
    // Decryption
    String decrypted =
        decrypt(cipher, key.toString());
  
    System.out.println("Decrypted message: " + decrypted);
  }
  byte[] encrypt
    (String plain, String key) {
  
    byte[] encrypted = null;
  
    try {
      Cipher cipher =
          Cipher.getInstance
            ("AES/CBC/NoPadding", "SunJCE");
    
      SecretKeySpec sks =
          new SecretKeySpec(key.getBytes("UTF-8"), "AES");
    
      AlgorithmParameterSpec params;
      params = new IvParameterSpec(IV.getBytes("UTF-8"));
      cipher.init(Cipher.ENCRYPT_MODE, sks, params);
  
      encrypted = cipher.doFinal(plain.getBytes("UTF-8"));
    } catch (Exception e) {
      e.printStackTrace();
    }
  
    return encrypted;
  }
  String decrypt(byte[] ct, String key) {
    StringBuffer decrypted = new StringBuffer();
    try {
      Cipher cipher =
          Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    
      SecretKeySpec sks =
          new SecretKeySpec(key.getBytes("UTF-8"), "AES");
    
      AlgorithmParameterSpec params;
      params = new IvParameterSpec(IV.getBytes("
UTF-8"));
      cipher.init(Cipher.DECRYPT_MODE, sks, params);
    
      String s = new String(cipher.doFinal(ct), "UTF-8");
      decrypted.append(s);
    } catch (Exception e) {
      e.printStackTrace();
    }
  
    for (int i=decrypted.length()-1; i>0; i--) {
      if (decrypted.charAt(i) == '\u0000')
        decrypted.deleteCharAt(i);
      else break;
    }
  
    return decrypted.toString();
  }
}



***F.E , Output:

Enter message: " Obeanie went up Tora Bora and ended up in Abotttabad, It took Yankee Doodle Doo and Big Brother 10 years and a fortune to figure it out. The singleness of purpose is imperative and the use of NSA's Prism doubtful. ."
 Enter key:JackRabbitMoab308

Cipher: 3224-48-107-24-112-6134-1234771101-48-82114-87-84-3512-19-117507-103427011567-3698-698248-548625587-25-56-65-69-546-5875-77563219

Decrypted message: Obeanie went up Tora Bora and ended up in Abotttabad, It took Yankee Doodle Doo and Big Brother 10 years and a fortune to figure it out. The singleness of purpose is imperative and the use of NSA's Prism doubtful.