/
pkcs_rsa2048_go.go
96 lines (86 loc) · 1.81 KB
/
pkcs_rsa2048_go.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"crypto/rand"
"crypto/rsa"
"encoding/hex"
"fmt"
"log"
"math/big"
"os"
)
// A helper method to use the prime in hex form, from crypto/cipher
func fromBase16(base16 string) *big.Int {
i, ok := new(big.Int).SetString(base16, 16)
if !ok {
panic("bad number: " + base16)
}
return i
}
var encrypting bool
func main() {
var test2048Key *rsa.PrivateKey
switch len(os.Args) {
case 4:
encrypting = true
case 6:
encrypting = false
default:
log.Fatal("Please provide N,E,Plain or P1,P2,E,D,Cipher as arguments")
}
var plaintext string
var Nn, Dd, P1, P2 *big.Int
var Ee int
if encrypting {
plaintext = os.Args[3]
Nn = fromBase16(os.Args[1])
Ee = int(fromBase16(os.Args[2]).Int64())
} else {
P1 = fromBase16(os.Args[1])
P2 = fromBase16(os.Args[2])
Nn = (new(big.Int).Mul(P1, P2))
Ee = int(fromBase16(os.Args[3]).Int64())
Dd = fromBase16(os.Args[4])
plaintext = os.Args[5]
}
pubKey := rsa.PublicKey{
N: Nn,
E: Ee,
}
if !encrypting {
test2048Key = &rsa.PrivateKey{
PublicKey: rsa.PublicKey{
N: Nn,
E: Ee,
},
D: Dd,
Primes: []*big.Int{
P1,
P2,
},
}
test2048Key.Precompute()
}
// According to the doc, the size of the plaintext shouldn't be
// bigger than that of the public modulus - 11 bytes !
// TODO: Check this
rng := rand.Reader
if encrypting {
secretMessage := []byte(plaintext)
ciphertext, err := rsa.EncryptPKCS1v15(rng, &pubKey, secretMessage)
if err != nil {
log.Fatalln(err, "FAIL")
}
fmt.Printf("%x\n", ciphertext)
} else {
// Let's decrypt it :
message, err := hex.DecodeString(plaintext)
if err != nil {
log.Fatalln(err, "FAIL")
}
newplaintext, err2 := rsa.DecryptPKCS1v15(rng, test2048Key, message)
if err2 != nil {
log.Fatalln(err2, "FAIL")
}
fmt.Printf("%s\n", string(newplaintext))
}
}