|
通过“异或”赋值运算符可以完成对数字的简单加密功能,其基本原理是对数字进行按位“异或”运算,例如,在本实例中,首先对输入文本框中的字符进行判断,确保输入的字符为数字,然后,再对输入的字符进行“异或”运算,其代码如下: private void button1_Click(object sender, EventArgs e) { char[] strOld = this.textBox1.Text.ToCharArray(); char[] strHy = this.textBox3.Text.ToCharArray(); for (int i = 0; i < strOld.Length;i++ ) { if(!Char.IsNumber(strOld[i])) { MessageBox.Show("请输入数字"); this.textBox1.Focus(); this.textBox1.SelectAll(); return; } } for (int i = 0; i < strHy.Length; i++) { if(!Char.IsNumber(strHy[i])) { MessageBox.Show("请输入数字"); this.textBox3.Focus(); this.textBox3.SelectAll(); return; } } int b = int.Parse(this.textBox1.Text.ToString()); int t= int.Parse(this.textBox3.Text.ToString()); int Z = b ^ t; this.textBox2.Text =Z.ToString() ; } |
|