-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSAEncryption
77 lines (68 loc) · 1.97 KB
/
RSAEncryption
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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/pem"
"flag"
"io/ioutil"
"log"
)
// Command-line flags
var (
keyFile = flag.String("key", "id_rsa", "Path to RSA private key")
inFile = flag.String("in", "in.txt", "Path to input file")
outFile = flag.String("out", "out.txt", "Path to output file")
label = flag.String("label", "", "Label to use (filename by default)")
decrypt = flag.Bool("decrypt", false, "Decrypt instead of encrypting")
)
func main() {
flag.Parse()
// Read the input file
in, err := ioutil.ReadFile(*inFile)
if err != nil {
log.Fatalf("input file: %s", err)
}
// Read the private key
pemData, err := ioutil.ReadFile(*keyFile)
if err != nil {
log.Fatalf("read key file: %s", err)
}
// Extract the PEM-encoded data block
block, _ := pem.Decode(pemData)
if block == nil {
log.Fatalf("bad key data: %s", "not PEM-encoded")
}
if got, want := block.Type, "RSA PRIVATE KEY"; got != want {
log.Fatalf("unknown key type %q, want %q", got, want)
}
// Decode the RSA private key
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatalf("bad private key: %s", err)
}
var out []byte
if *decrypt {
if *label == "" {
*label = *outFile
}
// Decrypt the data
out, err = rsa.DecryptOAEP(sha1.New(), rand.Reader, priv, in, []byte(*label))
if err != nil {
log.Fatalf("decrypt: %s", err)
}
} else {
if *label == "" {
*label = *inFile
}
out, err = rsa.EncryptOAEP(sha1.New(), rand.Reader, &priv.PublicKey, in, []byte(*label))
if err != nil {
log.Fatalf("encrypt: %s", err)
}
}
// Write data to output file
if err := ioutil.WriteFile(*outFile, out, 0600); err != nil {
log.Fatalf("write output: %s", err)
}
}