Last active
July 30, 2019 15:53
-
-
Save thales17/3fdc437b963af5665a7ac98c7679ed08 to your computer and use it in GitHub Desktop.
RSA Public/Private Key Node Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
const fs = require('fs'); | |
buffer = Buffer.from('albuquerque'); | |
fs.readFile('mykey.pub.pem', 'utf8', function(err, data) { | |
if (err) throw err; | |
console.log(data); | |
let key = {key: data, padding: crypto.constants.RSA_PKCS1_PADDING}; | |
let encrypted = crypto.publicEncrypt(key, buffer); | |
console.log(encrypted.toString('base64')); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
KEYNAME=mykey | |
rm -fv $KEYNAME* | |
ssh-keygen -t rsa -b 2048 -m pem -N "" -f $KEYNAME | |
ssh-keygen -e -m pem -f $KEYNAME.pub > $KEYNAME.pub.pem | |
rm -fv $KEYNAME.pub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/rand" | |
"crypto/rsa" | |
"crypto/x509" | |
"encoding/base64" | |
"encoding/pem" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
) | |
func main() { | |
cipherPtr := flag.String("cipher", "", "The encrypted message") | |
flag.Parse() | |
if len(*cipherPtr) == 0 { | |
log.Fatal("Please supply a cipher") | |
} | |
//message := "albuquerque" | |
privateKey, err := ioutil.ReadFile("mykey") | |
if err != nil { | |
log.Fatal("Read file", err) | |
} | |
cipherText, err := base64.StdEncoding.DecodeString(*cipherPtr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
/*publicKey, err := ioutil.ReadFile("mykey.pub.pem") | |
if err != nil { | |
log.Fatal("Read file", err) | |
} | |
block, _ := pem.Decode(publicKey) | |
if block == nil { | |
log.Fatal("public key error") | |
} | |
pub, err := x509.ParsePKCS1PublicKey(block.Bytes) | |
if err != nil { | |
log.Fatal("Parse Public Key", err) | |
} | |
body, err := rsa.EncryptPKCS1v15(rand.Reader, pub, []byte(message)) | |
if err != nil { | |
log.Fatal("Encrypt", err) | |
} | |
cipherText := string(body) | |
fmt.Println("Cipher:", cipherText) | |
*/ | |
block, _ := pem.Decode(privateKey) | |
if block == nil { | |
log.Fatal("private key error") | |
} | |
privInterface, err := x509.ParsePKCS1PrivateKey(block.Bytes) | |
if err != nil { | |
log.Fatal("Parse private key", err) | |
} | |
body, err := rsa.DecryptPKCS1v15(rand.Reader, privInterface, []byte(cipherText)) | |
if err != nil { | |
log.Fatal("Decrpyt", err) | |
} | |
fmt.Println("Decrpyted Cipher:", string(body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment