-
Notifications
You must be signed in to change notification settings - Fork 2
/
crypto.go
66 lines (61 loc) · 1.75 KB
/
crypto.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
package main
import (
"crypto/rsa"
"math/big"
)
var (
publicKey = rsa.PublicKey{
E: 0x10001,
N: new(big.Int).SetBytes([]byte{
0x51, 0xbc, 0xda, 0x08, 0x6d, 0x39, 0xfc, 0xe4,
0x56, 0x51, 0x60, 0xd6, 0x51, 0x71, 0x3f, 0xa2,
0xe8, 0xaa, 0x54, 0xfa, 0x66, 0x82, 0xb0, 0x4a,
0xab, 0xdd, 0x0e, 0x6a, 0xf8, 0xb0, 0xc1, 0xe6,
0xd1, 0xfb, 0x4f, 0x3d, 0xaa, 0x43, 0x7f, 0x15,
}),
}
privateKey = rsa.PrivateKey{
PublicKey: publicKey,
D: new(big.Int).SetBytes([]byte{
0x0a, 0x56, 0x37, 0xbc, 0x99, 0x13, 0x9c, 0x47,
0xc4, 0x22, 0xc6, 0x7c, 0x54, 0x10, 0x5e, 0x5b,
0xdb, 0xd0, 0xae, 0xae, 0x4a, 0xb4, 0xd4, 0x33,
0x43, 0x58, 0x27, 0x4e, 0x1b, 0xdf, 0x57, 0x06,
0xa1, 0xfb, 0xf4, 0xe6, 0x82, 0x89, 0x30, 0x81,
}),
Primes: []*big.Int{
new(big.Int).SetBytes([]byte{
0x01, 0x45, 0xd2, 0x58, 0x2f, 0x15, 0x95, 0x27,
0x04, 0xba, 0x91, 0x87, 0x8c, 0x88, 0xe4, 0x86,
0x46, 0x4d, 0x67, 0x75, 0x3f,
}),
new(big.Int).SetBytes([]byte{
0x40, 0x38, 0xc7, 0x8c, 0x87, 0x1a, 0x1b, 0x97,
0x90, 0xbb, 0xcd, 0x71, 0x3c, 0x28, 0xdb, 0xab,
0x84, 0xe6, 0x52, 0xab,
}),
},
}
)
// RSA transform function.
// To encrypt: rsatransform(block, E, N)
// To decrypt: rsatransform(block, D, N)
func rsatransform(block []byte, y, m *big.Int) []byte {
x := new(big.Int).SetBytes(block)
return new(big.Int).Exp(x, y, m).Bytes()
}
func blowfishKeyFromKeySource(keySource []byte) []byte {
ks := [80]byte{}
copy(ks[:], keySource)
byteswap(ks[:])
E := big.NewInt(int64(publicKey.E))
s0 := rsatransform(ks[:40], E, publicKey.N)
s1 := rsatransform(ks[40:], E, publicKey.N)
a := new(big.Int).SetBytes(s0)
b := new(big.Int).SetBytes(s1)
c := new(big.Int).Lsh(a, 312)
d := new(big.Int).Add(b, c)
key := d.Bytes()
byteswap(key)
return key
}