A program to encrypt or decrypt text using vigenere cipher technique

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 15774 Views - Last Post: 21 November 2011 - 02:32 AM Rate Topic: -----

#1 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

A program to encrypt or decrypt text using vigenere cipher technique

Posted 06 February 2007 - 03:12 AM

Description: Please do the validations like the string should be of limited size or only alphabets should be there, if you use this program. I was bit lazy to do that...This program uses vigenere cipher technique for encrypting and decrypting the text. This is one of the basic techniques used for ciphers
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <process.h>

void vigenereCipher(char *,char *);
void encipher();
void decipher();

int main()
{
     int choice;
     //loop takes choice from user and calles appropriate function
     while(1)
     {
          printf("n1. Encrypt Textn");
          printf("2. Decrypt Textn");
          printf("3. Exitn");
          printf("Enter Your Choice : ");
          scanf("%d",&choice);
          fflush(stdin);
          if(choice == 3) 
               exit(0);
          else if(choice == 1) 
               encipher();
          else if(choice == 2)
               decipher();
          else
               printf("Please Enter Valid Option.");
     }

     return 0;
}

void encipher()
{
     unsigned int i,j;
     char input[257],key[33];
     printf("Enter Text to be Encrypted [Max. 256 characters/ only alphabets]:n ");
     gets(input);
     printf("Enter Encryption Key [Max. 32 Characters/ only aphabets]: ");
     gets(key);
     for(i=0,j=0;i<strlen(input);i++,j++)
     {
          //repeat the key if you are at end of it.
          if(j>=strlen(key))
          {
               j=0;
          }
          //actual logic -> character from input + character from key % 26 is encrypted charater
          printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26));
     }

}

void decipher()
{
     unsigned int i,j;
     char input[257],key[33];
     int value;
     printf("Enter Text to be Decrypted [Max. 256 characters/ only alphabets]:n ");
     gets(input);
     printf("Enter Decryption Key [Max. 32 Characters/ only aphabets]: ");
     gets(key);
     for(i=0,j=0;i<strlen(input);i++,j++)
     {
          //repeat the key if you are at end of it.
          if(j>=strlen(key))
          {
               j=0;
          }
          //similar to encipher only difference is you need to subtract
          value = (toupper(input[i])-64)-(toupper(key[j])-64);
          //make positive if value is negative.
          if( value < 0)
          {
               value = value * -1;
          }          
          printf("%c",65 + (value % 26));
     }

}


Is This A Good Question/Topic? 0
  • +

Replies To: A program to encrypt or decrypt text using vigenere cipher technique

#2 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 06 February 2007 - 03:12 AM

Description: Please do the validations like the string should be of limited size or only alphabets should be there, if you use this program. I was bit lazy to do that...This program uses vigenere cipher technique for encrypting and decrypting the text. This is one of the basic techniques used for ciphers
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <process.h>

void vigenereCipher(char *,char *);
void encipher();
void decipher();

int main()
{
     int choice;
     //loop takes choice from user and calles appropriate function
     while(1)
     {
          printf("n1. Encrypt Textn");
          printf("2. Decrypt Textn");
          printf("3. Exitn");
          printf("Enter Your Choice : ");
          scanf("%d",&choice);
          fflush(stdin);
          if(choice == 3) 
               exit(0);
          else if(choice == 1) 
               encipher();
          else if(choice == 2)
               decipher();
          else
               printf("Please Enter Valid Option.");
     }

     return 0;
}

void encipher()
{
     unsigned int i,j;
     char input[257],key[33];
     printf("Enter Text to be Encrypted [Max. 256 characters/ only alphabets]:n ");
     gets(input);
     printf("Enter Encryption Key [Max. 32 Characters/ only aphabets]: ");
     gets(key);
     for(i=0,j=0;i<strlen(input);i++,j++)
     {
          //repeat the key if you are at end of it.
          if(j>=strlen(key))
          {
               j=0;
          }
          //actual logic -> character from input + character from key % 26 is encrypted charater
          printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26));
     }

}

