2 回答
TA贡献1887条经验 获得超5个赞
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int Three_Parties(char* data, int data_length)
{
typedef unsigned char UCHAR;
int i, index;
char* dict;
UCHAR* polybius_index;
//检查讯息的合法性
for(i = 0; i < data_length; i++)
{
if(data[i] >= 'a' && data[i] <= 'z')
{
//小写转化为大写
data[i] &= 0xDF; //小写字母第5位置为0即变为大写
//data[i] -= 'a' - 'A'; //这种写法也可以,但没有上一种效率高
}
else if(data[i] < 'A' || data[i] > 'Z')
{
//包含非字母的字符
return 1;
}
}
dict = "LEOCBFQSTNARGHJUWXDVIKMPYZ/";
polybius_index = (UCHAR*)malloc(3 * data_length);
for(i = 0; i < data_length; i++)
{
index = strchr(dict, data[i]) - dict;
polybius_index[i] = index / 9;
index %= 9;
polybius_index[i + data_length] = index / 3;
polybius_index[i + 2 * data_length] = index % 3;
}
for(i = 0; i < 3 * data_length; i += 3)
{
index = polybius_index[i]*9 + polybius_index[i+1]*3 + polybius_index[i+2];
data[i/3] = dict[index];
}
free(polybius_index);
return 0;
}
int main(int argc, char* argv[])
{
char data[] = "helloworld";
if(Three_Parties(data, strlen(data)) == 0)
{
printf("%s", data);
}
else
{
printf("input error");
}
return 0;
}
//加密部分还可以改成建索引表,那样效率会更高,但代码稍微复杂些。
希望对你能有所帮助。
TA贡献1871条经验 获得超13个赞
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int Three_Parties(char* data, int data_length)
{
typedef unsigned char UCHAR;
int i, index;
char* dict;
UCHAR* polybius_index;
//检查讯息的合法性
for(i = 0; i < data_length; i++)
{
if(data[i] >= 'a' && data[i] <= 'z')
{
//小写转化为大写
data[i] &= 0xDF; //小写字母第5位置为0即变为大写
//data[i] -= 'a' - 'A'; //这种写法也可以,但没有上一种效率高
}
else if(data[i] < 'A' || data[i] > 'Z')
{
//包含非字母的字符
return 1;
}
}
dict = "LEOCBFQSTNARGHJUWXDVIKMPYZ/";
polybius_index = (UCHAR*)malloc(3 * data_length);
for(i = 0; i < data_length; i++)
{
index = strchr(dict, data[i]) - dict;
polybius_index[i] = index / 9;
index %= 9;
polybius_index[i + data_length] = index / 3;
polybius_index[i + 2 * data_length] = index % 3;
}
for(i = 0; i < 3 * data_length; i += 3)
{
index = polybius_index[i]*9 + polybius_index[i+1]*3 + polybius_index[i+2];
data[i/3] = dict[index];
}
free(polybius_index);
return 0;
}
int main(int argc, char* argv[])
{
char data[] = "helloworld";
if(Three_Parties(data, strlen(data)) == 0)
{
printf("%s", data);
}
else
{
printf("input error");
}
return 0;
}
//加密部分还可以改成建索引表,那样效率会更高,但代码稍微复杂些。
添加回答
举报