Showing posts with label Cryptography. Show all posts
Showing posts with label Cryptography. Show all posts

Friday, January 6, 2017

How to Generate GPG Public / Private Key Pair (RSA / DSA / ElGamal)?

Written by Pranshu Bajpai |  | LinkedIn


This post is meant to simplify the procedure for generating GNUPG keys on a Linux machine. In the example below, I am generating a 4096 bit RSA public private key pair.

Step 1. Initiate the generation process

#gpg --gen-key
 This initiates the generation process. You have to answer some questions to configure the needed key size and your details. For example, select from several kinds of keys available. If you do not know which one you need, the default 1 will do fine.

I usually select my key size to be 4096 bits which is quite strong. You can do the same or select a lower bit size. Next, select an expiration date for your key -- I chose 'never'.



Step 2. Generate entropy


The program needs entropy, also known as randomness, to generate the keys. For this you need to type on the keyboard or move the mouse pointer or use disk. However, you may still have to wait a while before the keys are generated.


For this reason, I use rng-tools to generate randomness. First install 'rng-tools' by typing:
#apt-get install rng-tools
Run the tool: 
#rngd -r /dev/urandom
The process of finding entropy should now conclude faster. On my system, it was almost instantaneous.



Step 3. Check ~/.gnupg to locate the keys

Once the keys are generated, they are usually stored in ~/.gnupg, a hidden gnupg directory in the home folder. You can check the location of keys by typing:

#gpg -k
The key fingerprint can be obtained by:
   #gpg --fingerprint

Step 4. Export the public key to be shared with others


For others to be able to communicate with you, you need to share you public key. So move to the ~/.gnupg folder and export the public key:

#gpg --armor --export email@host.com > pub_key.asc
'ls' should now show you a new file in the folder called 'pub_key.asc'. 'cat' will show you that this is the public key file.



Important !

Needless to say, do not share your private key with anyone.

Sunday, March 16, 2014

Affine Cipher Encryption Decryption Source code in Java

Written by Pranshu Bajpai |  | LinkedIn

Encryption

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;
import java.io.*;
/**
 *
 * @author Pranshu
 */
public class DICAss19_AffineCipherEnc {
    public static void main(String[] args) {
        String Msg;
        String CTxt = "";
        
        int a = 5;
        int b = 8;
        int m = 26;
        
        BufferedReader ObjIn = new BufferedReader(
                new InputStreamReader(System.in));
        try{
            System.out.println("Enter your Message(in Caps and without spaces):");
            Msg = ObjIn.readLine();
        
            //ENCRYPTION...
            for (int i = 0 ; i < Msg.length() ; i++){
                CTxt = CTxt + (char)(((a * Msg.charAt(i) + b) % m) + 65);                
            }
            
            System.out.println("Cipher Text :"+ CTxt);
            
            //ENTERING CIPHER TEXT INTO A FILE...
            DICAss0_FileHandling ObjFilHand = new DICAss0_FileHandling();
            ObjFilHand.FileWriteFunc("BealeCipher_CTxt.txt", CTxt);
        }
        catch(Exception err){
            System.err.print("Error: " + err);
        }
    }         
}

Decryption

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;

/**
 *
 * @author Pranshu
 */
public class DICAss20_AffineCipherDec {
    public static void main(String[] args) {
        String Msg = "";
        String CTxt;
        
        int a = 5;
        int b = 8;
        int m = 26;
        int a_inv = 0;
        int flag = 0 ;

        //Finding a_inverse...
        for (int i = 0 ; i < 26 ; i++){
            flag = (a * i) % 26;
            if (flag == 1){
                a_inv = i;
            }
        }
        
        try{
            DICAss0_FileHandling ObjFileHand = new DICAss0_FileHandling();
            CTxt = ObjFileHand.FileReadFunc("BealeCipher_CTxt.txt");
            System.out.println("CIPHER TEXT IS :"+ CTxt);
            
        
            //DECRYPTION...
            for (int i = 0 ; i < CTxt.length() ; i++){
                Msg = Msg + (char)(((a_inv * (CTxt.charAt(i) - b)) % 26) + 65);
            }
            
            System.out.println("Original Message :"+ Msg);
        }
        catch(Exception err){
            System.err.print("Error: " + err);
        }
    }         
}

