异或,英文为exclusive OR,或缩写成xor
异或(xor)是一个数学运算符。它应用于逻辑运算。异或的数学符号为“⊕”,计算机符号为“xor”。其运算法则为: a⊕b = (¬a ∧
b) ∨ (a ∧¬b) 如果a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0。
异或也叫半加运算,其运算法则相当于不带进位的二进制加法:二进制下用1表示真,0表示假,则异或的运算法则为:0⊕0=0,1⊕0=1,0⊕1=1,1⊕1=0(同为0,异为1),这些法则与加法是相同的,只是不带进位。
异或略称为XOR、EOR、EX-OR 程序中有三种演算子:XOR、xor、⊕。 使用方法如下 z = x ⊕ y z = x xor y
MD5加密算法:http://blog.csdn.net/huangxiaoguo1/article/details/78042596
Base64加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78042715
异或加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78042802
DES加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78042908
AES自动生成base64密钥加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78043000
AES加密解密(ECB模式):http://blog.csdn.net/huangxiaoguo1/article/details/78043098
AES加密解密(CBC模式):http://blog.csdn.net/huangxiaoguo1/article/details/78043169
非对称RSA加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78043354
运算法则
- a ⊕ a = 0
- a ⊕ b = b ⊕ a
- a ⊕b ⊕ c = a ⊕ (b ⊕ c) = (a ⊕ b) ⊕ c;
- d = a ⊕ b ⊕ c 可以推出 a = d ⊕ b ⊕ c.
- a ⊕ b ⊕ a = b.
6.若x是二进制数0101,y是二进制数1011; 则x⊕y=1110 只有在两个比较的位不同时其结果是1,否则结果为0 即“两个输入相同时为0,不同则为1”!
效果
代码
ORActivity
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import tsou.com.encryption.R;
import tsou.com.encryption.aescbc.Base64Decoder;
import tsou.com.encryption.aescbc.Base64Encoder;
import tsou.com.encryption.or.OrUtils;
/**
* 一、什么是异或加密?
* <p>
* 异或运算中,如果某个字符(或数值)x 与 一个数值m 进行异或运算得到y,则再用y 与 m 进行异或运算就可以还原为 x ,
* 因此应用这个原理可以实现数据的加密解密功能。
* <p>
* 二、异或运算使用场景?
* <p>
* 两个变量的互换(不借助第三个变量)
* <p>
* 数据的简单加密解密
*/
public class ORActivity extends AppCompatActivity implements View.OnClickListener {
private EditText encryptionContext;
private Button encryption;
private TextView tvEncryption;
private Button decode;
private TextView tvDecode;
private Activity mActivity;
private Context mContext;
private Button encryptionNotFixed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_or);
mActivity = this;
mContext = this;
encryptionContext = (EditText) findViewById(R.id.et_encryption_context);
encryption = (Button) findViewById(R.id.btn_encryption);
encryptionNotFixed = (Button) findViewById(R.id.btn_encryption_not_fixed);
tvEncryption = (TextView) findViewById(R.id.tv_encryption);
decode = (Button) findViewById(R.id.btn_decode);
tvDecode = (TextView) findViewById(R.id.tv_decode);
initListener();
}
private void initListener() {
encryption.setOnClickListener(this);
encryptionNotFixed.setOnClickListener(this);
decode.setOnClickListener(this);
}
private boolean isEncryption = true;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_encryption://固定加密
String encryptionString = encryptionContext.getText().toString().trim();
if (TextUtils.isEmpty(encryptionString)) {
Toast.makeText(mContext, "请输入加密内容", Toast.LENGTH_SHORT).show();
return;
}
byte[] encode = OrUtils.encrypt(encryptionString.getBytes());
tvEncryption.setText(Base64Encoder.encode(encode));
isEncryption = true;
break;
case R.id.btn_encryption_not_fixed://不固定加密
String encryptionStringNotFixed = encryptionContext.getText().toString().trim();
if (TextUtils.isEmpty(encryptionStringNotFixed)) {
Toast.makeText(mContext, "请输入加密内容", Toast.LENGTH_SHORT).show();
return;
}
byte[] encodeNotFixed = OrUtils.encode(encryptionStringNotFixed.getBytes());
tvEncryption.setText(Base64Encoder.encode(encodeNotFixed));
isEncryption = false;
break;
case R.id.btn_decode://解密
String decodeString = tvEncryption.getText().toString().trim();
if (TextUtils.isEmpty(decodeString)) {
Toast.makeText(mContext, "请先加密", Toast.LENGTH_SHORT).show();
return;
}
if (isEncryption) {
byte[] encrypt = OrUtils.encrypt(Base64Decoder.decodeToBytes(decodeString));
tvDecode.setText(new String(encrypt));
} else {
byte[] decode = OrUtils.decode(Base64Decoder.decodeToBytes(decodeString));
tvDecode.setText(new String(decode));
}
break;
}
}
}
OrUtils
/**
* Created by Administrator on 2017/9/20 0020.
*/
public class OrUtils {
private final static int orKey = 0x520;
/**
* 固定key的方式,
* <p>
* 这种方式加密解密 算法一样
*
* @param bytes
* @return
*/
public static byte[] encrypt(byte[] bytes) {
if (bytes == null) {
return null;
}
int len = bytes.length;
int key = orKey;
//int key = 0x12;
for (int i = 0; i < len; i++) {
bytes[i] ^= key;
}
return bytes;
}
/**
* 不固定key的方式
* <p>
* 加密实现
*
* @param bytes
* @return
*/
public static byte[] encode(byte[] bytes) {
if (bytes == null) {
return null;
}
int len = bytes.length;
int key = orKey;
//int key = 0x12;
for (int i = 0; i < len; i++) {
bytes[i] = (byte) (bytes[i] ^ key);
key = bytes[i];
}
return bytes;
}
/**
* 不固定key的方式
* <p>
* 解密实现
*
* @param bytes
* @return
*/
public static byte[] decode(byte[] bytes) {
if (bytes == null) {
return null;
}
int len = bytes.length;
//int key = 0x12;
int key = orKey;
for (int i = len - 1; i > 0; i--) {
bytes[i] = (byte) (bytes[i] ^ bytes[i - 1]);
}
bytes[0] = (byte) (bytes[0] ^ key);
return bytes;
}
}