当前位置:网站首页>golang利用异或^交换两个变量以及加解密
golang利用异或^交换两个变量以及加解密
2022-06-11 11:26:00 【会飞的胖达喵】
使用异或交换两个变量
// 不适用临时变量tmp
// 利用异或的特性 形同为0 不同为1
func swap(a, b int) (int, int) {
a ^= b // 异或等于运算 相同为0 不同为1 0000 0010 (a=2) ^ 0000 0011 (b=3) = 0000 0001 (a=1)
b ^= a // 0000 0011 (b=3) ^ 0000 0001 (a=1) = 0000 0010 (b=2)
a ^= b // 0000 0001(a=1) ^ 0000 0010 (b=2) = 0000 0011 (a=3)
return a, b
}
可以利用异或的特性加密文件
加密
func main() {
const key = 11
var (
d byte
s string = "abc"
ds string
dsSlice []byte
)
//加密
for _, v := range s {
d = byte(v ^ key)
dsSlice = append(dsSlice, d)
fmt.Printf("加密之后byte:%v,字符:%s,原字符为:%s\n",d,string(d),string(v))
}
ds = string(dsSlice)
fmt.Printf("明文:%s 密文:%s\n",s,ds)
// 解密
dsSlice = dsSlice[:0]
for _, v := range ds {
d = byte(v ^ 11)
dsSlice = append(dsSlice, d)
fmt.Printf("解密之后byte:%v,字符:%s,原字符为:%s\n",d,string(d),string(v))
}
s = string(dsSlice)
fmt.Printf("密文:%s 明文:%s\n",ds,s)
}
//加密之后byte:106,字符:j,原字符为:a
//加密之后byte:105,字符:i,原字符为:b
//加密之后byte:104,字符:h,原字符为:c
// 明文:abc 密文:jih
//解密之后byte:97,字符:a,原字符为:j
//解密之后byte:98,字符:b,原字符为:i
//解密之后byte:99,字符:c,原字符为:h
// 密文:jih 明文:abc
边栏推荐
- 使用Yolov3训练自己制作数据集,快速上手
- 找到自己的优势,才能干活不累,事半功倍!
- Only when you find your own advantages can you work tirelessly and get twice the result with half the effort!
- Digital collection app applet official account source code
- 小白在同花顺上直接开户是安全的吗?
- Method of converting VOC format data set to Yolo format data set
- 数字藏品系统源码搭建
- National multi-year solar radiation spatial distribution data 1981-2022, temperature distribution data, evapotranspiration data, evaporation data, rainfall distribution data, sunshine data, wind speed
- Études à la fin de l'enseignement 03
- 使用Labelimg制作VOC数据集或yolo数据集的入门方法
猜你喜欢

Iterator mode -- battlefield autumn point

木瓜移动CFO刘凡 释放数字时代女性创新力量

js中Array.prototype.find() 方法在对象数组上无效果,捉急。。。

Appearance mode -- it has been used in various packages for a long time!

Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO

Only when you find your own advantages can you work tirelessly and get twice the result with half the effort!

Display of receiving address list 【 project mall 】

外观模式--在各种套餐中早就用到啦!

JS interview questions - arrow function, find and filter some and every

The complete manual of the strongest Flink operator is a good choice for the interview~
随机推荐
VOC格式数据集转yolo格式数据集的方法
Node connects to MySQL database and writes fuzzy query interface
适配器模式--能不能好好说话?
Don't be a fake worker
Runtime reconfiguration of etcd
Bark – 自己给自己的 iPhone 发推送提醒 – 最简单的推送提醒服务,开源免费
【碎碎念】关于波长|波速|周期的想法
AcWing 1353. Ski resort design (greedy)
【Go】Gin源码解读
全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据
JVM class loading process
2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!
导师转我800块,让我仿真一个电路(电源设计)
发布WordPress数据库缓存插件:DB Cache Reloaded 3.1
The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)
JVM-类加载过程
Iterator mode -- battlefield autumn point
Cap theory sounds very big, but it's actually very simple
The application of the spingboot+quartrz production environment supports distributed, custom corn, reflective execution of multiple tasks
命令模式--进攻,秘密武器