Vignere Cipher Encryption Decryption Source code in Java

Written by Pranshu Bajpai |  | LinkedIn

Encryption



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;

/**
 *
 * @author Pranshu
 */
public class DICAss14_VignereEnc {
    public static void main(String[] args){
        String Msg = "IAMUNDERATTACK";
        //String Msg = "vaibs";
        String Key = "CRYPTO";
        
        String CTxt = "";
        
        int MsgLen = Msg.length();
        int KeyLen = Key.length();
        
        int i;
        
        //CREATING FINAL KEY TO ENCRYPT THE MESSAGE!!!
        String KeyFinal = "";
        if( MsgLen < KeyLen){
            for( i = 0 ; i < MsgLen ; i++){
                KeyFinal = KeyFinal + Key.charAt(i);
            }
            //System.out.println(KeyFinal);
        }
        else{
            int Div = MsgLen / KeyLen;
            int Rem = MsgLen % KeyLen;
            
            for (i = 0 ; i < Div ; i++){
                KeyFinal = KeyFinal + Key;
            }
            
            for( i = 0 ; i < Rem ; i++){
                KeyFinal = KeyFinal + Key.charAt(i);
            }
            //System.out.println(KeyFinal);
        }
        
        System.out.println("Msg : " + Msg);
        System.out.println("Key : " + KeyFinal);
        
        //ENCRYPTION...
        int temp;
        char c;
        int sum;
        for(i = 0 ; i < MsgLen; i++){
            sum = Msg.charAt(i) + KeyFinal.charAt(i);
            if (sum >= 155 ){
                temp = sum - 90;
            }
            else {
                temp = sum - 64;
            }
                       
            c = (char) temp;
            CTxt = CTxt + c;
        }   
        System.out.println("CiP : " + CTxt);
    }
}

Decryption


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;

/**
 *
 * @author Pranshu
 */
public class DICAss15_VignereDec {
    public static void main(String[] args){
        String Msg = "";
        //String Msg = "vaibs";
        String Key = "CRYPTO";
        
        String CTxt = "LSLKHSHJZJNPFC";
        
        int CTxtLen = CTxt.length();
        int KeyLen = Key.length();

        //GENERATING FINAL KEY TO DECRYPT CIPHER TEXT!!!
        int i;
        String KeyFinal = "";
        if( CTxtLen < KeyLen){
            for( i = 0 ; i < CTxtLen ; i++){
                KeyFinal = KeyFinal + Key.charAt(i);
            }
            //System.out.println(KeyFinal);
        }
        else{
            int Div = CTxtLen / KeyLen;
            int Rem = CTxtLen % KeyLen;
            
            for (i = 0 ; i < Div ; i++){
                KeyFinal = KeyFinal + Key;
            }
            
            for( i = 0 ; i < Rem ; i++){
                KeyFinal = KeyFinal + Key.charAt(i);
            }
            //System.out.println(KeyFinal);
        }
        
        System.out.println("CiP : " + CTxt);
        System.out.println("Key : " + KeyFinal);
        
        //DECRYPTION...
        int temp;
        char c;
        int sum;
        
        for(i = 0 ; i < CTxtLen; i++){
            sum = CTxt.charAt(i) - KeyFinal.charAt(i);
            if (sum >= 0 ){
                temp = sum + 64;
            }
            else {
                temp = sum + 90;
            }
                       
            c = (char) temp;
            Msg = Msg + c;
        }   
        System.out.println("Msg : " + Msg);
    }
}

One Time Pad Encryption Decryption Source code in Java

Written by Pranshu Bajpai |  | LinkedIn

Encryption

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;
import java.util.Random;
import java.io.*;
/**
 *
 * @author Pranshu
 */
