Thursday, December 15, 2016

Java and python codes for caesar cipher, encryption and decryption

Python 
def c_enc(str_,shift):
base=ord('A')
end=ord('Z')
cipher=''
for ch in str_.upper():
if not ch.isalpha():
cipher += ch
else:
shifted_pos=ord(ch)+shift
if shifted_pos > end:
cipher += chr(shifted_pos % end + base - 1)
else:
cipher += chr(shifted_pos)
return cipher
def c_dec(str_,shift):
base=ord('A')
end=ord('Z')
plain=''
for ch in str_.upper():
if not ch.isalpha():
plain += ch
else:
orig_pos=ord(ch)-shift
if orig_pos < base:
plain += chr(end-(base - orig_pos)+1 )
else:
plain += chr(orig_pos)
return plain
cipher_txt=c_enc("ABCDEFGHIJKLMNOPQRSTUVWXYZ",3)
plain_txt=c_dec(cipher_txt,3)
print(plain_txt,"->",cipher_txt)

Java

I've implemented the following class for caeser cipher
class Caeser
{
    char[] alphabs={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'
                    ,'p','q','r','s','t','u','v','w','x','y','z'};
    char[] key=new char[26];
    Caeser(int shift)
    {
        setKey(shift);
    }
    private void setKey(int shift)
    {
       for(int i=0;i
       {
           key[i]=alphabs[(i+shift)%26];
       }
    }
    public void printKey()
    {
        System.out.println("\n\nThe Caeser cipher key is :");
        for (int i=0;i
        {
            System.out.print(alphabs[i]);
            System.out.print(" ");
        }
        System.out.println();
        for (int i=0;i
        {
            System.out.print(key[i]);
            System.out.print(" ");
        }
    }
    public char[] cipherText(char[] txt)
    {
        char[] temp=new char[txt.length];
        for(int i=0;i
        {
            if(Character.isUpperCase(txt[i]))
                temp[i]=key[txt[i]-'A'];
            else if(Character.isLowerCase(txt[i]))
                temp[i]=key[txt[i]-'a'];
            else
                temp[i]=txt[i];
        }
        
        return temp;
    }
    public char[] decipherTextWithoutKey(char[] txt)
    {
        //the frequency of English letters , 'e' letter is the most
        //frequently used letter. 
        char[] letterRanks={'e','a','r','i','o','t','n','s','l','c','u','d','p'};
        //the frequency of the letters in the provided text - Not sorted
        int[] cnts=charCount(String.valueOf(txt));
        //the frequency of the letters in the provided text - Sorted
        char[] kChars=sortChars(cnts);
        
        return letterRanks;
    }
    private char[] sortChars(int[] array)
    {
        //sorted by using the selection sort
        int temp=0;
        char tempc;
        char[] sortedChars=new char[26];
        //copy the contents of the alphabets into the sorted array
        System.arraycopy(alphabs, 0, sortedChars, 0, alphabs.length);
        for(int i=0;i
        {
            for (int j=i+1;j
            {
                if(array[j]>array[i])
                {
                    //swap the counts of the two characters
                    temp=array[i];
                    array[i]=array[j];
                    array[j]=temp;
                    //swap the two characters themself
                    tempc=sortedChars[i];
                    sortedChars[i]=sortedChars[j];
                    sortedChars[j]=tempc;
                }
            }
        }    
         return sortedChars;   
    }
    private int[] charCount(String s)
    {
        //we put an array of counts each cell contains the counts of each letter 
        //for exampel counts[0] contains the frequency of the letter 'a', counts[1]
        //contains the frequency of the letter 'b' and so on...
        int[] counts=new int[26];
        for (int i=0;i
            if(Character.isLetter(s.charAt(i)))
            {
                if(Character.isLowerCase(s.charAt(i)))
                    counts[s.charAt(i)-'a']++;
                else if(Character.isUpperCase(s.charAt(i)))
                    counts[s.charAt(i)-'A']++;
            }
        return counts;
    }

No comments:

Post a Comment