69,371
社区成员
发帖
与我相关
我的任务
分享
#include <conio.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void Encrypt(char input[80],int len); //你问的是参数怎么写吧参数名字和你的
void NoEncrypt(char input[80],int len);//szinput szoutput都不搭界 爱怎么写就怎么写
int k= 1000;
int main ()
{
char szInput[80];
char szOutput[80];
int nLen,i;
printf( "Input string:%s\n ");
gets(szInput);
nLen = strlen(szInput);
//加密函数
Encrypt(szInput,nLen);
return 0;
}
void Encrypt(char input[80],int len)
{
char szOutput[80];
int n;
for(n = 0; n < len; n ++)
{
if(n % 2)
szOutput[n] = input[n] ^ n;
else
szOutput[n] = input[n] + k;
}
szOutput[n] = 0;
// 输出加密后的字符串.
printf( "Output string:%s\n ", szOutput);
//加密函数里调用解密解密函数
NoEncrypt(szOutput,len);
}
void NoEncrypt(char input[80],int len)
{
char szinput[80];
int n;
for(n = 0; n < len; n ++)
{
if(n % 2)
*szinput[n] = *input[n] ^ n;
else
*szinput[n] = *input[n] - k;
}
*szinput[n] = 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* encrypt(char* source, char* pass) // source: 待加密文本,pass:密码;返回值:密文
{ // 由于异或的特点,加解密皆用此函数
int source_length = strlen(source);
int pass_length = strlen(pass);
char* tmp_str = (char*)malloc((source_length + 1) * sizeof(char));
memset(tmp_str, 0, source_length + 1);
for(int i = 0; i < source_length; ++i)
{
tmp_str[i] = source[i]^pass[i%pass_length];
if(tmp_str[i] == 0) // 要考虑到XOR等于0的情况,如果等于0,就相当
{ // 于字符串就提前结束了, 这是不可以的。
tmp_str[i] = source[i]; // 因此tmp_str[i]等于0的时候,保持原文不变
}
}
tmp_str[source_length] = 0;
return tmp_str;
}
int main(int argc, char* argv[])
{
char* s = "There is a kind of hush all over the world tonight...";
char* pass = "hello";
char* encrypted_text = encrypt(s, pass);
printf("Encrypted text is:\n%s\n", encrypted_text);
char* decrypted_text = encrypt(encrypted_text, pass);
printf("Decrypted text is:\n%s\n", decrypted_text);
free(encrypted_text);
free(decrypted_text);
return 0;
}
// 输出结果:
//Encrypted text is:
//<
//
//HLHH
//
//LL L
//L
//ELoFKB
//Decrypted text is:
//There is a kind of hush all over the world tonight...