哪位高手写一个用c语言对字符串进行加密解密的算法(最好有注释),万分火急呀!有重谢!!!

akfdgg 2012-07-10 11:59:32
用C实现一个对字符串进行加密解密的算法 最简单的 …………谢谢!
...全文
1106 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2012-07-11
  • 打赏
  • 举报
回复
用异或进行加解密呗,最简单了。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char* encrypt(char* source, char* 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...
akfdgg 2012-07-11
  • 打赏
  • 举报
回复
一楼的兄弟!不对呀!
char* decrypted_text = encrypt(encrypted_text, pass);
•Error NONAME00.C 34: Declaration is not allowed here!!!!
Defonds 2012-07-11
  • 打赏
  • 举报
回复
办法很多
时间坐标 2012-07-11
  • 打赏
  • 举报
回复
简单的话,就是古典密码了。随便用一种就行。
W170532934 2012-07-11
  • 打赏
  • 举报
回复
加解密算法看你怎么做的了

70,020

社区成员

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

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