Wednesday, September 16, 2015

Caesar Cipher

In cryptography, a Caesar cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plain text is replaced by a letter some fixed number of positions down the alphabet. The method is named after Julius Caesar, who used it in his private correspondence.

This is implemented in many traditional crypto such as One time cipher and vigeneer cipher.

For example
a right shift of 3, D would be replaced by G, E would become H, and so on. It also can be done right or left shift

Sample of left shift 3
Input:  THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Output: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD

Sample of right shift 23
Input:    ABCDEFGHIJKLMNOPQRSTUVWXYZ
Output:   XYZABCDEFGHIJKLMNOPQRSTUVW

Sample of mix plain text
Input: The quIck Brown foX JumpeD oVer tHe laZy DoG
Output: Ftq cgUow Ndaiz raJ VgybqP aHqd fTq xmLk PaS


Figure: example of left shift 3 step

Sample input
Programming Code For Life. Think Twice Code Once.
5

Samples Output
ORIGINAL STRING: Programming Code For Life. Think Twice Code Once.

RIGHT SHIFT: Uwtlwfrrnsl Htij Ktw Qnkj. Ymnsp Ybnhj Htij Tshj.
LEFT SHIFT: Kmjbmvhhdib Xjyz Ajm Gdaz. Ocdif Ordxz Xjyz Jixz.


Now Lets Decrypt

1. With key..
In Caesar cipher has only 2 ways of shift either left or right. Then use the shift key to shift left and right to get correct message.

2. Without key.
Caesar cipher is the easiest cipher attack can decrypt message quickly and correctly. shifting in alphabet has only 26 letter. What if i repeat 26 time of shifting either left or right and it give 26 possible message.? Try it..


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.*;
import java.lang.*;
import java.math.*;

public class Cipher {
    public static void main(String[] args) {
 
        String str = "Programming Code For Life. Think Twice Code Once.";
        int shift = 5;

        System.out.println("ORIGINAL STRING: "+str);
        
        String r_encode_str = rightEncode(str,shift);
        System.out.println("\nRIGHT SHIFT ENCRYPT: "+r_encode_str);
        
        String l_decode_str = leftDecode(r_encode_str,shift);
        System.out.println("DECRYPT: "+l_decode_str);
        
        String l_encode_str = leftEncode(str,shift);
        System.out.println("\nLEFT SHIFT ENCRYPT: "+l_encode_str);  
        
        String r_decode_str = rightDecode(l_encode_str,shift);
        System.out.println("DECRYPT: "+r_decode_str);
        
        
        System.out.println("\nDECRYPTION WITHOUT KEY. ASSUME MAX KEY IS 26");
        System.out.println("-LEFT SHIFT DECRYPT FIRST-");
        for(int x=1;x<26;x++){
            System.out.println(x+". "+leftDecode(r_encode_str,x));
        }
        System.out.println("\n-RIGHT SHIFT DECRYPT-");
        for(int x=1;x<26;x++){
            System.out.println(x+". "+rightDecode(l_encode_str,x));
        }
                
    }
 
    public static String leftEncode(String enc, int offset) {
        return rightEncode(enc, 26-offset);
    }
 
    public static String rightEncode(String enc, int offset) {
        offset = offset % 26 + 26;
        StringBuilder encoded = new StringBuilder();
        
        for (char i : enc.toCharArray()) {
            if (Character.isLetter(i)) {
                if (Character.isUpperCase(i)) {
                    encoded.append((char) ('A' + (i - 'A' + offset) % 26 ));
                } else {
                    encoded.append((char) ('a' + (i - 'a' + offset) % 26 ));
                }
            } else {
                encoded.append(i);
            }
        }
        return encoded.toString();
    }
    
     public static String leftDecode(String enc,int offset){
         return rightDecode(enc, 26-offset);
     }
     
     public static String rightDecode(String enc,int offset){
         offset = offset % 26 + 26;
         StringBuilder encoded = new StringBuilder();
         
         for (char i : enc.toCharArray()) {
             if (Character.isLetter(i)) {
                 if (Character.isUpperCase(i)) {
                     encoded.append((char) ('A' + (i - 'A' + offset) % 26 ));
                 } else {
                     encoded.append((char) ('a' + (i - 'a' + offset) % 26 ));
                 }
             } else {
                 encoded.append(i);
             }
         }
         return encoded.toString(); 
     }
}

No comments:

Post a Comment