最简单的加密---异或加密

 

/*************************************************
*IDE:VS2017
*Author:Rise
*Project:XOR 加密解密
**************************************************/
#include <stdio.h>

/***********************
*cipherttext 密文
*plaintext	明文
*textlen	文本长度
*key		密钥
********************/

void EN_XOR(unsigned char *ciphertext, unsigned char* Plaintext, unsigned char textlen, unsigned char key)
{
	int i = 0;
	for (i = 0; i < textlen; i++)
	{
		ciphertext[i] = Plaintext[i] ^ key;
	}

}

void DE_XOR(unsigned char *ciphertext, unsigned char* Plaintext, unsigned char textlen, unsigned char key)
{
	int i = 0;
	for (i = 0; i < textlen; i++)
	{
		Plaintext[i] = ciphertext[i] ^ key;
	}
}

void test()
{
	int i = 0;
	unsigned char plaint[10] = { 0xaa,0x55,0x44,0x22,0xff,0xcc,0x88,0x99,0x56,0x65 };
	unsigned char ciphert[10];
	unsigned char key = 0xA5;

	EN_XOR(ciphert, plaint, 10, key);

	for (i = 0; i < 10; i++)
	{
		printf("plaint[%d] 0x%02x\n", i, plaint[i]);
	}
	printf("\n");

	for (i = 0; i < 10; i++)
	{
		printf("ciphert[%d] 0x%02x\n", i, ciphert[i]);
	}
	printf("\n");

	DE_XOR(ciphert, plaint, 10, key);
	for (i = 0; i < 10; i++)
	{
		printf("plaint[%d] 0x%02x\n", i, plaint[i]);
	}
	printf("\n");
}


int main()
{
	test();
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python中的异或加密是一种简单加密算法,可以用来对字符串或文件进行加密和解密操作。异或操作符在Python中用^表示。异或加密的原理是将明文和密钥按位进行异或操作,得到密文。解密时,再将密文和密钥按位进行异或操作,还原出明文。 下面是一个用Python实现异或加密函数的示例代码: ```python def xor_cipher(string, key): encrypted = "" for i in range(len(string)): temp = chr(ord(string[i]) ^ ord(key[i % len(key)])) encrypted += temp return encrypted ``` 上面的代码定义了一个`xor_cipher`函数,接收两个参数:`string`是要加密或解密的字符串,`key`是密钥。该函数通过循环遍历字符串的每个字符,并将其与对应位置的密钥字符进行异或操作,得到加密或解密后的字符。最后将加密或解密后的字符拼接成字符串,返回结果。 例如,如果要加密字符串"Hello, world!",使用密钥"secret",可以调用`xor_cipher`函数进行加密操作: ```python plaintext = "Hello, world!" key = "secret" ciphertext = xor_cipher(plaintext, key) print("明文:", plaintext) print("密钥:", key) print("密文:", ciphertext) ``` 运行上述代码会输出加密后得到的密文。 要解密密文,只需要将密文和密钥再次传入`xor_cipher`函数即可: ```python decryptedtext = xor_cipher(ciphertext, key) print("解密后的明文:", decryptedtext) ``` 这样就可以得到原始的明文字符串。 值得注意的是,异或加密是一种简单加密算法,安全性较低,只适用于简单加密需求。在真正的实际应用中,需要使用更加安全可靠的加密算法来保护数据的安全性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [使用Python实现异或密码](https://blog.csdn.net/qq_33885122/article/details/130374298)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python实现文件简单加解密](https://blog.csdn.net/SAGIRIsagiri/article/details/124541807)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Risehuxyc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值