Advertisement
Guest User

GoLang Public Key Encryption

a guest
Mar 22nd, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.79 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "crypto"
  5.     "crypto/rand"
  6.     "crypto/rsa"
  7.     "crypto/sha256"
  8.     "fmt"
  9.     "os"
  10. )
  11.  
  12. func main() {
  13.  
  14.     // Set up both public and private key to both jimena and Alisair.
  15.     jimenaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  16.  
  17.     if err != nil {
  18.         fmt.Println(err.Error())
  19.         os.Exit(1)
  20.     }
  21.  
  22.     jimenaPublicKey := &jimenaPrivateKey.PublicKey
  23.  
  24.     alistairPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  25.  
  26.     if err != nil {
  27.         fmt.Println(err.Error())
  28.         os.Exit(1)
  29.     }
  30.  
  31.     alistairPublicKey := &alistairPrivateKey.PublicKey
  32.  
  33.     fmt.Println("JIMENA PRIVATE KEY:")
  34.     fmt.Println("")
  35.     fmt.Println(jimenaPrivateKey)
  36.     fmt.Println("")
  37.  
  38.     fmt.Println("JIMENA PUBLIC KEY:")
  39.     fmt.Println("")
  40.     fmt.Println(jimenaPublicKey)
  41.     fmt.Println("")
  42.  
  43.     fmt.Println("ALISTAIR PRIVATE KEY:")
  44.     fmt.Println("")
  45.     fmt.Println(alistairPrivateKey)
  46.     fmt.Println("")
  47.  
  48.     fmt.Println("ALISTAIR PUBLIC KEY:")
  49.     fmt.Println("")
  50.     fmt.Println(alistairPublicKey)
  51.     fmt.Println("")
  52.  
  53.     // Get message, convert to bytes, hash the message and encrypt with alistairs public key.
  54.     message := []byte("The code must be like a piece of music")
  55.     label := []byte("")
  56.     hash := sha256.New()
  57.  
  58.     ciphertext, err := rsa.EncryptOAEP(hash, rand.Reader, alistairPublicKey, message, label)
  59.     if err != nil {
  60.         fmt.Println(err)
  61.         os.Exit(1)
  62.     }
  63.     fmt.Println("ENCRYPTED MESSAGE:")
  64.     fmt.Println("")
  65.     fmt.Printf("OAEP encrypted the message: %s to: \n[%x]\n", string(message), ciphertext)
  66.     fmt.Println("")
  67.  
  68.     // Next Jimena should sign your message with her
  69.     //private key to ensure the recipient can check
  70.     //the message sender with Jimena’s public key
  71.     //to confirm the sender is Jimena.
  72.  
  73.     var opts rsa.PSSOptions
  74.     opts.SaltLength = rsa.PSSSaltLengthAuto // for simple example
  75.     PSSmessage := message
  76.     newhash := crypto.SHA256
  77.     pssh := newhash.New()
  78.     pssh.Write(PSSmessage)
  79.     hashed := pssh.Sum(nil)
  80.  
  81.     signature, err := rsa.SignPSS(rand.Reader, jimenaPrivateKey, newhash, hashed, &opts)
  82.  
  83.     if err != nil {
  84.         fmt.Println(err)
  85.         os.Exit(1)
  86.     }
  87.     fmt.Println("PSS SIGNATURE:")
  88.     fmt.Println("")
  89.     fmt.Printf("PSS Signature : %x\n", signature)
  90.     // Now Alistair needs to decrypt the message
  91.     plainText, err := rsa.DecryptOAEP(hash, rand.Reader, alistairPrivateKey, ciphertext, label)
  92.  
  93.     if err != nil {
  94.         fmt.Println(err)
  95.         os.Exit(1)
  96.     }
  97.  
  98.     fmt.Println("")
  99.     fmt.Println("DECRYPTED MESSAGE:")
  100.     fmt.Println("")
  101.     fmt.Printf("OAEP decrypted [%x] to \n[%s]\n", ciphertext, plainText)
  102.  
  103.     // Last thing is for Alistair to check the message origin
  104.     // to determine that it is Jimena who sent the message
  105.     err = rsa.VerifyPSS(jimenaPublicKey, newhash, hashed, signature, &opts)
  106.  
  107.     if err != nil {
  108.         fmt.Println("Who are U?? Verify signature failed")
  109.         os.Exit(1)
  110.     } else {
  111.         fmt.Println("")
  112.         fmt.Println("Verify signature successful")
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement