Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.63 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "crypto/rand"
  5.     "crypto/rsa"
  6.     "crypto/sha512"
  7.     "crypto/x509"
  8.     "encoding/pem"
  9.     "fmt"
  10.     "log"
  11. )
  12.  
  13. // GenerateKeyPair generates a new key pair
  14. func GenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey) {
  15.     privkey, err := rsa.GenerateKey(rand.Reader, bits)
  16.     if err != nil {
  17.         log.Fatal(err)
  18.  
  19.     }
  20.     return privkey, &privkey.PublicKey
  21. }
  22.  
  23. // PrivateKeyToBytes private key to bytes
  24. func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte {
  25.     privBytes := pem.EncodeToMemory(
  26.         &pem.Block{
  27.             Type:  "RSA PRIVATE KEY",
  28.             Bytes: x509.MarshalPKCS1PrivateKey(priv),
  29.         },
  30.     )
  31.  
  32.     return privBytes
  33. }
  34.  
  35. // PublicKeyToBytes public key to bytes
  36. func PublicKeyToBytes(pub *rsa.PublicKey) []byte {
  37.     pubASN1, err := x509.MarshalPKIXPublicKey(pub)
  38.     if err != nil {
  39.         log.Fatal(err)
  40.     }
  41.  
  42.     pubBytes := pem.EncodeToMemory(&pem.Block{
  43.         Type:  "RSA PUBLIC KEY",
  44.         Bytes: pubASN1,
  45.     })
  46.  
  47.     return pubBytes
  48. }
  49.  
  50. // BytesToPrivateKey bytes to private key
  51. func BytesToPrivateKey(priv []byte) *rsa.PrivateKey {
  52.     block, _ := pem.Decode(priv)
  53.     enc := x509.IsEncryptedPEMBlock(block)
  54.     b := block.Bytes
  55.     var err error
  56.     if enc {
  57.         log.Println("is encrypted pem block")
  58.         b, err = x509.DecryptPEMBlock(block, nil)
  59.         if err != nil {
  60.             log.Fatal(err)
  61.         }
  62.     }
  63.     key, err := x509.ParsePKCS1PrivateKey(b)
  64.     if err != nil {
  65.         log.Fatal(err)
  66.     }
  67.     return key
  68. }
  69.  
  70. // BytesToPublicKey bytes to public key
  71. func BytesToPublicKey(pub []byte) *rsa.PublicKey {
  72.     block, _ := pem.Decode(pub)
  73.     enc := x509.IsEncryptedPEMBlock(block)
  74.     b := block.Bytes
  75.     var err error
  76.     if enc {
  77.         log.Println("is encrypted pem block")
  78.         b, err = x509.DecryptPEMBlock(block, nil)
  79.         if err != nil {
  80.             log.Fatal(err)
  81.         }
  82.     }
  83.     ifc, err := x509.ParsePKIXPublicKey(b)
  84.     if err != nil {
  85.         log.Fatal(err)
  86.     }
  87.     key, ok := ifc.(*rsa.PublicKey)
  88.     if !ok {
  89.         log.Fatal("not ok")
  90.     }
  91.     return key
  92. }
  93.  
  94. // EncryptWithPublicKey encrypts data with public key
  95. func EncryptWithPublicKey(msg []byte, pub *rsa.PublicKey) []byte {
  96.     hash := sha512.New()
  97.     ciphertext, err := rsa.EncryptOAEP(hash, rand.Reader, pub, msg, nil)
  98.     if err != nil {
  99.         log.Fatal(err)
  100.     }
  101.     return ciphertext
  102. }
  103.  
  104. // DecryptWithPrivateKey decrypts data with private key
  105. func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) []byte {
  106.     hash := sha512.New()
  107.     plaintext, err := rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext, nil)
  108.     if err != nil {
  109.         log.Fatal(err)
  110.     }
  111.     return plaintext
  112. }
  113.  
  114. func main() {
  115.     public_k, private_k := GenerateKeyPair(2048)
  116.     fmt.Println("Private Key : ", private_k)
  117.     fmt.Println("Public key ", public_k)
  118.     fmt.Println(EncryptWithPublicKey([]byte("kek"), *public_k))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement