this is my code, i want to input string "adiva fiqri" with key "div" but i want the key is "divdi vdivd" (with space seems like string input) but my new key is following the string (including space) "divdivdivdi"
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
void capital(int i, int j, char str[100]){
//converting entered string to Capital letters
for(i=0,j=0;i<strlen(str);i++)
{
str[j]=toupper(str[i]);
j++;
}
str[j]='\0';
return str[j];
}
int main()
{
int i,j,k,numstr[100],numkey[100],numcipher[100];
char str[100],key[100], newKey[100];
printf("Enter a string : ");
gets(str);
//converting entered string to Capital letters
/*
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;
}
}
str[j]='\0';
printf("Entered string is : %s \n",str);
*/
capital(i, j, str);
printf("Entered string is : %s \n",str);
//Storing string in terms of ascii
for(i=0;i<strlen(str);i++)
{
numstr[i]=str[i]-'A';
}
//masukan kunci
printf("Enter a key : ");
gets(key);
//converting entered key to Capital letters
for(i=0,j=0;i<strlen(key);i++)
{
key[j]=toupper(key[i]);
j++;
}
key[j]='\0';
//Assigning key to the string
for(i=0;i<strlen(str);)
{
for(j=0;(j<strlen(key))&&(i<strlen(str));j++)
{
numkey[i]=key[j]-'A';
i++;
}
}
//enkripsi
for(i=0;i<strlen(str);i++)
{
numcipher[i]=numstr[i]+numkey[i];
if(numcipher[i]>25)
{
numcipher[i]=numcipher[i]-26;
}
}
//generating new key
for(i = 0, j = 0; i < strlen(str); ++i, ++j){
if(j == strlen(key))
j = 0;
newKey[i] = key[j];
}
//hasil
printf("New Key : %s \n", newKey);
printf("Vigenere Cipher text is : ");
for(i=0;i<strlen(str);i++)
{
printf("%c",(numcipher[i]+'A'));
}
printf("\n");
}
this is my code, i want to input string "adiva fiqri" with key "div" but i want the key is "divdi vdivd" (with space seems like string input) but my new key is following the string (including space) "divdivdivdi"
The key is to understand you only encode and decode Letters. All other characters remain unchanged. The case of the key does not matter since the key simply represents an offset from the beginning of the alphabet. The only requirement is that you be consistent with case when measuring offset from the beginning.
You should only call
strlen()
once on any given string. If you need to save the length, then create a variable, e.g.There is no need to use
strlen()
in a loop limit (e.g.for(i=0;i<strlen(str);i++)
) to iterate over a string. In C a string is terminated with the nul-character'\0'
(e.g. plain old ASCII0
). All you need to do it iterate using the character as the test. When your reach the nul-terminating character and the loop will exit on its own, e.g.With that in mind, you can follow the description at Vigenère cipher to write your encryption and decryption implementations. You can write the encrypt function as:
And your decrypt function would be:
If you like, you can replace the use of pointers with array indexes (e.g. using a
for
loop with indexi
andk[i]
instead of using a pointer to the current character -- up to you. I find it easier just to iterate with a pointer until*ptr == 0;
when the nul-character is reached at the end of the string. (just the same as usingptr[i]
)A complete example where you pass the
key
as the first argument to the program ("LEMON"
by default if no argument is given -- to match the examples in the link above). The user is prompted to enter the string to encrypt/decrypt:Example Use/Output*
Using the Wikipedia Example:
Using your example:
Look things over and let me know if you have further questions.