異或,英文為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;
}
}