我正在尝试进行vigenere密码。有关此信息,请访问:https : //www.youtube.com/watch?v=9zASwVoshiM我的代码在某些情况下似乎不起作用。我的代码在下面列出,请不要给我发送如何制作vigenere密码的链接,而是一种修复我的密码的方法。例如,如果我将键设置为z,则其值为25 acc(按字母顺序)。现在,如果我将要加密的文本设置为c,即c为2,则新文本的值为27,应该显示b,但对我而言则不然。因此,如果该值超过25,则不会显示我想要的值,否则它将起作用。对于实际的输出示例:ab作为键应将ca更改为cb
#include<stdio.h>
#include<cs50.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
int main( int argc , string argv[]){
//string plaintext;
string key;
if(argc != 2){
printf("Please run the programme again this time using a command line argument!\n");
return 1;
}
key = argv[1];
int keys[strlen(key)];
for(int m = 0; m< strlen(key);m++){
if(isalpha(key[m])==false){
printf("Re-Run The programme without any symbols.\n");
return 1;
}
}
for(int b = 0; b < strlen(key);b++){
if(isupper(key[b]) == false){
keys[b] = key[b] - 'a';
}
else{
keys[b] = key[b] - 'A';
}
}
//printf("Enter a string which should be encrypted: \n");
string plaintext = GetString();
int plength = strlen(plaintext);
int klength = strlen(key);
string ciphertext = key;
for(int u = 0; u<plength;u++){
if(isalpha(plaintext[u])==false){
printf("%c",plaintext[u]);
continue;
}
int value = u % klength;
ciphertext[u] = (keys[value] + plaintext[u]);
//By the more than 90 I am referring to 'z'
if(ciphertext[u]>90){
ciphertext[u] = ciphertext[u] ;
}
printf("%c",ciphertext[u]);
}
printf("\n");
return 0;
}
感谢Kalyan
通过始终从其代码中减去'A'
大写字母和小写字母的代码,可以正确处理键中的值'a'
。它给你:A | a => 0,B | b => 1,...,Z | z => 25。
但是,在加密时,您只是将此值添加到字符代码中,而无需随时进行换行。
让我们使用您的示例:键为'z'=>值25 in keys
,很好。取字符“ c”。它的ASCII(*)代码为0x63或99。99 + 25 = 124给出ascii表'|'
!要正确包装,必须以任何方式确保'z'+ 1 =>'a'。您的代码可能是
/* test wrapping for lowercase letters */
if ((islower(plaintext[u]) && (ciphertext[u]>'z')) {
ciphertext[u] = ciphertext[u] - 'z' + 'a' - 1;
}
/* same for uppercase */
if ((isupper(plaintext[u]) && (ciphertext[u]>'Z')) {
ciphertext[u] = ciphertext[u] - 'Z' + 'A' - 1;
}
(*)该示例假定ASCII代码,因为它是当今最常见的代码,但是该代码仅假定所有大写字母都在顺序中,所有小写字母也都在顺序中,而无需要求其确切值或上下顺序案例序列。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句