void decipher()
{
     unsigned int i,j;
     char input[257],key[33];
     int value;
     printf("Enter Text to be Decrypted [Max. 256 characters/ only alphabets]:n ");
     gets(input);
     printf("Enter Decryption Key [Max. 32 Characters/ only aphabets]: ");
     gets(key);
     for(i=0,j=0;i<strlen(input);i++,j++)
     {
          //repeat the key if you are at end of it.
          if(j>=strlen(key))
          {
               j=0;
          }
          //similar to encipher only difference is you need to subtract
          value = (toupper(input[i])-64)-(toupper(key[j])-64);
          //if value is negative. We have to rotate it backwards (like backwards from z,y,x) 
          //so add it to 26 (it's a negative value to adding will actually cause subtraction) to get original character.
          if( value < 0)
          {
               value = 26 + value;
          }          
          printf("%c",65 + (value % 26));
     }

}


Was This Post Helpful? 0
  • +
  • -

#3 becirovic   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-April 08

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 04 April 2008 - 01:58 AM

can you tell me how can i find the key from a known text and an encrypt text?
Was This Post Helpful? 0
  • +
  • -

#4 Srijeet Mukherjee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 17-June 08

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 17 June 2008 - 05:40 AM

good
Was This Post Helpful? 0
  • +
  • -

#5 samalik   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 13-September 10

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 13 September 2010 - 08:54 PM

very well done
Was This Post Helpful? 0
  • +
  • -

#6 harshah   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 26-September 10

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 26 September 2010 - 08:01 AM

Is there any way to find out key text only knowing encrypted text?????
Was This Post Helpful? 0
  • +
  • -

#7 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 27 September 2010 - 10:35 PM

If you have only encrypted text then it becomes bit difficult. In such cases brute force might become the option though it's not the best one. Plus one has to check if the received plain text is good enough. If you have partial known text then it becomes bit easier to automate it to find the key.
Was This Post Helpful? 0
  • +
  • -

#8 Murhaf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 25-April 09

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 25 October 2010 - 01:14 PM

The encryption is very well-done, but I think there is a problem with the decryption, try this example (from wikipedia): Plaintext: ATTACKATDAWN Key: LEMON Ciphertext: LXFOPVEFRNHR
Was This Post Helpful? 0
  • +
  • -

#9 mocoyombi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 25-October 10

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 25 October 2010 - 07:44 PM

hey hi... I am trying to understand your code and I am confuse with some line code, for example in line 49. printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26)); Why you are adding 65 and subtracting 65 to the input and key? And in the decipher () if I place the ciphertext with the key I donít get the original plaintext.
Was This Post Helpful? 0
  • +
  • -

#10 mocoyombi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 25-October 10

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 25 October 2010 - 07:51 PM

printf("%c") what it does?
Was This Post Helpful? 0
  • +
  • -

#11 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 25 October 2010 - 09:22 PM

Hey, thanks Murhaf for pointing that out. I goofed up a bit there at the rotating negative value. Check the modified code now. @mocoyombi : the 65 is equivalent of 'A' in ASCII values. if that number is confusing you, you can directly write 'A' at it's place, it's same thing. I am subtracting that form key and text to get the numbers from 0 to 25 (the alphabet number in alphabets, instead of ASCII. It makes the calculation bit easy as you can mod by 26 to rotate alphabets, instead of keep checking if it's gone beyond 'Z' then rotate from 'A' etc. kind of logic.). Check the modified code now, it works fine. for "%c" it's a printf function's format specifier that tells function about what to print, %c is char, %d is int and so on. You can get more information at : http://www.cplusplus.../cstdio/printf/
Was This Post Helpful? 0
  • +
  • -

#12 mocoyombi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 25-October 10

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 25 October 2010 - 10:14 PM

hey sorry..where I can see the modified code?
Was This Post Helpful? 0
  • +
  • -

#13 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 25 October 2010 - 11:05 PM

I have modified this same snippet. check Line No. 76 in this code and compare it to old code (if block). It should have changed. I changed it to "value = 26 + value;"
Was This Post Helpful? 0
  • +
  • -

#14 Murhaf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 25-April 09

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 26 October 2010 - 11:39 AM

@AmitTheInfinity: Exactly that's what I did! Thanks anyway :-)
Was This Post Helpful? 0
  • +
  • -

#15 mocoyombi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 25-October 10

Re: A program to encrypt or decrypt text using vigenere cipher technique

Posted 26 October 2010 - 06:09 PM

ok it work...two more things, there is another way to get ciphertext without the "%c". and what about the 64 in the value of the decipher instead of 65? what it does? scanf("%d",&choice); fflush(stdin);
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2