public class DICAss17_OneTimePadEnc {
    public static void main(String[] args) {
        try{
            String Msg ;
            String Key = "";
            String CTxt = "";
            
            //taking message from user
            BufferedReader ObjIn  = new BufferedReader(
                    new InputStreamReader(System.in));
            System.out.print("Enter the Message(Without any space and in Block Letters): \n");
            Msg = ObjIn.readLine();
            
            //to Generate random no. between 0-25 and Generating Key
            Random randomGenerator = new Random();
            for (int idx = 1; idx <= Msg.length(); ++idx){
              int randomInt = randomGenerator.nextInt(26);
              Key = Key + (char)(65+randomInt);
            }
            System.out.println("Key: \n" + Key);
            
            //ENCRYPTION...
            int temp;
            char c;
            int sum;
            for(int i = 0 ; i < Msg.length(); i++){
                sum = Msg.charAt(i) + Key.charAt(i);

                if (sum >= 155 ){
                    temp = sum - 90;
                }
                else {
                    temp = sum - 64;
                }

                c = (char) temp;
                CTxt = CTxt + c;
            }   
            System.out.println("CiP : " + CTxt);
            
            //CREATING FILES FOR THE KEY & CIPHER TEXT... FOR TRANSMISSION!!!
            DICAss0_FileHandling ObjFileHand = new DICAss0_FileHandling();            
            ObjFileHand.FileWriteFunc("OTP_Key.txt", Key);
            ObjFileHand.FileWriteFunc("OTP_CipherText.txt", CTxt);
        }
        catch(Exception err){
            System.err.println("Error: " + err);
        }
    }
}

Decryption


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;

/**
 *
 * @author Pranshu
 */
public class DICAss18_OneTimePadDec {
    public static void main(String[] args){
        String Msg = "";
        String Key;        
        String CTxt;
        
        DICAss0_FileHandling ObjFileHand = new DICAss0_FileHandling();
        Key = ObjFileHand.FileReadFunc("OTP_Key.txt");
        CTxt = ObjFileHand.FileReadFunc("OTP_CipherText.txt");
                
        //DECRYPTION...
        int temp;
        char c;
        int sum;
        
        for(int i = 0 ; i < CTxt.length(); i++){
            sum = CTxt.charAt(i) - Key.charAt(i);
            if (sum >= 0 ){
                temp = sum + 64;
            }
            else {
                temp = sum + 90;
            }
                       
            c = (char) temp;
            Msg = Msg + c;
        }   
        System.out.println("Msg : " + Msg);
    }
}

Character Frequency Analysis in Java | Source Code | Simple Cryptanalysis

Written by Pranshu Bajpai |  | LinkedIn

Source Code


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Pranshu
 */

