各位,能不能把下面这段代码该写一下:实现加密函数调用解密函数(就是把加密和解密封装为两个函数),那里面的参数怎样定义呀……急呀!

akfdgg 2012-07-12 11:22:02
#include <conio.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void Encrypt()
{
char szInput[80];
char szOutput[80];
int n,nLen,i,k;


srand((unsigned int)time(NULL));

k= rand()%10+1;
printf("%d\n",k);

}
}

// 输入待加密字符串.
printf( "Input string:%s\n ");
gets(szInput);
nLen = strlen(szInput);

// 加密.
for(n = 0; n < nLen; n ++)
{
if(n % 2)
szOutput[n] = szInput[n] ^ n;
else szOutput[n] = szInput[n] + k;
}
szOutput[n] = 0;
// 输出加密后的字符串.
printf( "Output string:%s\n ", szOutput);
Dencrypt(k,nLen,szInput,szOutput);


// 清空保存源串的字符数组.
memset(szInput, 0, 80 * sizeof(char));
int n = 0;
// 解密.
for(n = 0; n < nLen; n ++)
{
if(n % 2)
*szInput[n] = *szOutput[n] ^ n;
else *szInput[n] = *szOutput[n] - k;
}

*szInput[n] = 0;
// 输出源串.
printf( "Source string:%s\n ", *szInput);
}

...全文
251 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
akfdgg 2012-07-23
  • 打赏
  • 举报
回复
各位!帮忙看一下……有什么错误

#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);


int main ()
{
char szInput[20];
char szOutput[20];
int nLen,i,k;
k = rand()%10+1;
printf( "Input string:%s\n ");
gets(szInput);
nLen = strlen(szInput);
//加密函数

Encrypt(szInput,nLen,k);
return 0;
}



void Encrypt(char input[20],int len,int k)
{
char szOutput[20];
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);
szOutput = stract(szOutput,k);
//加密函数里调用解密解密函数
NoEncrypt(szOutput,len);
}

void NoEncrypt(char input[20],int len)
{
char szinput[20];
int n,t;
char* p = szinput;
t = strlen(p);
char *k=&p[t-1];
for(n = 0; n < len; n ++)
{
if(n % 2)
szinput[n] = input[n] ^ n;
else
szinput[n] = input[n] - *k;
}
szinput[n] = 0;
}
akfdgg 2012-07-23
  • 打赏
  • 举报
回复
你好!请问 if(n % 2)
*szinput[n] = *input[n] ^ n;
else
*szinput[n] = *input[n] - k;
中的"*"s是什么意思呀……
akfdgg 2012-07-23
  • 打赏
  • 举报
回复
你好!请问 if(n % 2)
*szinput[n] = *input[n] ^ n;
else
*szinput[n] = *input[n] - k;
中的"*"s是什么意思呀……
心死 2012-07-19
  • 打赏
  • 举报
回复
楼主你要的是这个效果吧

#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;
}
akfdgg 2012-07-15
  • 打赏
  • 举报
回复
问题是这样的:我是想对一个字符串进行简单的加密,现在想写一个简单的加密和解密函数函数(如上面所写的),

加密就是: 就是输入一个字符串,让它的奇数位余所产生的随机数进行异或运算,让它的偶数位和随机数进行相加减。
解密就是:把这个字符串的奇数位同样进行异或运算,偶数位和随机数进行相减加。

现请各位改写一下小弟的这个程序,让加密函数可以和解密函数分开,加密函数调用解密函数!!!!
xiebin133 2012-07-14
  • 打赏
  • 举报
回复
百度 , des加密。。
pathuang68 2012-07-14
  • 打赏
  • 举报
回复
下面是一段以前帮一个朋友写的异或加解密的代码,供楼主参考:

#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...
flowingfirefly 2012-07-14
  • 打赏
  • 举报
回复
lz, 用编程语言来实现并不是什么难事, 但大多数人(包括我),对加密解密的不了解,以前就听过公钥啊,密钥啊什么的,lz啊!您好歹先给大家上一堂密码入门知识的扫盲课呀!
akfdgg 2012-07-12
  • 打赏
  • 举报
回复
应该是这样:
#include <conio.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void Encrypt()
{
char szInput[80];
char szOutput[80];
int n,nLen,i,k;


srand((unsigned int)time(NULL));

k= rand()%10+1;
printf("%d\n",k);

}
}

// 输入待加密字符串.
printf( "Input string:%s\n ");
gets(szInput);
nLen = strlen(szInput);

// 加密.
for(n = 0; n < nLen; n ++)
{
if(n % 2)
szOutput[n] = szInput[n] ^ n;
else szOutput[n] = szInput[n] + k;
}
szOutput[n] = 0;
// 输出加密后的字符串.
printf( "Output string:%s\n ", szOutput);
Dencrypt(k,nLen,szInput,szOutput);


// 清空保存源串的字符数组.
memset(szInput, 0, 80 * sizeof(char));
int n = 0;
// 解密.
for(n = 0; n < nLen; n ++)
{
if(n % 2)
szInput[n] = szOutput[n] ^ n;
else szInput[n] = szOutput[n] - k;
}

szInput[n] = 0;
// 输出源串.
printf( "Source string:%s\n ", *szInput);
}
akfdgg 2012-07-12
  • 打赏
  • 举报
回复
呵呵……有问题了大家一起解决,共同进步……

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