Contact Learn C
Copy Program Share Program

Program 218:One Time Pad Cipher using C

Program 218: To understand What is One Time Pad Cipher ?
 
#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
 //All the text which ever entered is converted to upper and without spaces
 int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];
 char str[100],key[100],cipher[100];
 printf("Enter a string text to encrypt\n");
 gets(str);
 for(i=0,j=0;i<strlen(str);i++)
 {
  if(str[i]!=' ')
  {
   str[j]=toupper(str[i]);   
   j++;
  }
 }
 str[j]='\0';
 //obtaining numerical plain text ex A-0,B-1,C-2
    for(i=0;i<strlen(str);i++)
    {
     numstr[i]=str[i]-'A';    
    }   
    printf("Enter key string of random text\n");
    gets(key);
 for(i=0,j=0;i<strlen(key);i++)
 {
  if(key[i]!=' ')
  {
   key[j]=toupper(key[i]);   
   j++;
  }
 }
 key[j]='\0';
 //obtaining numerical one time pad(OTP) or key
    for(i=0;i<strlen(key);i++)
    {
     numkey[i]=key[i]-'A';    
    }  
    
    for(i=0;i<strlen(str);i++)
    {
     numcipher[i]=numstr[i]+numkey[i];
    }
    //To loop the number within 25 i.e if addition of numstr and numkey is 27 then numcipher should be 1
    for(i=0;i<strlen(str);i++)
    {
     if(numcipher[i]>25)
     {
      numcipher[i]=numcipher[i]-26;
     }
    }
    printf("One Time Pad Cipher text is\n");
    for(i=0;i<strlen(str);i++)
    {
      printf("%c",(numcipher[i]+'A')); 
    }
    printf("\n");

}


Explanation:
Pre Explanation:
One Time Pad Cipher Explanation


//Coming Soon...

Output:


One Time Pad Cipher using c

One Time Pad Cipher








 

3 comments:

  1. there is code for decrypt?
    I,m still newbie
    thank u

    ReplyDelete
    Replies
    1. yes , please click the link below

      http://www.cprograms4future.com/p/decryption-of-one-time-pad.html

      Delete
  2. thank you, it helps me a lot,
    may I ask a something?
    in OTP algorithm, can we encypt with 255 characters?

    ReplyDelete

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide