I am trying to make a substitution cipher for an assignment and we need a cipher.txt file that substitutes letters

huangapple go评论65阅读模式
英文:

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&lt;stdio.h&gt;
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&lt;stdio.h&gt;
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); } }
英文:

My cipher.txt file

#include&lt;stdio.h&gt;

int main()
{
	//ask the user what they want to encrypt or decrypt a string
	printf(&quot;Do you want to encrypt or decrypt a string? (e/d): &quot;);
	char choice;
	scanf(&quot;%c&quot;, &amp;choice);

	//if the user wants to encrypt, then use cipher.txt to encrypt the string
	if (choice == &#39;e&#39;)
	{
		//ask the user for the string they want to encrypt
		printf(&quot;Enter the string you want to encrypt: &quot;);
		char string[100];
		scanf(&quot;%s&quot;, string);

		//open cipher.txt
		FILE* cipher;
		cipher = fopen(&quot;cipher.txt&quot;, &quot;r&quot;);

		//read the cipher.txt file
		char cipherText[26];
		fscanf(cipher, &quot;%s&quot;, cipherText);

		//close cipher.txt
		fclose(cipher);

		//encrypt the string
		int i;
		for (i = 0; i &lt; 100; i++)
		{
			if (string[i] == &#39;
#include&lt;stdio.h&gt;
int main()
{
//ask the user what they want to encrypt or decrypt a string
printf(&quot;Do you want to encrypt or decrypt a string? (e/d): &quot;);
char choice;
scanf(&quot;%c&quot;, &amp;choice);
//if the user wants to encrypt, then use cipher.txt to encrypt the string
if (choice == &#39;e&#39;)
{
//ask the user for the string they want to encrypt
printf(&quot;Enter the string you want to encrypt: &quot;);
char string[100];
scanf(&quot;%s&quot;, string);
//open cipher.txt
FILE* cipher;
cipher = fopen(&quot;cipher.txt&quot;, &quot;r&quot;);
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, &quot;%s&quot;, cipherText);
//close cipher.txt
fclose(cipher);
//encrypt the string
int i;
for (i = 0; i &lt; 100; i++)
{
if (string[i] == &#39;\0&#39;)
{
break;
}
else if (string[i] &gt;= &#39;a&#39; &amp;&amp; string[i] &lt;= &#39;z&#39;)
{
string[i] = cipherText[string[i] - &#39;a&#39;];
}
else if (string[i] &gt;= &#39;A&#39; &amp;&amp; string[i] &lt;= &#39;Z&#39;)
{
string[i] = cipherText[string[i] - &#39;A&#39;] - 32;
}
}
//print the encrypted string
printf(&quot;The encrypted string is: %s: &quot;, string);
}
//if the user chooses decryption, then decrypt the string
else if (choice == &#39;d&#39;)
{
//ask the user for the string they want to decrypt
printf(&quot;Enter the string you want to decrypt: &quot;);
char string[100];
scanf(&quot;%s&quot;, string);
//open cipher.txt
FILE* cipher;
cipher = fopen(&quot;cipher.txt&quot;, &quot;r&quot;);
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, &quot;%s&quot;, cipherText);
//close cipher.txt
fclose(cipher);
//decrypt the string
int i;
for (i = 0; i &lt; 100; i++)
{
if (string[i] == &#39;\0&#39;)
{
break;
}
else if (string[i] &gt;= &#39;a&#39; &amp;&amp; string[i] &lt;= &#39;z&#39;)
{
string[i] = cipherText[string[i] - &#39;a&#39;];
}
else if (string[i] &gt;= &#39;A&#39; &amp;&amp; string[i] &lt;= &#39;Z&#39;)
{
string[i] = cipherText[string[i] - &#39;A&#39;] - 32;
}
}
//print the decrypted string
printf(&quot;The decrypted string is: %s&quot;, string);
}
}
&#39;) { break; } else if (string[i] &gt;= &#39;a&#39; &amp;&amp; string[i] &lt;= &#39;z&#39;) { string[i] = cipherText[string[i] - &#39;a&#39;]; } else if (string[i] &gt;= &#39;A&#39; &amp;&amp; string[i] &lt;= &#39;Z&#39;) { string[i] = cipherText[string[i] - &#39;A&#39;] - 32; } } //print the encrypted string printf(&quot;The encrypted string is: %s: &quot;, string); } //if the user chooses decryption, then decrypt the string else if (choice == &#39;d&#39;) { //ask the user for the string they want to decrypt printf(&quot;Enter the string you want to decrypt: &quot;); char string[100]; scanf(&quot;%s&quot;, string); //open cipher.txt FILE* cipher; cipher = fopen(&quot;cipher.txt&quot;, &quot;r&quot;); //read the cipher.txt file char cipherText[26]; fscanf(cipher, &quot;%s&quot;, cipherText); //close cipher.txt fclose(cipher); //decrypt the string int i; for (i = 0; i &lt; 100; i++) { if (string[i] == &#39;
#include&lt;stdio.h&gt;
int main()
{
//ask the user what they want to encrypt or decrypt a string
printf(&quot;Do you want to encrypt or decrypt a string? (e/d): &quot;);
char choice;
scanf(&quot;%c&quot;, &amp;choice);
//if the user wants to encrypt, then use cipher.txt to encrypt the string
if (choice == &#39;e&#39;)
{
//ask the user for the string they want to encrypt
printf(&quot;Enter the string you want to encrypt: &quot;);
char string[100];
scanf(&quot;%s&quot;, string);
//open cipher.txt
FILE* cipher;
cipher = fopen(&quot;cipher.txt&quot;, &quot;r&quot;);
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, &quot;%s&quot;, cipherText);
//close cipher.txt
fclose(cipher);
//encrypt the string
int i;
for (i = 0; i &lt; 100; i++)
{
if (string[i] == &#39;\0&#39;)
{
break;
}
else if (string[i] &gt;= &#39;a&#39; &amp;&amp; string[i] &lt;= &#39;z&#39;)
{
string[i] = cipherText[string[i] - &#39;a&#39;];
}
else if (string[i] &gt;= &#39;A&#39; &amp;&amp; string[i] &lt;= &#39;Z&#39;)
{
string[i] = cipherText[string[i] - &#39;A&#39;] - 32;
}
}
//print the encrypted string
printf(&quot;The encrypted string is: %s: &quot;, string);
}
//if the user chooses decryption, then decrypt the string
else if (choice == &#39;d&#39;)
{
//ask the user for the string they want to decrypt
printf(&quot;Enter the string you want to decrypt: &quot;);
char string[100];
scanf(&quot;%s&quot;, string);
//open cipher.txt
FILE* cipher;
cipher = fopen(&quot;cipher.txt&quot;, &quot;r&quot;);
//read the cipher.txt file
char cipherText[26];
fscanf(cipher, &quot;%s&quot;, cipherText);
//close cipher.txt
fclose(cipher);
//decrypt the string
int i;
for (i = 0; i &lt; 100; i++)
{
if (string[i] == &#39;\0&#39;)
{
break;
}
else if (string[i] &gt;= &#39;a&#39; &amp;&amp; string[i] &lt;= &#39;z&#39;)
{
string[i] = cipherText[string[i] - &#39;a&#39;];
}
else if (string[i] &gt;= &#39;A&#39; &amp;&amp; string[i] &lt;= &#39;Z&#39;)
{
string[i] = cipherText[string[i] - &#39;A&#39;] - 32;
}
}
//print the decrypted string
printf(&quot;The decrypted string is: %s&quot;, string);
}
}
&#39;) { break; } else if (string[i] &gt;= &#39;a&#39; &amp;&amp; string[i] &lt;= &#39;z&#39;) { string[i] = cipherText[string[i] - &#39;a&#39;]; } else if (string[i] &gt;= &#39;A&#39; &amp;&amp; string[i] &lt;= &#39;Z&#39;) { string[i] = cipherText[string[i] - &#39;A&#39;] - 32; } } //print the decrypted string printf(&quot;The decrypted string is: %s&quot;, 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(&quot;%s&quot;, string);
    scanf(&quot;%99s&quot;, string);

    char cipherText[26];
    // fscanf(cipher, &quot;%s&quot;, cipherText);
    fscanf(cipher, &quot;%25s&quot;, 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, &quot;%s&quot;, 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, &quot;%s&quot;, cipherText);
    fscanf(cipher, &quot;%26s&quot;, cipherText);

Wrong case mapping

OP indicates the cipher has an uppercase mapping. Then encoding needs to change:

        else if (string[i] &gt;= &#39;a&#39; &amp;&amp; string[i] &lt;= &#39;z&#39;)
        {
            //string[i] = cipherText[string[i] - &#39;a&#39;];
            string[i] = cipherText[string[i] - &#39;a&#39;] - &#39;A&#39; + &#39;a&#39;;
        }
        else if (string[i] &gt;= &#39;A&#39; &amp;&amp; string[i] &lt;= &#39;Z&#39;)
        {
            // string[i] = cipherText[string[i] - &#39;A&#39;] - 32;
            string[i] = cipherText[string[i] - &#39;A&#39;];
        }

Code decrypts incorrectly

Looks like a repeat of encrypt code using same &quot;cipher.txt&quot; file. Code needs to reverse the mapping, not simply apply it again.

Perhaps other issues too

huangapple
  • 本文由 发表于 2023年2月9日 03:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定