当前位置:网站首页>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
边栏推荐
- nft数字藏品系统开发搭建流程
- Etcd introduction
- JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation
- Recommend several gravatar avatar caching plug-ins
- [file upload vulnerability 05] server suffix detection and bypass experiment (based on upload-labs-3 shooting range)
- The application of the spingboot+quartrz production environment supports distributed, custom corn, reflective execution of multiple tasks
- js面试题---箭头函数,find和filter some和every
- WordPress站内链接修改插件:Velvet Blues Update URLs
- How to understand CPU load
- 推荐几款Gravatar头像缓存插件
猜你喜欢

How to form a good habit? By perseverance? By determination? None of them!

MSF CS OpenSSL traffic encryption
![Set the default receiving address [project mall]](/img/eb/2864b124b66d01849315a367948ed4.png)
Set the default receiving address [project mall]
![my.cnf中 [mysql]与[mysqld] 的区别 引起的binlog启动失败的问题](/img/bd/a28e74654c7821b3a9cd9260d2e399.png)
my.cnf中 [mysql]与[mysqld] 的区别 引起的binlog启动失败的问题

Maximum water container

苹果MobileOne: 移动端仅需1ms的高性能骨干

Node connects to MySQL database and writes fuzzy query interface

2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!

Gerber文件在PCB制造中的作用

ELK - X-Pack设置用户密码
随机推荐
Is it safe for Xiaobai to open an account directly on the flush?
Node连接MySql数据库写模糊查询接口
WordPress landing page beautification plug-in: recommended by login Designer
Processing of uci-har datasets
Etcd介绍
How to form a good habit? By perseverance? By determination? None of them!
Web development model selection, who graduated from web development
typescript 编译选项和配置文件
Only when you find your own advantages can you work tirelessly and get twice the result with half the effort!
The complete manual of the strongest Flink operator is a good choice for the interview~
MYCAT sub database and sub table
JVM class loading process
WordPress重新生成特色图像插件:Regenerate Thumbnails
在毕设中学习03
Elk - x-pack set user password
Etcd introduction
File excel export
js面试题---箭头函数,find和filter some和every
Node connects to MySQL database and writes fuzzy query interface
2020-07 学习笔记整理