Android 中数据加密 ---- 异或加密

前言:

对于异或加密,在博文 异或加密 已经有了详细说明,这边博文将其用Android 实现。

 

更多的加密可以看:

数据加密 ---- 总篇

Android 中数据加密 ---- AES加密

Android 中数据加密 ---- DES加密

Android 中数据加密 ---- 3DES加密

Android 中数据加密 ---- MD5加密

Android 中数据加密 ---- SHA-1加密

 

实例:

Activity 中添加两个调用的代码:

    private void testEncryptionXOR() {
        Button exor = (Button) findViewById(R.id.encrypt_xor);
        exor.setOnClickListener(this);

        Button dxor = (Button) findViewById(R.id.decrypt_xor);
        dxor.setOnClickListener(this);

        mXOREncryption = new XOREncryption(KEY_XOR);
    }

    private void xorEncryption() {
//        String strSource = "hliuhiufhliuhsd;jfijso;goshgosjogijsgo;j";
        String strSource = "呵呵哈哈";
        if (mXOREncryption != null) {
            Log.d(TAG, "==== strSource = " + strSource);
            mStrEncrypted = mXOREncryption.strEncrypt(strSource);
            Log.d(TAG, "==== strEncrypted = " + mStrEncrypted);
        }
    }

    private void xorDecrypted() {
        if (mXOREncryption != null) {
            String ret = mXOREncryption.strDecrypt(mStrEncrypted);
            Log.d(TAG, "==== strDecrypted = " + ret);
        }
    }

 

XOREncryption 代码(后期会进一步补充对于文件的XOR 加密):

public class XOREncryption {
    private final String mKey;

    public XOREncryption(String key) {
        mKey = key;

        if (TextUtils.isEmpty(mKey)) {
            throw new IllegalArgumentException("key is empty...");
        }
    }

    public String strEncrypt(final String strSource) {
        int i, j;
        StringBuilder sb = new StringBuilder();
        for (i = 0, j = 0; i < strSource.length(); i++) {
            j = i % mKey.length();
            sb.append((char) (strSource.charAt(i) ^ mKey.charAt(j)));
        }
        return sb.toString();
    }

    // 再次进行异或运算就可以解密
    public String strDecrypt(final String strSource) {
        return strEncrypt(strSource);
    }
}

运行的结果:

--------- beginning of main
--------- beginning of system
11-29 06:28:53.221  3633  3633 D TestEncryptionActivity: ==== strSource = 呵呵哈哈
11-29 06:28:53.221  3633  3633 D TestEncryptionActivity: ==== strEncrypted = 吔向咭咢
11-29 06:28:55.027  3633  3633 D TestEncryptionActivity: ==== strDecrypted = 呵呵哈哈

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

私房菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值