Skip to content

Commit

Permalink
Merge pull request #8 from ieee0824/aes
Browse files Browse the repository at this point in the history
RSA暗号用のencryptorを実装
  • Loading branch information
ieee0824 authored Sep 9, 2017
2 parents 118aafd + a42a754 commit 201d2e9
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/rsa/private-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAyQCoER5mc2RKvTkdeYlBv33amEXXT5DGIlTZfj+0Tv1FZcPV
YZw7dRlRWvXNIfr9GSk4J18ceZdII0rZBe+Gce+Vy8bme72apbnPAvfYzs5u5xvh
W/W0YYtxBOkdKjZOpDQEOoLZBCIqW/+VrnQrI5TbFArOedBvH6qUdUDouKvf4dvD
KQGG2nFeJ4BMVRbVHHgr3PDCZTLqgccaZxavco1UiQYThZ/d3nh/4o3VPPXx8gP+
O99W7t20qvLlk9475Eb5ptLdkXHdjKoqF4cuU6q0XQ/fdmMkZRHQ7rGqrX2hNggJ
nGJaxCW4vZ4917d0CEZcaBpeQiuOD/nLlFb3HQIDAQABAoIBAQCxAmT/flgWsnGn
Jc8xIpDGBB0nEApGxWXXAgXuykcNcLlWnxkgtqcdvkgI5IHbtN6C8+vBsF6xLWqX
uehY89DgMW8nX2fF/PldjL4jDQTfSqgb2k+h3kUVyeijvG60p9KlhDFdHbHjk/rd
vbyhPyhmU0aC5eljGQ1jQ6MthxXq2dA1Dej5IbPYNm65bj0eNCSCvPql0BRN0jZZ
OgvWdPIUNfaLV2MPAhrod1WMZM9s4ZrWrc5P+K66CuyDhZ3ry2tI1jO/UeQzyOuE
xQg9vXvhQ8kN6KS5C6A7E5gBwdAm28qULKJ+oBT1jgMOTdoX9dOJKJbnWpUnuCbj
G5EYIsBBAoGBAPjhjwbp4Jy0kyJaUiLJXGcfI+FQAsvJcZU0s/TTFZMWs46xJ9an
sIBjD2/lKAI6cYIbbPliqaRs3b0aGOLJckHb0Ll9FI99lP3um8ceGc7Ft8QNsBx7
5kZhx0CTV1e15JKf2j0QkQHUfSbR0B9GFlJWS+LEwGMLLiSfUkZbkyfVAoGBAM7A
gWeAxf81dnGZDFDTe5vMFjZdXfso7CL5+/GLqWyWAsykSl5fSGFmkhDQ6lGB7D3W
kb62dNKVVQFSgx5iFsfPslDaJsIW5E1JQvZnTws6qpm35b4XfJGeY7fa2jausYta
ao5IFSTarGiY2GZG06NqlNYGPhQmmUdJ6UhdoD4pAoGAG9QcygkctGj8irrOtFUP
n/g7atYneeHscWKTPKzdnLTNHwdwDPBzK+fnsftNnfIq/wFSDhCaJ2iE4sAoSDN+
Mx8BF7yEsAa6wioMARe8iwJQqftd1dGEzhngzber7ILKLBrzrCBOFoZL+Cs9ukF7
/Xjow1J8sJZvcmjeKj/TFIECgYBDSy8TVDudtHYHxWvXxu3dqNC2Ay+6tvgc7kbN
uMp9WFoy52p5SwQRB8SVCpqpyuLswbEQZOyjOg2/725T7lNB191PKaIh3DeU3Dqa
XsfAfOjYPZ3egGCAORB14BrUX4nUNKqoAtg29aHWLaGJsJXkA6XL/54nCuUq6eiv
MCIe+QKBgQC1z2BZ35MoKRefL7FnvXTQAe4hDSvXWzZGbhS/6fl/fnnRcQKmM7l9
iWtCzhxwxVQK9AukkCRDdVvJyzsd4j9nfUwNrjgKayvew231XtFNrjDy4MVHGot3
qarOUbyqHfPGEV3eIRog7YJfjGkVfNLPuNggPND6IJPnidWSxz8DrA==
-----END RSA PRIVATE KEY-----
9 changes: 9 additions & 0 deletions examples/rsa/public-key.pem
Original file line number Diff line number Diff line change
@@ -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-----
57 changes: 57 additions & 0 deletions examples/rsa/rsa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"encoding/json"
"fmt"
"github.com/ieee0824/cryptex"
"github.com/ieee0824/cryptex/rsa"
"io/ioutil"
"log"
)

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))
}
96 changes: 96 additions & 0 deletions rsa/rsa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package rsa

import (
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
)

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 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 {
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) {
priKey, err := decodePrivateKey(r.privateKey)
if err != nil {
return nil, err
}

out, err := rsa.DecryptOAEP(sha512.New(), rand.Reader, priKey, c, []byte(""))
if err != nil {
return nil, err
}
return out, nil
}

0 comments on commit 201d2e9

Please sign in to comment.