快速了解异或运算符:
异或运算符在C#中用 “^” 来表示
口诀:相同取0,相异取1
简单加密解密winform示例:
/// <summary>/// 异或运算符加密实现/// </summary>/// <param name="p_int_Num">初始值</param>/// <param name="p_int_Key">加密Key</param>private void button1_Click(object sender, EventArgs e){int p_int_Num, p_int_Key;if(int.TryParse(textBox1.Text, out p_int_Num)&&int.TryParse(textBox2.Text,out p_int_Key )){textBox3.Text=(p_int_Num^p_int_Key).ToString();}else{MessageBox.Show("请输入数值", "出现错误");}/*int a = 5;int b = 30;a^b = 275的二进制:0000 010130的二进制:0001 1110而异或的算法就是,两个二进制数的每一位进行比较,如果相同则为0,不同则为10000 01010001 1110----------0001 1011 = 1+2+8+16 = 27*/}/// <summary>/// 异或运算符解密实现/// </summary>/// <param name="P_int_Key">加密Key</param>/// <param name=" P_int_Encrypt">加密后的值</param>private void button2_Click(object sender, EventArgs e){int P_int_Key, P_int_Encrypt;if(int.TryParse(textBox2.Text,out P_int_Key)&& int.TryParse(textBox3.Text,out P_int_Encrypt)){textBox4.Text = (P_int_Key^P_int_Encrypt).ToString();}else{MessageBox.Show("请输入数值", "出现错误");}}/*128 64 32 16 8 4 2 127: 0 0 0 1 1 0 1 130: 0 0 0 1 1 1 1 00 0 1 0 1 = 5*/