Thursday, October 8, 2015

Kamasutra Cipher

One of the earliest descriptions of encryption by substitution appears in the Kama-sutra, a text written in the 4th century AD by the Brahmin scholar Vatsyayana, but based on manuscripts dating back to the 4th century BC. The Kama-sutra recommends that women should study 64 arts, including cooking, dressing, massage and the preparation of perfumes. The list also includes some less obvious arts, including conjuring, chess, bookbinding and carpentry. Number 45 on the list is mlecchita-vikalpa, the art of secret writing, advocated in order to help women conceal the details of their liaisons. One of the recommended techniques involves randomly pairing letters of the alphabet, and then substituting each letter in the original message with its partner.4

How it work
The kamasutra generate list of 26 alphabet with no duplicate. Then divide by 2 row. Find for each letter of message text in table and choose the opposite of the letter

for example:
Key = G H A J R I O B E S Q C L F V Z T Y K M X W N U D P

divide by 2 rows

G H A J R I O B E S Q C L
F V Z T Y K M X W N U D P

Given String = KAMASUTRA

K is at 2nd row and 5th column. Get the opposite of K that is I. Do each letter until the end

Cipher : IZOZNQJYZ

To decyrpt the cipher, use the table with same random list of 26 alphabet. Without the key is difficult to decrypt because of  26! = 403,291,461,000,000,000,000,000,000 = 403 septilion of combination of key can be produce uniquely.




 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
/*
   authors by hafiq
   
   Kamasutra Cipher and decrytion
      
   any expert can advise me to improve this code...tq..
*/
import java.util.*;

public class KSCipher{
   public static void main(String[] args){
      String text = "KAMA SUTRA CIPHER";
      String key  = RandomAlphaNoDuplicate(26);
      
      String enc = KSutra(text,key);
      System.out.println("Encryption: "+ enc);
      String dec = KSutra(enc,key);
      System.out.println("Decryption: "+ dec);
   }
   
   public static String RandomAlphaNoDuplicate(int len){
      Random r = new Random();
      String key = "";
      for (int i = 0; i < len;) {
          char c = (char) (r.nextInt(26) + 'A');
          if(!key.toString().contains(""+c)){
             key = key + c;
             i++;
          }
      }      
      return key;
   }
   
   public static String KSutra(String text,String key){
      int keyLen = key.length()/2;
      
      // arrange random key
      char[][] keyRow = new char[2][keyLen];
      int count=0;
      for(int x=0;x<2;x++){
         for(int y=0;y<keyLen;y++){
            keyRow[x][y] = key.charAt(count);
            System.out.print(keyRow[x][y]+" ");
            count++;
         }
         System.out.println();
      }
      
      String sb = "";
      
      count=0;
      for(int x=0;x<text.length();x++){
         for(int y=0;y<2;y++){
            for(int z=0;z<keyLen;z++){
               if(y == 0){
                  if(text.charAt(x) == keyRow[y][z])
                     sb += keyRow[y+1][z];
               }
               else if (y == 1){
                  if(text.charAt(x) == keyRow[y][z])
                     sb += keyRow[y-1][z];
               }
            }
         }
         if(text.charAt(x) == ' ')
            sb += text.charAt(x);
      }
      
      return sb;
   }
}

No comments:

Post a Comment