拿来主义之_异或加解密

异或,英文为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

算法

  1. a ⊕ a = 0
  2. a ⊕ b = b ⊕ a
  3. a ⊕b ⊕ c = a ⊕ (b ⊕ c) = (a ⊕ b) ⊕ c;
  4. d = a ⊕ b ⊕ c 可以推出 a = d ⊕ b ⊕ c.
  5. 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;
    }
}

感谢

博主在此给大家安利一款分享学习资料的app,此app的主要就是收集了互联网上的各种学习资料做整合并免费分享。里面涵盖了Android、java、python、数据结构、算法、前端、爬虫、大数据、深度学习、UI等等技术,包含M课网课程、某课堂VIP课程、各培训机构课程、某鱼的一些付费课程以及算法面试资料,并且正在不断更新中,你想要的学习资料几乎都有,当然你也可以加入我们的技术Q群获取资料:687667883,任何课程问题可直接咨询群主。
目前对接的课程论坛如下,您可以提前了解下有哪些课程:
链接资源如下:
    享学app资源对接论坛一(IT码上发)
    享学app资源对接论坛二(学途无忧)
download地址如下:
下载二维码_1.0.0版本.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值