home
search
help
profile
liking the experience? our app is even better
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now  
Search
showing results for
1//One Time Pad Cipher
2#include <stdio.h>
3#include <string.h>
4#include <ctype.h>
5main()
6{
7    //All the text which ever entered is converted to upper and without spaces
8    int i, j, len1, len2, numstr[100], numkey[100], numcipher[100];
9    char str[100], key[100], cipher[100];
10    printf("Enter a string text to encrypt\n");
11    gets(str);
12    for (i = 0, j = 0; i < strlen(str); i++)
13    {
14        if (str[i] != ' ')
15        {
16            str[j] = toupper(str[i]);
17            j++;
18        }
19    }
20    str[j] = '\0';
21    //obtaining numerical plain text ex A-0,B-1,C-2
22    for (i = 0; i < strlen(str); i++)
23    {
24        numstr[i] = str[i] - 'A';
25    }
26    printf("Enter key string of random text\n");
27    gets(key);
28    for (i = 0, j = 0; i < strlen(key); i++)
29    {
30        if (key[i] != ' ')
31        {
32            key[j] = toupper(key[i]);
33            j++;
34        }
35    }
36    key[j] = '\0';
37    //obtaining numerical one time pad(OTP) or key
38    for (i = 0; i < strlen(key); i++)
39    {
40        numkey[i] = key[i] - 'A';
41    }
42
43    for (i = 0; i < strlen(str); i++)
44    {
45        numcipher[i] = numstr[i] + numkey[i];
46    }
47    //To loop the number within 25 i.e if addition of numstr and numkey is 27 then numcipher should be 1
48    for (i = 0; i < strlen(str); i++)
49    {
50        if (numcipher[i] > 25)
51        {
52            numcipher[i] = numcipher[i] - 26;
53        }
54    }
55    printf("One Time Pad Cipher text is\n");
56    for (i = 0; i < strlen(str); i++)
57    {
58        printf("%c", (numcipher[i] + 'A'));
59    }
60    printf("\n");
61}
upvote
downvote
queries leading to this page