Tuesday, December 15, 2015

Affine Cipher

Remember: 
Formula to encrypt : ax + b % 26
Formual to decrypt : IN * (x - b) mod 26


There are 2 key:
for example : 17 , 20

Text = TWENTYFIFTEEN

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
-------------------------------------------------------------------
0 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

To encrypt :

T  W  E  N  T  Y  F  I  F  T  E  E  N
19 22 4  13 19 24 5  8  5  19 4  4  13

By using formula encryption ax+b % 26.
a = first key
b = second key
x = is the each letter



--------------------------------------
T  W  E  N  T  Y  F  I  F  T  E  E  N
--------------------------------------
19 22 4  13 19 24 5  8  5  19 4  4  13
5  4  10 7  5  12 1  0  1  5  10 10 7   <== ax+b % 26
--------------------------------------
F  E  K  H  F  M  B  A  B  F  K  K  H 
--------------------------------------

To decrypt :
 
F  E  K  H  F  M  B  A  B  F  K  K  H 
5  4  10 7  5  12 1  0  1  5  10 10 7

By using formula decryption.
a = first key
b = second key
x = 0 - infiniti

by using first key, find the inverse modular which firstkey * x mod 26 must equal to 1.


17 * 0 mod 26 != 1
17 * 1 mod 26 != 1
.
.
.
17 * 23 mod 26 == 1  <--- 23 is the modular inverse

by using 23,
b = second key
x = is the each letter encrypted letter

23 *(x-b) mod 26
--------------------------------------
F  E  K  H  F  M  B  A  B  F  K  K  H 
--------------------------------------
5  4  10 7  5  12 1  0  1  5  10 10 7
19 22 4  13 19 24 5  8  5  19 4  4  13 <== 23 *(x-b) mod 26
--------------------------------------
T  W  E  N  T  Y  F  I  F  T  E  E  N
--------------------------------------


Done!

Here the source code


Change Snippet Background Color
          
            
import java.util.*;
import java.lang.*;
import java.math.*;

public class Test{
   public static void main(String[] args) {
       String input = "TWENTYFIFTEEN";
       int x = 17;
       int y = 20;
       String enc = encrypt(input,x,y);
       String dec = decrypt(enc,x,y);
       System.out.println("Input:     " + input);
       System.out.println("Decrypted: " + enc);
       System.out.println("Decrypted: " + dec);
   }
   
   public static String encrypt(String input,int FK,int SK) {
       String str = "";
       for (int in = 0; in < input.length(); in++) {
           char get = input.charAt(in);
           if (Character.isLetter(get)) {
               // ax + b % 26
               get = (char) ((FK * (int)(get + 'A') + SK) % 26 + 'A');
           }
           str +=get;
       }
       return str;
   }
   
   public static String decrypt(String input,int FK,int SK) {
       String str = "";
       int x = 0;
       int inverse = 0;
       
       // find 1 by using modular inverse
       // 17 * IN mod 26 == 1
       // IN is 0 - infiniti
       // if total == 1, then IN is the inverse modular
       while(true){
         inverse = FK * x % 26;
            if(inverse == 1)
               break;
         x++;
       }
       
       for (int in = 0; in < input.length(); in++) {
           char get = input.charAt(in);
           if (Character.isLetter(get)) {
               // IN *(x-b) mod 26
               get = (char)(x * ((get + 'A') - SK) % 26 + 'A');
           }
           str +=get;
       }
       return str;
   }
}

          
        

2 comments: