-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
70 lines (58 loc) · 1.37 KB
/
encoder.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"crypto/rc4"
"encoding/hex"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)
func g() string {
rand.Seed(time.Now().UnixNano())
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
length := rand.Intn(10) + 16
var key = strings.Builder{}
for i := 0; i < length; i++ {
key.WriteByte(chars[rand.Intn(len(chars))])
}
return key.String()
}
func reverseString(str string) string {
runes := []rune(str)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func main() {
key := g()
fmt.Println("密钥:", key)
fileName := reverseString(key) + ".tmp"
fmt.Println("文件名:", fileName)
file, err := os.Open("1.bin")
if err != nil {
panic(err)
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
xordMessage := make([]byte, len(content))
for i := 0; i < len(content); i++ {
xordMessage[i] = content[i] ^ 0xff
}
cipher, _ := rc4.NewCipher([]byte(key))
rc4Message := make([]byte, len(xordMessage))
cipher.XORKeyStream(rc4Message, xordMessage)
hexCiphertext := make([]byte, hex.EncodedLen(len(rc4Message)))
n := hex.Encode(hexCiphertext, rc4Message)
hexCiphertext = hexCiphertext[:n]
err = ioutil.WriteFile(fileName, hexCiphertext, 0644)
if err != nil {
panic(err)
}
fmt.println("完成!")
}