最简单的加密当然是混淆了. 任何一个字节,通过两次异或可以还原, 那么用这个原理就可以双方约定一组约定的字节序列进行混淆了

好了, 直接贴几个语言的实现

第一个luajit

local bit = require 'bit'
 
local function xor_encode(input, key)
    assert(#key > 0)
    --为了便于计算,这里使用0开始循环,由于lua语言的差异(末尾是闭区间), 所以减一
    local input_count, key_count = #input, #key
    local ret = ''
    for i = 1, input_count do
        local a = string.byte(input, i)
        local j = i%key_count
        local e = string.byte(key, j == 0 and key_count or j)
        local v =  bit.bxor(a, e)
        ret = ret .. string.char(v)
    end
    return ret
end
 
 
local t = xor_encode('hello!', 'ab')
print(xor_encode(t, 'ab'))

 贴个dart版本的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import 'dart:convert' show utf8;
 
// 将字符加密
List<int> xorEncode(String input,String key) {
   
  final keyLen = key.length;
  final count = input.length;
   
  List<int> inputBytes = utf8.encode(input);
  List<int> keyBytes = utf8.encode(key);
   
  List<int> ret = [];
  for(var i=0; i<count; i++){
    final j = i%keyLen;
    final v = inputBytes[i] ^ keyBytes[j];
    ret.add(v);
     
  }
  return ret;
}
 
// 将加密后的东西解开
String xorDecode(List<int> inputBytes, String key) {
  List<int> keyBytes = utf8.encode(key);
   
  final keyLen = key.length;
  final count = inputBytes.length;
   
  for(var i=0; i<count; i++){
    final j = i%keyLen;
    inputBytes[i] ^=  keyBytes[j];
  }
   
  return utf8.decode(inputBytes);
}
 
void main() async {
  final input = "hello!";
  final key = "ab";
  final encRet = xorEncode(input, key);
  final decRet = xorDecode(encRet, key);
  print(decRet);
}

  再来个js版本的

// 编码
function xorEncode(input, key) {
    let len = input.length
    let keyLen = key.length
    let results = []
    for(var i = 0; i < len; i++) {
        let j = i%keyLen
        let v = input[i].charCodeAt() ^ key[j].charCodeAt()
        results.push(v)
    }
    return results
}

// 解码
function xorDecode(input, key) {
    let len = input.length
    let keyLen = key.length
    let results = ''
    for(var i = 0; i < len; i++) {
        let j = i%keyLen
        let v = input[i] ^ key[j].charCodeAt()
        results += String.fromCharCode(v)
    }
    return results
}

let e = xorEncode('hello~!', 'ab')
console.log(xorDecode(e, 'ab'))

 

当然js可能得把uint8数组转成base64

// 使用utf-8字符集进行base64编码
function utoa(str) {
    return window.btoa(unescape(encodeURIComponent(str)));
}
// 使用utf-8字符集解析base64字符串 
function atou(str) {
    return decodeURIComponent(escape(window.atob(str)));
}

// 编码
function xorEncode(input, key) {
    let len = input.length
    let keyLen = key.length
    let str = ''
    for(var i = 0; i < len; i++) {
        let j = i%keyLen
        let v = input[i].charCodeAt() ^ key[j].charCodeAt()
        str += String.fromCharCode(v)
    }
    return window.btoa(str)
}

// 解码
function xorDecode(base64, key) {
    let bstr = window.atob(base64)
    let len = bstr.length
    let input = new Uint8Array(len)
    for(let i =0; i< len; i++){
        input[i] = bstr.charCodeAt(i)
    }

    let keyLen = key.length
    let results = ''
    for(var i = 0; i < len; i++) {
        let j = i%keyLen
        let v = input[i] ^ key[j].charCodeAt()
        results += String.fromCharCode(v)
    }
    return results
}

let e = xorEncode(encodeURIComponent('你好啊'), 'ab')
console.log( e)
console.log(decodeURIComponent(xorDecode(e, 'ab')))

 

posted @ 2021-09-06 03:17 冷侃 阅读(177) 评论(0) 推荐(0) 编辑
摘要: centos7默认是使用vi,而不是使用vim,所以,我们需要修改一下vi的别名,并且,我们使用neovim,vi毕竟还是有很多功能比较原始 所以 插件不能少,,省心点,我们用github最多星的 https://github.com/amix/vimrc 执行以下脚本,安装vim常用插件,具体插件 阅读全文
posted @ 2019-06-11 12:23 冷侃 阅读(1186) 评论(0) 推荐(0) 编辑
摘要: 我们都知道,所谓的随机都是伪随机,随机的结果是由随机算法和随机种子决定的。 所以,当我们没有初始化的时候,如果直接使用math.random(),那么出来的值肯定是每次都一样,因为种子等于0。 因此,我们都会在进程启动前,我们先调用一下种子 --随机种子初始化 math.randomseed(tos 阅读全文
posted @ 2019-03-18 22:21 冷侃 阅读(997) 评论(0) 推荐(0) 编辑
摘要: 阅读全文
posted @ 2018-11-09 13:49 冷侃 阅读(304) 评论(0) 推荐(0) 编辑
摘要: 不同程序之间经常会交换数据,我们经常采用的套路是: 假设要传输的信息是json,我们假设其为json_data,通过http传递信息为 json_data_encode=json_data&sign=md5(json_data+key) 接收方通过验证sign就知道内容有没有被篡改. 但是,这样js 阅读全文
posted @ 2018-09-26 22:26 冷侃 阅读(4089) 评论(0) 推荐(0) 编辑
摘要: 先安装一下包管理工具 yum install luarocks lua-devel -y luarocks install lpack ln -s /usr/lib64/lua /usr/local/lib 指定luarocks安装路径参数备忘 luarocks install lpack --tr 阅读全文
posted @ 2018-03-16 14:42 冷侃 阅读(758) 评论(0) 推荐(0) 编辑
摘要: 我们处理游戏Boss掉落时经常碰到一个问题,假设这个BOSS会掉3个部位的装备, 武器:20% 衣服:30% 头盔:50% 那么求,期望次数多少,可以集齐这三件装备 作为程序员,我们先来一段暴力破解,循环1000000万次,也便于我们验证解果 但是这出来也只是个近似值,得到一个靠谱值,这个时候容 阅读全文
posted @ 2017-07-14 17:51 冷侃 阅读(619) 评论(0) 推荐(0) 编辑
摘要: 编译时出现以下错误In file included from lj_ffrecord.c:859:0: lj_recdef.h:224:1: error: ‘recff_rawlen’ undeclared here (not in a function) recff_rawlen, ^ Makef 阅读全文
posted @ 2017-01-19 22:02 冷侃 阅读(1081) 评论(0) 推荐(0) 编辑
摘要: 现在编译方案都偏爱使用cmake解决问题,这两条做unity插件,还是用Makefile,居然忘得光光,好记性不如烂笔头。 后面,翻箱倒柜找到以前为炼金术写的Makefiel,发现还真是挺好用,贴出来,当万能Makefile模板挺好的。。 修改LIBNAME为你想要库名 在源码目录文件名为Makef 阅读全文
posted @ 2017-01-18 23:47 冷侃 阅读(3329) 评论(0) 推荐(0) 编辑
摘要: // <![CDATA[ // ]]> 阅读全文
posted @ 2016-09-25 22:38 冷侃 阅读(238) 评论(1) 推荐(0) 编辑
点击右上角即可分享
微信分享提示