forked from easonlin404/ksm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
63 lines (55 loc) · 1.71 KB
/
rsa.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
package rsa
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
)
// Encrypt encrypts the given message with RSA-OAEP.
// Need a DER encoded public key, These values are
// typically found in PEM blocks with "BEGIN PUBLIC KEY".
func Encrypt(publicKey, origData []byte) ([]byte, error) {
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, errors.New("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pub := pubInterface.(*rsa.PublicKey)
//fmt.Println("Modulus : ", pub.N.String())
//fmt.Println(">>> ", pub.N)
//fmt.Printf("Modulus(Hex) : %X\n", pub.N)
//fmt.Println("Public Exponent : ", pub.E)
//return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
return rsa.EncryptOAEP(sha256.New(), rand.Reader, pub, origData, nil)
}
func EncryptByCert(pemCertificate, origData []byte) ([]byte, error) {
block, _ := pem.Decode(pemCertificate)
if block == nil {
panic("failed to parse certificate PEM")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
panic("failed to parse certificate: " + err.Error())
}
pub := cert.PublicKey.(*rsa.PublicKey)
return rsa.EncryptOAEP(sha256.New(), rand.Reader, pub, origData, nil)
}
func Decrypt(privateKey, ciphertext []byte) ([]byte, error) {
fmt.Println(len(ciphertext))
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) // ASN.1 PKCS#1 DER encoded form.
if err != nil {
return nil, err
}
//return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
return rsa.DecryptOAEP(sha256.New(), rand.Reader, priv, ciphertext, nil)
}