public class DICAss16_CharFrequency {

    
    public static void main(String[] args) throws FileNotFoundException, IOException {
    
        File f = new File("test.txt");
        
        Scanner input = new Scanner(System.in);
        BufferedWriter w = new BufferedWriter(new FileWriter(f));
                    
        System.out.println("Enter text in book");
        
        w.write(input.nextLine());
        
        w.flush();
        w.close();
        
        BufferedReader r = new BufferedReader(new FileReader(f));
        
        String str;
        String alphabets = "abcdefghijklmnopqrstuvwxyz";
        String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String char1 = "";
        String char2 = "";
        String char3 = "";
        String char4 = "";
        
        str = r.readLine();
        System.out.println(str);
        
        char ch;
        char ch1;
        char ch2;
        char ch3 = 0;
        float[] arr = new float[26];
        int k;
        float len;
        
        // for single character frequency...
        for(int i=0; i < alphabets.length(); i++) {
            ch = alphabets.charAt(i);
            ch1 = alpha.charAt(i);
            k = 0;
            for(int j=0; j < str.length(); j++) {
                if((ch == str.charAt(j))||(ch1 == str.charAt(j))) {
                    k++;
                }
            }
        
            System.out.println("frequency of " + alphabets.charAt(i) + "= " + k);
            len = str.length();
            arr[i] = k / len;
           
        }
        
        System.out.println("________________________________________________");
        
        for(int i = 0; i < alphabets.length(); i++) {
            System.out.println("Probability of " + alphabets.charAt(i) + " = " + arr[i]);
        }
        
        // for  character pair frequency...
        System.out.println("________________________________________________");
        
        for(int i = 0; i < 26; i++) {
            ch = (char) alphabets.charAt(i);
            char1 = "";
            char1 = char1 + ch;
            for(int j = 0; j < 26; j++) {
                ch1 = (char) alphabets.charAt(j);
                char2 = "";
                char2 = char2 + ch1;
            
                 k = 0;
                for(int l = 0; l < str.length() - 1; l++) {
                    if((ch == str.charAt(l) && ch1 == str.charAt(l+1))) {
                        k++;
                    }
                        
                }
            
                System.out.println("Frequency for " + char1 + char2 + "= "+k);
            }
        }
        
        System.out.println("________________________________________________");
        
        //for character triplet frequency...
        for(int i = 0; i < 26; i++) {
            ch = (char) alphabets.charAt(i);
            char1 = "";
            char1 = char1 + ch;
            for(int j = 0; j < 26; j++) {
                ch1 = (char) alphabets.charAt(j);
                char2 = "";
                char2 = char2 + ch1;
       
                for(int x = 0; x < 26; x++) {
                    ch2 = (char) alphabets.charAt(x);
                    char3 = "";
                    char3 = char3 + ch2;
                    k = 0;
            
                    for(int l = 0; l < str.length() - 2; l++) {
                        if((ch == str.charAt(l) && ch1 == str.charAt(l+1) 
                                && ch2 == str.charAt(l+2))) {
                            k++;
                        }
                        
                    }
                    
                    System.out.println("for " + char1 + char2 + char3 + "= "+k);                
                }
            }
        }
        
        System.out.println("________________________________________________");
        
        // for character quadruplet frequency...
        for(int i = 0; i < 26; i++) {
            ch = (char) alphabets.charAt(i);
            char1 = "";
            char1 = char1 + ch;
        
            for(int j = 0; j < 26; j++) {
                ch1 = (char) alphabets.charAt(j);
                char2 = "";
                char2 = char2 + ch1;
               
                for(int x = 0; x < 26; x++) {
                    ch2 = (char) alphabets.charAt(x);
                    char3 = "";
                    char3 = char3 + ch2;
               
                    for(int y = 0; y < 26; y++) {
                        ch3 = (char) alphabets.charAt(y);
                        char4 = "";
                        char4 = char4 + ch3;  
                    }
                    
                    k = 0;
                    for(int l = 0; l < str.length() - 3; l++) {
                        if((ch == str.charAt(l) && 
                            ch1 == str.charAt(l+1) && 
                            ch2 == str.charAt(l+2) && 
                            ch3 == str.charAt(l+3))) {
                                k++;
                        }
                    }
                    System.out.println("for " + char1 + char2 + char3 + char4 + "= "+k);                
                }
            }
        }
    }
}

XOR Encryption Decryption Java Source Code

Written by Pranshu Bajpai |  | LinkedIn

Encryption


package DICAss;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Pranshu
 */
public class DICAss10_XOREncryption {
    public static void main(String[] args){
        String Msg = "I AM UNDER ATTACK!";
        char Key = 'k';
        String CTxt = "";
        int xor;
        char temp;
        for(int i = 0 ; i < Msg.length() ; i++){
            xor = Msg.charAt(i) ^ Key;
            temp = (char)xor;
            CTxt = CTxt + temp;
        }        
        
        System.out.println(CTxt);
    }
}

Decryption



package DICAss;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Pranshu
 */
public class DICAss11_XORDecryption {
    public static void main(String[] args){
        String CTxt = "\"K*&K>%/.9K*??*( J";
        char Key = 'k';
        String Msg = "";
        int xor;
        char temp;
        for(int i = 0 ; i < CTxt.length() ; i++){
            xor = CTxt.charAt(i) ^ Key;
            temp = (char)xor;
            Msg = Msg + temp;
        }        
        
        System.out.println(Msg);
    }
}