Advertisements
Home > Information Technology, programming, Security > Computer Security: Multi Transpose Encryption/ Decryption Method

Computer Security: Multi Transpose Encryption/ Decryption Method

To avoid unauthorized people from eavesdropping your message through out the network, a good method of encryption has to be implemented. One of these techniques called substitution, where a character is substituted with another caracter. A simple example is the Caesar cipher where a character is shifted to another character. Take a sentence “Meet me at the bar”, by shifting this letters 3 times, the sentence will be encrypted to: “Oggv og cv dct”. This technique provides a confusion to the hacker since the sentence become incomprehensible.

However, this technique is easily hacked by using brute force technique. Since alphabet letters are only 26 letters, you have n26 chance to decrypt the message. Not much a hassle for a today’s computer. Another technique to hack this is the statistic attack. The weakness of a language is that it is redundant. It doesn’t use all the letter distributedly and evenly, making it vulnerable to be analysed. In English, the letter “e” and “t” are often used in a sentence. You can make an intelligent guess on the encrypted message by counting the most used characters to be either “e” or “t”. There are other techniques to counter these problems such as Vigenère, Hill, or polyalphabetic caesar cipher. However, it still doesn’t solve the redundancies of the language.

In attempt to counter language redundancy, transposition technique might be one of the techniques to confuse the language sequence. Take the same example: “Meet me at the bar” and provide a key to encrypt the message: “53241″. The encryption process will be as follow:

Key         5        3          2          4         1
Plaintext   M        e          e          t         m
            e        a          t          t         h
            e        b          a          r
Ciphertext  mhetaeabttrmee

This technique will confuse the hacker from doing pattern analysing since the sequence of the letters has been jumbled, giving it an additional nCn possibility to decrypt it. The strength of this technique is it will be more confusing if you repeat this technique over and over again. The more you do transpotition technique, the more it will be confusing. The weakness, however, is if the key is hacked and successfully decrypted than it is doomed for the message to be eavesdropped or modified. More advance technique that is still in research is the one-time pad, but I will save this discussion for later, since it is brain-crunching. So, to get a hold of computer security subject, take a hold of this simple multi transpostion encryption/decryption algorithm:

Encryption

#include <cstdlib>
#include <conio.h>
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <string.h>

void sort(char a[] , int b[], int n);
void scan(char a[] , int b[], int n , int *done);
void swap(char *a ,char *b ,int *c, int *d);

int main(int argc, char *argv[])
{
   char keyword[1000], plaintext[1000];
   int keySequence[1000];
   int numTrans = 0;

   printf("Insert your plaintext : ");
   scanf("%s",plaintext);
   printf("\nInsert your keyword : ");
   scanf("%s",keyword);
   printf("\nHow many times will this be transposed? ");
   scanf("%d",&numTrans);

   strlwr(plaintext);

   if(strlen(keyword) > 0) {
      printf("Chipered Text:\n===========================\n");
      char keyReference[1000];
      strcpy(keyReference,keyword);
      for(int i = 0; i < strlen(keyReference); i++) {
           keySequence[i] = i;
      }

      sort(keyReference,keySequence, strlen(keyReference));

      //Chipering plaintext using transpose method
      char chiperText[1000];
      int num = 0;
      int repetition = 0;
      for(int x = 0; x < numTrans; x++) {
         for(int i = 0; i < strlen(keyReference); i++) {
           repetition = 0;
           while((keySequence[i]+(repetition*strlen(keyReference))) <
              strlen(plaintext)) {
              chiperText[num] = plaintext[(keySequence[i]+
                                  (repetition*strlen(keyReference)))];
              repetition++;
              num++;
           }
         }

         strcpy(plaintext,chiperText);
         num = 0;
         repetition = 0;
      }

      printf(chiperText);
      printf("\n===========================\n");
   }

   system("PAUSE");
   return EXIT_SUCCESS;
}

void sort(char a[1] , int b[1], int n) {
   int done;
   done = 1;
   while(done == 1) scan(a , b ,  n , &done);
}

void scan(char a[1] , int b[1], int n , int *done)   {
   int i;
   *done=0;
   for(i=0 ; i<n-1 ; ++i)  {
      if(a[i]>a[i+1]) {
        swap(&a[i],&a[i+1],&b[i],&b[i+1]);
        *done=1;
      }
   }
}

void swap(char *a ,char *b, int *c, int *d) {
   char temp;
   temp = *a;
   *a   = *b;
   *b   = temp;

   int temp2;
   temp2 = *c;
   *c   = *d;
   *d   = temp2;
}

Decryption

#include <conio.h>
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <string.h>

void sort(char a[] , int b[], int n);
void scan(char a[] , int b[], int n , int *done);
void swap(char *a ,char *b ,int *c, int *d);

int main(int argc, char *argv[])
{
    char keyword[1000], chipertext[1000];
    int keySequence[1000];
    int numTrans = 0;

    printf("Insert your chipertext : ");
    scanf("%s",chipertext);
    printf("\nInsert your keyword : ");
    scanf("%s",keyword);
    printf("\nHow many times will this be transposed? ");
    scanf("%d",&numTrans);

    strlwr(chipertext);

    if(strlen(keyword) > 0) {
      printf("Dechipered Text:\n===========================\n");
      char keyReference[1000];
      strcpy(keyReference,keyword);
      for(int i = 0; i < strlen(keyReference); i++) {
            keySequence[i] = i;
      }

      sort(keyReference,keySequence, strlen(keyReference));

      char plaintext[1000];
      int num = 0;
      int repetition = 0;
      for(int x = 0; x < numTrans; x++) {
            for(int i = 0; i < strlen(keyReference); i++) {
                    repetition = 0;
                    while((keySequence[i]+(repetition*strlen(keyReference))) <
                            strlen(chipertext)) {
                            plaintext[(keySequence[i]+
                               (repetition*strlen(keyReference)))] = 
                               chipertext[num];
                            repetition++;
                            num++;
                    }
            }

            strcpy(chipertext,plaintext);
            num = 0;
            repetition = 0;
      }  

      /*for(int i = 0; i < strlen(chipertext); i++) {
          printf("%c", plaintext[i]);
      }*/
      printf(plaintext);
      printf("\n===========================\n");

    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

void sort(char a[1] , int b[1], int n) {
     int done;
     done = 1;
     while(done == 1) scan(a , b ,  n , &done);
}

void scan(char a[1] , int b[1], int n , int *done)   {
     int i;
     *done=0;
     for(i=0 ; i<n-1 ; ++i)  {
        if(a[i]>a[i+1]) {
           swap(&a[i],&a[i+1],&b[i],&b[i+1]);
           *done=1;
        }
     }
}

void swap(char *a ,char *b, int *c, int *d) {
  char temp;
  temp = *a;
  *a   = *b;
  *b   = temp;

  int temp2;
  temp2 = *c;
  *c   = *d;
  *d   = temp2;

}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: