Skip to content

Instantly share code, notes, and snippets.

@Frederic-Zhou
Last active October 21, 2016 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Frederic-Zhou/27deec9f2dd21b9b10d90e46dd177fd6 to your computer and use it in GitHub Desktop.
Save Frederic-Zhou/27deec9f2dd21b9b10d90e46dd177fd6 to your computer and use it in GitHub Desktop.
Example with XOR algorithm for reversible encryption, 用异或算法进行可逆加密算法范例
package tests
import (
"fmt"
"testing"
)
// 可逆加密简单算法测试,采用异或方式
func TestPwd(t *testing.T) {
pwd := "Hello world!"
key := "test"
pwdBytes := []byte(pwd)
keyBytes := []byte(key)
hashBytes := []byte{}
cpwdBytes := []byte{}
keyLen := len(keyBytes)
var cpwd string
for i, v := range pwdBytes {
hashBytes = append(hashBytes, v^keyBytes[i%keyLen])//获得密码和key的异或值
}
fmt.Println("hashBytes: ", hashBytes)
fmt.Println("hashPwdStr: ", string(hashBytes))
for i, v := range hashBytes {
cpwdBytes = append(cpwdBytes, v^keyBytes[i%keyLen])//通过key与异或值 异或,得到密码值
}
cpwd = string(cpwdBytes)
fmt.Println("cpwdBytes: ", cpwdBytes)
fmt.Println("pwd: ", cpwd)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment