当前位置:网站首页>Golang uses XOR ^ to exchange two variables and encrypt / decrypt them
Golang uses XOR ^ to exchange two variables and encrypt / decrypt them
2022-06-11 11:44:00 【Flying fat Da Miao】
Swap two variables with XOR
// Not applicable to temporary variables tmp
// Using the properties of XOR Equivalent to 0 Different for 1
func swap(a, b int) (int, int) {
a ^= b // XOR equals operation Same as 0 Different for 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
}
You can use the XOR feature to encrypt files
encryption
func main() {
const key = 11
var (
d byte
s string = "abc"
ds string
dsSlice []byte
)
// encryption
for _, v := range s {
d = byte(v ^ key)
dsSlice = append(dsSlice, d)
fmt.Printf(" After encryption byte:%v, character :%s, Original character is :%s\n",d,string(d),string(v))
}
ds = string(dsSlice)
fmt.Printf(" Plaintext :%s Ciphertext :%s\n",s,ds)
// Decrypt
dsSlice = dsSlice[:0]
for _, v := range ds {
d = byte(v ^ 11)
dsSlice = append(dsSlice, d)
fmt.Printf(" After decryption byte:%v, character :%s, Original character is :%s\n",d,string(d),string(v))
}
s = string(dsSlice)
fmt.Printf(" Ciphertext :%s Plaintext :%s\n",ds,s)
}
// After encryption byte:106, character :j, Original character is :a
// After encryption byte:105, character :i, Original character is :b
// After encryption byte:104, character :h, Original character is :c
// Plaintext :abc Ciphertext :jih
// After decryption byte:97, character :a, Original character is :j
// After decryption byte:98, character :b, Original character is :i
// After decryption byte:99, character :c, Original character is :h
// Ciphertext :jih Plaintext :abc
边栏推荐
- Eulato
- WP super cache static cache plug-in concise tutorial
- Test cos HTML cache static cache plug-in
- MYCAT sub database and sub table
- Runtime reconfiguration of etcd
- 统计出现次数最多的前K个字符串
- Publish WordPress database cache plug-in: DB cache reloaded 3.1
- 中文输入法输入事件composition的使用
- Set the default receiving address [project mall]
- 全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据
猜你喜欢

It will be too late if you don't brush the questions. The most complete bat interview questions
![[第二章 基因和染色体的关系]生物知识概括–高一生物](/img/f0/9f78682798a7874ba176797a6b22ca.png)
[第二章 基因和染色体的关系]生物知识概括–高一生物

National multi-year solar radiation spatial distribution data 1981-2022, temperature distribution data, evapotranspiration data, evaporation data, rainfall distribution data, sunshine data, wind speed
![Définir l'adresse de réception par défaut [Centre commercial du projet]](/img/eb/2864b124b66d01849315a367948ed4.png)
Définir l'adresse de réception par défaut [Centre commercial du projet]

Guangdong municipal safety construction data management software 2022 new forms are coming

Elk - x-pack set user password

JS prototype. The find () method has no effect on the object array. It is urgent...

Iframe value transfer

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

2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!
随机推荐
17.4 creating multiple threads, data sharing problem analysis and case code
Gerber文件在PCB制造中的作用
Études à la fin de l'enseignement 03
ELK - ElastAlert最大的坑
Smart sidebar plug-in: Mo widgets
普通人应当如何挑选年金险产品?
The application of the spingboot+quartrz production environment supports distributed, custom corn, reflective execution of multiple tasks
Iframe value transfer
Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
nft数字藏品系统品台搭建
It will be too late if you don't brush the questions. The most complete bat interview questions
导师转我800块,让我仿真一个电路(电源设计)
2019年书单
[C language] anonymous/unnamed struct & Union
How to form a good habit? By perseverance? By determination? None of them!
在毕设中学习02——numpy多维数组的切片,形态变化,维度交换
202年最新热门收益较高的年金险产品是什么?
Test cos HTML cache static cache plug-in
Let WordPress support registered users to upload custom avatars
ELK - X-Pack设置用户密码