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

C/C++ code#include

#include

#include

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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值