如何解决凯撒密码帮助,找出空格,整数和特殊字符
我正在尝试使用C语言创建凯撒密码,但是在加密和解密空格,整数和特殊字符(如!
,$
,#
,等。我有一个简单的代码来找出字符串部分并将其转换。任何建议将是可爱的!改善我的代码的建议也有帮助。
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
// Ascii codes: 'a'=97,'b' = 98,and so on.
void encrypt()
{
char message[500],c;
int i;
int key;
printf("Enter a message to encrypt: ");
scanf("%s",&message);
printf("Enter key: ");
scanf("%d",&key);
for (i = 0; message[i] != '\0'; i++) {
c = message[i];
if (c >= 'a' && c <= 'z') {
c = c + key;
if (c > 'z') {
c = c - 'z' + 'a' - 1;
}
message[i] = c;
} else if (c >= 'A' && c <= 'Z') {
c = c + key;
if (c > 'Z') {
c = c - 'Z' + 'A' - 1;
}
message[i] = c;
}
}
printf("Encrypted message: %s",message);
}
void decrypt()
{
char message[500],c;
int i;
int key;
printf("Enter a message to decrypt: ");
scanf("%s",&key);
for (i = 0; message[i] != '\0'; i++) {
c = message[i];
if(c >= 'a' && c <= 'z') {
c = c - key;
if (c < 'a') {
c = c + 'z' - 'a' + 1;
}
message[i] = c;
} else if(c >= 'A' && c <= 'Z') {
c = c - key;
if(c < 'A') {
c = c + 'Z' - 'A' + 1;
}
message[i] = c;
}
}
printf("Decrypted message: %s",message);
}
int main()
{
encrypt();
return 0;
}
解决方法
一种可能的解决方案是维护一个恒定的特殊字符数组,并将其索引用于加密/解密目的。
请考虑以下代码段:
const char specialCharacterList[] = "!@#$%&(){}[] +-*/'?.,;\"";
int getSpecialCharacterIndex(char c)
{
int specialCharacterCount = strlen(specialCharacterList);
int i;
for(i=0; i<specialCharacterCount; i++)
{
if(c == specialCharacterList[i])
return i;
}
return -1;
}
void encrypt(char givenMessage[],int shiftValue)
{
char givenCharacter,encryptedCharacter;
int messageLength = strlen(givenMessage);
int i,specialCharacterIndex;
for(i=0; i<messageLength; i++)
{
givenCharacter = givenMessage[i];
if(isalpha(givenCharacter))
{
if(isupper(givenCharacter))
{
// handle upper case characters
}
else
{
// handle lower case characters
}
}
else if(isdigit(givenCharacter))
{
//handle digits
}
else
{
specialCharacterIndex = getSpecialCharacterIndex(givenCharacter);
if(specialCharacterIndex == -1)
{
printf("Unidentified character found: %c\n",givenCharacter);
encryptedCharacter = ' ';
}
else
{
encryptedCharacter = specialCharacterList[(specialCharacterIndex+shiftValue) % strlen(specialCharacterList)];
}
}
givenMessage[i] = encryptedCharacter;
}
}
在这里,我将特殊字符存储在specialCharacterList[]
数组中。使用getSpecialCharacterIndex()
函数,我检索数组中特殊字符的索引,并对数组的索引执行操作。
注意:您还可以在specialCharacterList[]
中包含空格,制表符,换行符。
如果字符未包含在数组中,它将显示在控制台上。然后我们可以将其包含在specialCharacterList[]
中。
在另一个注释上,您提到您也要处理空格。在那种情况下,我建议您使用gets()
而不是scanf()
来获取输入,因为scanf()
一旦遇到空间就会停止接收输入。
缺点: 仅当加密器和解密器具有相同的特殊字符数组时,该解决方案才可行。
,您可以检查字符是否在以下范围内 32–47 / 58–64 / 91–96 / 123–126 ,则为特殊字符。
或者这就是我的使用方式
if(c >= 'a' && c <= 'z'){
c = c - key;
if(c < 'a'){
c = c + 'z' - 'a' + 1;
}
message[i] = c;
}
else if(c >= 'A' && c <= 'Z'){
c = c - key;
if(c < 'A'){
c = c + 'Z' - 'A' + 1;
}
message[i] = c;
}
else{
message[i] = c;
}
这将非常简单
,该算法在Caesar cipher - wikipedia中进行了描述。您已经发现,值的偏移将需要您考虑距范围末端的偏移量,这很好。在遇到问题的地方,两种情况都是对字符进行key
(移位)的加减,以及随后对大于'z'
或小于'a'
的测试: / p>
c = c + key;
if (c > 'z') {
和
c = c - key;
if (c < 'a') {
如果加或减key
(移位值)的结果不大于'z'
或小于'a'
,(且不能保证它们会或不会基于key
的大小),除非您的条件测试为 true ,否则您根本不会应用任何编码或解码。
对于该算法,要求(x + n)
或(x - n)
的结果必须在0 - 25
之间。 (请参阅链接中的算法)这是% 26
用来防止值超过25
的情况,但是您还必须确保该值是非负数。您可以通过添加26
并用一点代数来得出一个涵盖所有情况的表达式(一个代表大写字母,一个代表小写字母)来满足此要求。例如:
#define SIZE 26
...
int encipher (int c,int shift)
{
if ('A' <= c && c <= 'Z')
/*
* c = 'A' + (c - 'A' + 'Z' - shift + 1 - 'A') % SIZE;
* c = 'A' + (c - 'A' - shift + 'Z' - 'A' + 1) % SIZE;
* where ('Z' - 'A' + 1 == SIZE)
*/
c = 'A' + (c - 'A' - shift + SIZE) % SIZE;
else if ('a' <= c && c <= 'z')
c = 'a' + (c - 'a' - shift + SIZE) % SIZE;
return c;
}
并用于解码:
int decipher (int c,int shift)
{
if ('A' <= c && c <= 'Z')
/*
* c = 'A' + (c - 'A' - ('Z' - shift + 1 - 'A')) % SIZE;
* c = 'A' + (c - 'A' - 'Z' + shift - 1 + 'A') % SIZE;
* c = 'A' + (c - 'Z' + shift - 1) % SIZE;
* where (c - 'Z' < 0) for (shift - 1 < SIZE)
*/
c = 'A' + (c - 'Z' + shift - 1 + SIZE) % SIZE;
else if ('a' <= c && c <= 'z')
c = 'a' + (c - 'z' + shift - 1 + SIZE) % SIZE;
return c;
}
添加一个简短的示例,使您可以输入移位(您的key
)和要编码和解码的文本(最多1024个字符,1023 +空字符),您可以这样做:
#include <stdio.h>
#include <string.h>
#define SIZE 26
#define MAXC 1024
int encipher (int c,int shift)
{
if ('A' <= c && c <= 'Z')
/*
* c = 'A' + (c - 'A' + 'Z' - shift + 1 - 'A') % SIZE;
* c = 'A' + (c - 'A' - shift + 'Z' - 'A' + 1) % SIZE;
* where ('Z' - 'A' + 1 == SIZE)
*/
c = 'A' + (c - 'A' - shift + SIZE) % SIZE;
else if ('a' <= c && c <= 'z')
c = 'a' + (c - 'a' - shift + SIZE) % SIZE;
return c;
}
int decipher (int c,int shift)
{
if ('A' <= c && c <= 'Z')
/*
* c = 'A' + (c - 'A' - ('Z' - shift + 1 - 'A')) % SIZE;
* c = 'A' + (c - 'A' - 'Z' + shift - 1 + 'A') % SIZE;
* c = 'A' + (c - 'Z' + shift - 1) % SIZE;
* where (c - 'Z' < 0) for (shift - 1 < SIZE)
*/
c = 'A' + (c - 'Z' + shift - 1 + SIZE) % SIZE;
else if ('a' <= c && c <= 'z')
c = 'a' + (c - 'z' + shift - 1 + SIZE) % SIZE;
return c;
}
int main (void) {
int n = 0,shift;
char text[MAXC],encode[sizeof text],decode[sizeof text];
fputs ("enter shift: ",stdout); /* just use text for both inputs */
if (!fgets (text,MAXC,stdin))
return 1;
if (sscanf (text,"%d",&shift) != 1) {
fputs ("error: invalid integer input.\n",stderr);
return 1;
}
fputs ("\ninput : ",stdout);
if (!fgets (text,stdin))
return 0;
text[strcspn(text,"\n")] = 0; /* trim trailing \n */
do /* encode text */
encode[n] = encipher(text[n],shift);
while (text[n++]);
n = 0;
do /* decode encoded text */
decode[n] = decipher(encode[n],shift);
while (encode[n++]);
printf ("encode: %s\ndecode: %s\n",encode,decode);
}
(注意:,实际上在加密中,编码很少表示为以n结尾的字符串。在这里,由于结果仅限于ASCII字符,因此便于输出且便于练习,但通常您只会存储编码值和字符数计数
使用/输出示例
用字母检查大写和小写:
$ ./bin/ceasar_cipher
enter shift: 3
input : ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
encode: XYZABCDEFGHIJKLMNOPQRSTUVW xyzabcdefghijklmnopqrstuvw
decode: ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
对照维基百科示例:
$./bin/ceasar_cipher
enter shift: 3
input : THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
encode: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
decode: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
仔细检查一下,如果还有其他问题,请告诉我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。