英文:
I am trying to make a substitution cipher for an assignment and we need a cipher.txt file that substitutes letters
问题
#include<stdio.h>
int main()
{
//询问用户是要加密还是解密字符串
printf("你想加密还是解密字符串? (e/d): ");
char choice;
scanf("%c", &choice);
//如果用户选择加密,则使用cipher.txt来加密字符串
if (choice == 'e')
{
//询问用户要加密的字符串
printf("输入要加密的字符串:");
char string[100];
scanf("%s", string);
//打开cipher.txt文件
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//读取cipher.txt文件
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//关闭cipher.txt文件
fclose(cipher);
//加密字符串
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '#include<stdio.h>
int main()
{
//询问用户是要加密还是解密字符串
printf("你想加密还是解密字符串? (e/d): ");
char choice;
scanf("%c", &choice);
//如果用户选择加密,则使用cipher.txt来加密字符串
if (choice == 'e')
{
//询问用户要加密的字符串
printf("输入要加密的字符串:");
char string[100];
scanf("%s", string);
//打开cipher.txt文件
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//读取cipher.txt文件
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//关闭cipher.txt文件
fclose(cipher);
//加密字符串
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '\0')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//打印加密后的字符串
printf("加密后的字符串是:%s: ", string);
}
//如果用户选择解密,则解密字符串
else if (choice == 'd')
{
//询问用户要解密的字符串
printf("输入要解密的字符串:");
char string[100];
scanf("%s", string);
//打开cipher.txt文件
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//读取cipher.txt文件
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//关闭cipher.txt文件
fclose(cipher);
//解密字符串
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '\0')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//打印解密后的字符串
printf("解密后的字符串是:%s", string);
}
}
')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//打印加密后的字符串
printf("加密后的字符串是:%s: ", string);
}
//如果用户选择解密,则解密字符串
else if (choice == 'd')
{
//询问用户要解密的字符串
printf("输入要解密的字符串:");
char string[100];
scanf("%s", string);
//打开cipher.txt文件
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//读取cipher.txt文件
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//关闭cipher.txt文件
fclose(cipher);
//解密字符串
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '#include<stdio.h>
int main()
{
//询问用户是要加密还是解密字符串
printf("你想加密还是解密字符串? (e/d): ");
char choice;
scanf("%c", &choice);
//如果用户选择加密,则使用cipher.txt来加密字符串
if (choice == 'e')
{
//询问用户要加密的字符串
printf("输入要加密的字符串:");
char string[100];
scanf("%s", string);
//打开cipher.txt文件
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//读取cipher.txt文件
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//关闭cipher.txt文件
fclose(cipher);
//加密字符串
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '\0')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//打印加密后的字符串
printf("加密后的字符串是:%s: ", string);
}
//如果用户选择解密,则解密字符串
else if (choice == 'd')
{
//询问用户要解密的字符串
printf("输入要解密的字符串:");
char string[100];
scanf("%s", string);
//打开cipher.txt文件
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//读取cipher.txt文件
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//关闭cipher.txt文件
fclose(cipher);
//解密字符串
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '\0')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//打印解密后的字符串
printf("解密后的字符串是:%s", string);
}
}
')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//打印解密后的字符串
printf("解密后的字符串是:%s", string);
}
}
英文:
#include<stdio.h>
int main()
{
//ask the user what they want to encrypt or decrypt a string
printf("Do you want to encrypt or decrypt a string? (e/d): ");
char choice;
scanf("%c", &choice);
//if the user wants to encrypt, then use cipher.txt to encrypt the string
if (choice == 'e')
{
//ask the user for the string they want to encrypt
printf("Enter the string you want to encrypt: ");
char string[100];
scanf("%s", string);
//open cipher.txt
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//close cipher.txt
fclose(cipher);
//encrypt the string
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '#include<stdio.h>
int main()
{
//ask the user what they want to encrypt or decrypt a string
printf("Do you want to encrypt or decrypt a string? (e/d): ");
char choice;
scanf("%c", &choice);
//if the user wants to encrypt, then use cipher.txt to encrypt the string
if (choice == 'e')
{
//ask the user for the string they want to encrypt
printf("Enter the string you want to encrypt: ");
char string[100];
scanf("%s", string);
//open cipher.txt
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//close cipher.txt
fclose(cipher);
//encrypt the string
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '\0')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//print the encrypted string
printf("The encrypted string is: %s: ", string);
}
//if the user chooses decryption, then decrypt the string
else if (choice == 'd')
{
//ask the user for the string they want to decrypt
printf("Enter the string you want to decrypt: ");
char string[100];
scanf("%s", string);
//open cipher.txt
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//close cipher.txt
fclose(cipher);
//decrypt the string
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '\0')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//print the decrypted string
printf("The decrypted string is: %s", string);
}
}
')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//print the encrypted string
printf("The encrypted string is: %s: ", string);
}
//if the user chooses decryption, then decrypt the string
else if (choice == 'd')
{
//ask the user for the string they want to decrypt
printf("Enter the string you want to decrypt: ");
char string[100];
scanf("%s", string);
//open cipher.txt
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//close cipher.txt
fclose(cipher);
//decrypt the string
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '#include<stdio.h>
int main()
{
//ask the user what they want to encrypt or decrypt a string
printf("Do you want to encrypt or decrypt a string? (e/d): ");
char choice;
scanf("%c", &choice);
//if the user wants to encrypt, then use cipher.txt to encrypt the string
if (choice == 'e')
{
//ask the user for the string they want to encrypt
printf("Enter the string you want to encrypt: ");
char string[100];
scanf("%s", string);
//open cipher.txt
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//close cipher.txt
fclose(cipher);
//encrypt the string
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '\0')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//print the encrypted string
printf("The encrypted string is: %s: ", string);
}
//if the user chooses decryption, then decrypt the string
else if (choice == 'd')
{
//ask the user for the string they want to decrypt
printf("Enter the string you want to decrypt: ");
char string[100];
scanf("%s", string);
//open cipher.txt
FILE* cipher;
cipher = fopen("cipher.txt", "r");
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, "%s", cipherText);
//close cipher.txt
fclose(cipher);
//decrypt the string
int i;
for (i = 0; i < 100; i++)
{
if (string[i] == '\0')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//print the decrypted string
printf("The decrypted string is: %s", string);
}
}
')
{
break;
}
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'];
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'] - 32;
}
}
//print the decrypted string
printf("The decrypted string is: %s", string);
}
}
I tried to open and use the Cipher.txt file to swap the letters and encrypt simple words like hello and be able to decrypt them as well by inputting the cipher text. The cipher.txt file is written as so to swap the letters:
H,X
E,P
L,R
O,Q
And I cannot figure out how to get my program to read the file and take swap the letters with that given file
答案1
得分: 1
代码存在以下问题。
输入没有限制
代码存在溢出cipherText[]
的风险。应该使用宽度限制,比数组大小少1。
char string[100];
scanf("%99s", string);
char cipherText[26];
fscanf(cipher, "%25s", cipherText);
cipher.txt问题
关于cipher.txt
的内容不明确,但似乎有25个字母映射[A-Z],但没有'O'。
我期望cipher.txt
应该是包含26个大写字母的:QWRST...A
。
cipher.txt
文件可疑,因为1)它没有26个映射,2)一些字母映射到非字母。
#2 使得解密变得困难,如果原始消息包含非字母内容。
cipherText[]
太小
要从文本文件读取密码,并使用它来编码26个字母,cipherText[]
需要是27,以容纳26个字母映射和空字符。
char cipherText[26+1];
fscanf(cipher, "%26s", cipherText);
大小写映射错误
原作者表示密码是大写映射。那么编码需要更改:
else if (string[i] >= 'a' && string[i] <= 'z')
{
string[i] = cipherText[string[i] - 'a'] - 'A' + 'a';
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
string[i] = cipherText[string[i] - 'A'];
}
代码解密不正确
看起来是加密代码的重复,使用相同的"cipher.txt"
文件。代码需要反向映射,而不只是再次应用。
可能还有其他问题
英文:
Code has at least these problems.
No limit on input
Code risks overflowing cipherText[]
. Instead, use a width limit which is 1 less than the size.
char string[100];
// scanf("%s", string);
scanf("%99s", string);
char cipherText[26];
// fscanf(cipher, "%s", cipherText);
fscanf(cipher, "%25s", cipherText);
cipher.txt issues
It is unclear concerning the contents of cipher.txt
, but it appears to have 25 letter mappings [A-Z], but not 'O'.
I would expect cipher.txt to be 26 uppercase characters: QWRST...A
.
cipher.txt file is dubious as it 1) does not have 26 mappings and 2) maps some letters to non-letters.
#2 makes it problematic to decipher if the original message contained non-letters.
cipherText[]
too small
To read a cipher from a text file with fscanf(cipher, "%s", cipherText);
and use it to encode 26 letters, cipherText[]
needs to be 27 to hold the 26 letter mappings and the null character.
char cipherText[26+1];
// fscanf(cipher, "%s", cipherText);
fscanf(cipher, "%26s", cipherText);
Wrong case mapping
OP indicates the cipher has an uppercase mapping. Then encoding needs to change:
else if (string[i] >= 'a' && string[i] <= 'z')
{
//string[i] = cipherText[string[i] - 'a'];
string[i] = cipherText[string[i] - 'a'] - 'A' + 'a';
}
else if (string[i] >= 'A' && string[i] <= 'Z')
{
// string[i] = cipherText[string[i] - 'A'] - 32;
string[i] = cipherText[string[i] - 'A'];
}
Code decrypts incorrectly
Looks like a repeat of encrypt code using same "cipher.txt"
file. Code needs to reverse the mapping, not simply apply it again.
Perhaps other issues too
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论