From 53726e583ea449c500b240d166ac9dc972300e66 Mon Sep 17 00:00:00 2001 From: shirase_aoi Date: Sun, 10 Sep 2017 01:15:10 +0900 Subject: [PATCH 1/4] =?UTF-8?q?rsa=E6=9A=97=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rsa/rsa.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 rsa/rsa.go diff --git a/rsa/rsa.go b/rsa/rsa.go new file mode 100644 index 0000000..1e9fe8b --- /dev/null +++ b/rsa/rsa.go @@ -0,0 +1,87 @@ +package rsa + +import ( + "encoding/pem" + "fmt" + "crypto/x509" + "crypto/rsa" + "crypto/sha512" + "errors" + "crypto/rand" + "crypto/sha1" +) + +var ( + NotPemEncodeErr = errors.New("not PEM-encoded") + UnknownKeyTypeErr = errors.New("Unknown key type error") + BadPrivateKeyErr = errors.New("bad private key") + InvalidPubKeyErr = errors.New("invalid public key data") + BadPublicKeyErr = errors.New("not RSA public key") +) + +func decodePublicKey(key []byte) (*rsa.PublicKey, error) { + publicKeyBlock, _ := pem.Decode(key) + if publicKeyBlock == nil { + return nil,InvalidPubKeyErr + } + if publicKeyBlock.Type != "PUBLIC KEY" { + return nil, errors.New(fmt.Sprintf("invalid public key type : %s", publicKeyBlock.Type)) + } + + publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes) + if err != nil { + return nil, err + } + + publicKey, ok := publicKeyInterface.(*rsa.PublicKey) + if !ok { + return nil, BadPublicKeyErr + } + + return publicKey, nil +} + +type RSA struct { + privateKey []byte + publicKey []byte +} + +func New(pri, pub []byte) *RSA { + return &RSA{ + privateKey: pri, + publicKey: pub, + } +} + +func (r *RSA) Encrypt(p []byte) ([]byte, error) { + pubkey, err := decodePublicKey(r.publicKey) + if err != nil { + return nil, err + } + + out, err := rsa.EncryptOAEP(sha512.New(), rand.Reader, pubkey, p, []byte("")) + if err != nil { + return nil, err + } + return out, nil +} + +func (r *RSA) Decrypt(c []byte) ([]byte, error) { +block, _ := pem.Decode(r.privateKey) + if block == nil { + return nil, NotPemEncodeErr + } + got, want := block.Type, "RSA PRIVATE KEY" + if got != want { + return nil, errors.New(fmt.Sprintf("%v: %q, want: %q", UnknownKeyTypeErr.Error(), got, want)) + } + priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) + if err != nil { + return nil, BadPrivateKeyErr + } + out, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, priv, c, []byte("")) + if err != nil { + return nil, err + } + return out, nil +} \ No newline at end of file From d4af1c8556a363787c2b83c27e185c50541200f0 Mon Sep 17 00:00:00 2001 From: shirase_aoi Date: Sun, 10 Sep 2017 01:29:14 +0900 Subject: [PATCH 2/4] rsa sample --- examples/rsa/private-key.pem | 27 +++++++++++++++++ examples/rsa/public-key.pem | 9 ++++++ examples/rsa/rsa.go | 57 ++++++++++++++++++++++++++++++++++++ rsa/rsa.go | 33 +++++++++++++-------- 4 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 examples/rsa/private-key.pem create mode 100644 examples/rsa/public-key.pem create mode 100644 examples/rsa/rsa.go diff --git a/examples/rsa/private-key.pem b/examples/rsa/private-key.pem new file mode 100644 index 0000000..a412d6b --- /dev/null +++ b/examples/rsa/private-key.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAyQCoER5mc2RKvTkdeYlBv33amEXXT5DGIlTZfj+0Tv1FZcPV +YZw7dRlRWvXNIfr9GSk4J18ceZdII0rZBe+Gce+Vy8bme72apbnPAvfYzs5u5xvh +W/W0YYtxBOkdKjZOpDQEOoLZBCIqW/+VrnQrI5TbFArOedBvH6qUdUDouKvf4dvD +KQGG2nFeJ4BMVRbVHHgr3PDCZTLqgccaZxavco1UiQYThZ/d3nh/4o3VPPXx8gP+ +O99W7t20qvLlk9475Eb5ptLdkXHdjKoqF4cuU6q0XQ/fdmMkZRHQ7rGqrX2h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AORB14BrUX4nUNKqoAtg29aHWLaGJsJXkA6XL/54nCuUq6eiv +MCIe+QKBgQC1z2BZ35MoKRefL7FnvXTQAe4hDSvXWzZGbhS/6fl/fnnRcQKmM7l9 +iWtCzhxwxVQK9AukkCRDdVvJyzsd4j9nfUwNrjgKayvew231XtFNrjDy4MVHGot3 +qarOUbyqHfPGEV3eIRog7YJfjGkVfNLPuNggPND6IJPnidWSxz8DrA== +-----END RSA PRIVATE KEY----- diff --git a/examples/rsa/public-key.pem b/examples/rsa/public-key.pem new file mode 100644 index 0000000..a0b8fc5 --- /dev/null +++ b/examples/rsa/public-key.pem @@ -0,0 +1,9 @@ +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyQCoER5mc2RKvTkdeYlB +v33amEXXT5DGIlTZfj+0Tv1FZcPVYZw7dRlRWvXNIfr9GSk4J18ceZdII0rZBe+G +ce+Vy8bme72apbnPAvfYzs5u5xvhW/W0YYtxBOkdKjZOpDQEOoLZBCIqW/+VrnQr +I5TbFArOedBvH6qUdUDouKvf4dvDKQGG2nFeJ4BMVRbVHHgr3PDCZTLqgccaZxav +co1UiQYThZ/d3nh/4o3VPPXx8gP+O99W7t20qvLlk9475Eb5ptLdkXHdjKoqF4cu +U6q0XQ/fdmMkZRHQ7rGqrX2hNggJnGJaxCW4vZ4917d0CEZcaBpeQiuOD/nLlFb3 +HQIDAQAB +-----END PUBLIC KEY----- diff --git a/examples/rsa/rsa.go b/examples/rsa/rsa.go new file mode 100644 index 0000000..4bdf882 --- /dev/null +++ b/examples/rsa/rsa.go @@ -0,0 +1,57 @@ +package main + +import ( + "github.com/ieee0824/cryptex/rsa" + "io/ioutil" + "log" + "github.com/ieee0824/cryptex" + "fmt" + "encoding/json" +) + +func main() { + priBin, err := ioutil.ReadFile("private-key.pem") + if err != nil { + log.Fatalln(err) + } + pubBin, err := ioutil.ReadFile("public-key.pem") + if err != nil { + log.Fatalln(err) + } + + encryptor := rsa.New(priBin, pubBin) + plainMap := map[string]interface{}{ + "hoge": "huga", + "foo": "bar", + "int": 0, + "float": 1.1, + "sub_map": map[string]interface{}{ + "alice": 12, + "bob": 25, + }, + } + c := cryptex.New(encryptor) + if err != nil { + log.Fatalln(err) + } + cipher, err := c.Encrypt(plainMap) + if err != nil { + log.Fatalln(err) + } + binEncrypted, err := json.MarshalIndent(cipher, "", " ") + if err != nil { + log.Fatalln(err) + } + + fmt.Println(string(binEncrypted)) + + plain, err := c.Decrypt(cipher) + if err != nil { + log.Fatalln(err) + } + binPlain, err := json.MarshalIndent(plain, "", " ") + if err != nil { + log.Fatalln(err) + } + fmt.Println(string(binPlain)) +} \ No newline at end of file diff --git a/rsa/rsa.go b/rsa/rsa.go index 1e9fe8b..c3f2760 100644 --- a/rsa/rsa.go +++ b/rsa/rsa.go @@ -8,7 +8,6 @@ import ( "crypto/sha512" "errors" "crypto/rand" - "crypto/sha1" ) var ( @@ -19,6 +18,23 @@ var ( BadPublicKeyErr = errors.New("not RSA public key") ) +func decodePrivateKey(key []byte) (*rsa.PrivateKey, error) { + privateKeyBlock, _ := pem.Decode(key) + if privateKeyBlock == nil { + return nil, BadPrivateKeyErr + } + if privateKeyBlock.Type != "RSA PRIVATE KEY" { + return nil, BadPrivateKeyErr + } + + privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes) + if err != nil { + return nil, err + } + + return privateKey, err +} + func decodePublicKey(key []byte) (*rsa.PublicKey, error) { publicKeyBlock, _ := pem.Decode(key) if publicKeyBlock == nil { @@ -67,19 +83,12 @@ func (r *RSA) Encrypt(p []byte) ([]byte, error) { } func (r *RSA) Decrypt(c []byte) ([]byte, error) { -block, _ := pem.Decode(r.privateKey) - if block == nil { - return nil, NotPemEncodeErr - } - got, want := block.Type, "RSA PRIVATE KEY" - if got != want { - return nil, errors.New(fmt.Sprintf("%v: %q, want: %q", UnknownKeyTypeErr.Error(), got, want)) - } - priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) + priKey, err := decodePrivateKey(r.privateKey) if err != nil { - return nil, BadPrivateKeyErr + return nil, err } - out, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, priv, c, []byte("")) + + out, err := rsa.DecryptOAEP(sha512.New(), rand.Reader, priKey, c, []byte("")) if err != nil { return nil, err } From 859f35ffcbcf2f1391c41bada192cd2e0734efaa Mon Sep 17 00:00:00 2001 From: shirase_aoi Date: Sun, 10 Sep 2017 01:30:45 +0900 Subject: [PATCH 3/4] fmt --- examples/rsa/rsa.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/rsa/rsa.go b/examples/rsa/rsa.go index 4bdf882..92ac62b 100644 --- a/examples/rsa/rsa.go +++ b/examples/rsa/rsa.go @@ -1,12 +1,12 @@ package main import ( + "encoding/json" + "fmt" + "github.com/ieee0824/cryptex" "github.com/ieee0824/cryptex/rsa" "io/ioutil" "log" - "github.com/ieee0824/cryptex" - "fmt" - "encoding/json" ) func main() { @@ -54,4 +54,4 @@ func main() { log.Fatalln(err) } fmt.Println(string(binPlain)) -} \ No newline at end of file +} From a42a754070571c7bd8d9be507ee79cedb6024d19 Mon Sep 17 00:00:00 2001 From: shirase_aoi Date: Sun, 10 Sep 2017 01:31:40 +0900 Subject: [PATCH 4/4] fmt --- rsa/rsa.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rsa/rsa.go b/rsa/rsa.go index c3f2760..67a13ed 100644 --- a/rsa/rsa.go +++ b/rsa/rsa.go @@ -1,21 +1,21 @@ package rsa import ( - "encoding/pem" - "fmt" - "crypto/x509" + "crypto/rand" "crypto/rsa" "crypto/sha512" + "crypto/x509" + "encoding/pem" "errors" - "crypto/rand" + "fmt" ) var ( - NotPemEncodeErr = errors.New("not PEM-encoded") + NotPemEncodeErr = errors.New("not PEM-encoded") UnknownKeyTypeErr = errors.New("Unknown key type error") - BadPrivateKeyErr = errors.New("bad private key") - InvalidPubKeyErr = errors.New("invalid public key data") - BadPublicKeyErr = errors.New("not RSA public key") + BadPrivateKeyErr = errors.New("bad private key") + InvalidPubKeyErr = errors.New("invalid public key data") + BadPublicKeyErr = errors.New("not RSA public key") ) func decodePrivateKey(key []byte) (*rsa.PrivateKey, error) { @@ -38,7 +38,7 @@ func decodePrivateKey(key []byte) (*rsa.PrivateKey, error) { func decodePublicKey(key []byte) (*rsa.PublicKey, error) { publicKeyBlock, _ := pem.Decode(key) if publicKeyBlock == nil { - return nil,InvalidPubKeyErr + return nil, InvalidPubKeyErr } if publicKeyBlock.Type != "PUBLIC KEY" { return nil, errors.New(fmt.Sprintf("invalid public key type : %s", publicKeyBlock.Type)) @@ -59,13 +59,13 @@ func decodePublicKey(key []byte) (*rsa.PublicKey, error) { type RSA struct { privateKey []byte - publicKey []byte + publicKey []byte } func New(pri, pub []byte) *RSA { return &RSA{ privateKey: pri, - publicKey: pub, + publicKey: pub, } } @@ -93,4 +93,4 @@ func (r *RSA) Decrypt(c []byte) ([]byte, error) { return nil, err } return out, nil -} \ No newline at end of file +}