Skip to content

Instantly share code, notes, and snippets.

@d3rrick
Forked from hothero/rsa_ecb_pkcs1.go
Created February 1, 2021 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3rrick/dbff765336f151d01b1ddcd8213d4861 to your computer and use it in GitHub Desktop.
Save d3rrick/dbff765336f151d01b1ddcd8213d4861 to your computer and use it in GitHub Desktop.
RSA/ECB/PKCS1Padding imnplementation by Golang (can work with JAVA, C#, etc.)
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/hothero/playground/rsa_generation/xrsa"
)
func main() {
var (
plainText = "hello world"
)
fmt.Println("=== start generating RSA key pair")
pubKey := bytes.NewBuffer([]byte{})
priKey := bytes.NewBuffer([]byte{})
xrsa.CreateKeys(pubKey, priKey, 2048)
fmt.Println(pubKey.String())
fmt.Println(priKey.String())
// saveToFile("filename", []byte(priKey.String()))
fmt.Println("=== start encrypting ===")
encryptedData, err := RsaEncrypt([]byte(plainText), []byte(pubKey.String()))
checkError(err)
encryptedText := base64.StdEncoding.EncodeToString(encryptedData)
fmt.Println(encryptedText)
fmt.Println("=== start decrypting ===")
encryptedData, err = base64.StdEncoding.DecodeString(encryptedText)
checkError(err)
decryptedData, err := RsaDecrypt([]byte(encryptedData), []byte(priKey.String()))
fmt.Println(string(decryptedData))
}
func CreateKeys(publicKeyWriter, privateKeyWriter io.Writer, keyLength int) error {
// generate private key
privateKey, err := rsa.GenerateKey(rand.Reader, keyLength)
if err != nil {
return err
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "PRIVATE KEY",
Bytes: derStream,
}
err = pem.Encode(privateKeyWriter, block)
if err != nil {
return err
}
// generate public key
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return err
}
block = &pem.Block{
Type: "PUBLIC KEY",
Bytes: derPkix,
}
err = pem.Encode(publicKeyWriter, block)
if err != nil {
return err
}
return nil
}
func RsaDecrypt(ciphertext []byte, privKey []byte) ([]byte, error) {
block, _ := pem.Decode(privKey)
if block == nil {
return nil, errors.New("private key error!")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
func RsaEncrypt(ciphertext []byte, pubKey []byte) ([]byte, error) {
block, _ := pem.Decode(pubKey)
if block == nil {
return nil, errors.New("public key error!")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa.EncryptPKCS1v15(rand.Reader, pub.(*rsa.PublicKey), ciphertext)
}
func readFile(fileName string) ([]byte, error) {
bytes, err := ioutil.ReadFile(fileName)
checkError(err)
return bytes, err
}
func saveToFile(fileName string, key []byte) {
err := ioutil.WriteFile(fileName, key, 0644)
checkError(err)
}
func checkError(err error) {
if err != nil {
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment