Vigenere密码 [英] Vigenere Cipher
问题描述
我正在尝试使用C中的Vigenere Cipher。 https://www.youtube.com/ watch?v = 9zASwVoshiM 这是关于Vigenere Cipher的信息。我的代码工作不适用于某些情况,如加密世界,说你好!作为xoqmd,rby gflkp!使用baz作为关键字,而不是将其加密为xomd,szz fl。另一个例子是:
使用BaZ作为关键字将BaRFoo加密为CaQGon,而将其加密为CakGo。我的代码如下,请帮助我:
I am trying to make Vigenere Cipher in C. https://www.youtube.com/watch?v=9zASwVoshiM this is info about Vigenere Cipher. My code works doesnt work for certain cases like encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword instead it encrypts it as xomd, szz fl. Another example is: encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword but instead it encrypts it as CakGo. My code is given below please help me out:
#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]);
if ((islower(plaintext[u])) && (ciphertext[u])>'z') {
ciphertext[u] = ciphertext[u] - 'z' + 'a' - 1;
}
if ((isupper(plaintext[u])) && (ciphertext[u])>'z') {
ciphertext[u] = ciphertext[u] - 'Z' + 'A' - 1;
}
printf("%c", ciphertext[u]);
}
printf("\n");
return 0;
}
推荐答案
很多小问题code> ciphertext 需要动态分配,并作为 plaintext
的副本开始,而不是键
;需要mod计算到字母的长度;错误的计算;打印错误到
stderr
)和很多很少的优化可以(组合循环;将如果
clauses;将 key
长度组合到一个变量前面)。代码的修改:
Lots of little issues (ciphertext
needs to be allocated dynamically and start out as a copy of plaintext
, not key
; need to mod calculations to the length of the alphabet; incorrect calculations; print errors to stderr
) and lots of little optimizations that can be made (combine loops; combine if
clauses; save key
length to a variable earlier). A rework of your code:
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[]) {
if (argc != 2) {
fprintf(stderr, "Please run the program again with a command line argument!\n");
return 1;
}
string key = argv[1];
int key_length = strlen(key);
int keys[key_length];
for (int i = 0; i < key_length; i++) {
if (!isalpha(key[i])) {
fprintf(stderr, "Re-run The program without any symbols.\n");
return 1;
}
keys[i] = toupper(key[i]) - 'A';
}
// printf("Enter a string which should be encrypted: \n");
string plaintext = GetString();
int text_length = strlen(plaintext);
string ciphertext = strcpy(malloc(text_length + 1), plaintext);
for (int i = 0, j = 0; i < text_length; i++) {
if (!isalpha(plaintext[i])) {
continue;
}
int index = j++ % key_length;
ciphertext[i] = (toupper(plaintext[i]) - 'A' + keys[index]) % (1 + 'Z' - 'A');
ciphertext[i] += isupper(plaintext[i]) ? 'A' : 'a';
}
printf("%s\n", ciphertext);
free(ciphertext);
return 0;
}
加密世界,打个招呼!作为xoqmd,rby gflkp!使用关键字baz
Encrypts "world, say hello!" as "xoqmd, rby gflkp!" with key "baz"
加密BaRFoo为CaQGon与键BaZ
Encrypts "BaRFoo" as "CaQGon" with key "BaZ"
这篇关于Vigenere密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!