Skip to content

a Go library to encrypt/decrypt data with RSA public/private key pair and do base85/base64 encoding/decoding.

Notifications You must be signed in to change notification settings

vspaz/rsa-encrypt-decrypt-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rsa-encrypt-decrypt-golang

a small library to encrypt/decrypt data with RSA public/private key pair and base64/85 encoding/decoding capabilities.

you can generate your own key pair as:

Generating test public/private RSA key-pair.

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -out public.pem -pubout -outform PEM

Usage example

package main

const (
	testPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAxGDcSAjiHKP9v2ITR+BjQmt9Tx2zW08ZyrjOxPew+Gxl2m5z
JyoP8sicZV81BeMNFkMg6q7sMtRXHhX1nFiTql5HBIqhZohYlN3LIXK2bdPWpDtt
rOFXfsSbZ4Wqy3XhXBhiPNn3kkkRv1N5L/IYcdrxwqaqvTlJzOeQnDsd3+AmkYst
uD4rgElOFkcUawtF7lKIYYFi42cYkJo51UD460mYieBezP6dZhFZB56pZ2rV8cQU
NrUQy2llpj+PxX/yhGnYI88ij0FST0gI2l4UsjtwXVB1Y2SxqrhNMdBU7W6ZA8WU
QQidr4MBxEFoujsLjaCl8LMsbEpAAilKezwubQIDAQABAoIBAFgkwbrzgcopMXP9
qXnRlbvyU0R3qFGLp5/+Y5C1PJHE1dK9UKJ7lrz6nnhBy6Lgzrb3Wob8DLij5pZy
dNPATkdiGa5IKznCaUAobUyOGKQjOWxt4ESAwKz9wmMs9ARu3MBhkXaOvzjB411l
Mjf7Ck3QYENmW6yjUiTOq3H0duxM/rn1Y88a9z2+aoWXQTltWvu0qKfb8SsqKzzx
HQFSalgNUxIqs+NoHRAT4ygzGGgipdP2/gXA966UonYuFAkpkutCeKVd7/6dMbm8
bgnr/x6ivGeLkbIaVkHNPRU+P4SYX1/XZohYIkTbggIih2aeH6+lEka8yZURANI6
HSUwLA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FiT
ql5HBIqhZohYlN3LIXK2bdPWpDttrOFXfsSbZ4Wqy3XhXBhiPNn3kkkRv1N5L/IY
cdrxwqaqvTlJzOeQnDsd3+AmkYstuD4rgElOFkcUawtF7lKIYYFi42cYkJo51UD4
60mYieBezP6dZhFZB56pZ2rV8cQUNrUQy2llpj+PxX/yhGnYI88ij0FST0gI2l4U
sjtwXVB1Y2SxqrhNMdBU7W6ZA8WUQQidr4MBxEFoujsLjaCl8LMsbEpAAilKezwu
bQIDAQAB
-----END PUBLIC KEY-----
`
)
package main

import (
    "log"
    "github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
)

func rsaEncryptDecrypt() {
	// example_1: Encrypt/Decrypt with RSA
	expectedText := "some expectedText goes here"
	encoder := cryptolib.NewEncoder(testPublicKey)
	rsaEncryptedText := encoder.Encrypt(expectedText)

	decoder := cryptolib.NewDecoder(testPrivateKey)
	rsaDecryptedText := decoder.Decrypt(rsaEncryptedText)

	if expectedText != rsaDecryptedText {
		log.Fatalln("failed to decrypt")
	}
}
package main

import (
	"log"
	"github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
)

func rsaEncryptDecryptAndBase85Encode() {
	expectedText := "some expectedText goes here"

	// encoding example
	encoder := cryptolib.NewEncoder(testPublicKey)
	rsaEncodedText := encoder.Encrypt(text)
	base85EncodedText := encoder.ToBase85(rsaEncodedText)
	log.Println(base85EncodedText)

	// decoding example
	decoder := cryptolib.NewDecoder(testPrivateKey)
	base85decodedText := decoder.FromBase85(base85EncodedText)
	rsaDecodedText := decoder.Decrypt(base85decodedText)
	log.Println(rsaDecodedText) // ->  "some text data"
	if rsaDecodedText != expectedText {
		log.Fatalln("failed to decrypt")
	}
}
package main

import (
	"github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
	"log"
)

func rsaEncryptDecryptAndBase64Encode() {
	expectedText := "some expectedText goes here"

	// encoding example
	encoder := cryptolib.NewEncoder(testPublicKey)
	rsaEncodedText := encoder.Encrypt(expectedText)
	base64EncodedText := encoder.ToBase64(rsaEncodedText)
	log.Println(base64EncodedText)

	// decoding example
	decoder := cryptolib.NewDecoder(testPrivateKey)
	base64decodedText := decoder.FromBase64(base64EncodedText)
	rsaDecodedText := decoder.Decrypt(base64decodedText)
	log.Println(rsaDecodedText) // ->  "some text data"
	if rsaDecodedText != expectedText {
		log.Fatalln("failed to decrypt")
	}
}
package main

import (
	"github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
	"log"
)

func encodeDecodeWithBase85() {
	expectedText := "some expectedText goes here"
	encodedText := cryptolib.Encoder{}.ToBase85([]byte(expectedText))
	actualText := string(cryptolib.Decoder{}.FromBase85(encodedText))
	if actualText !=  expectedText {
		log.Fatalln("failed to decode")
    }
}
package main

import (
	"github.com/vspaz/rsa-encrypt-decrypt-golang/pkg/cryptolib"
	"log"
)

func encodeDecodeWithBase64() {
	expectedText := "some expectedText goes here"
	encodedText := cryptolib.Encoder{}.ToBase64([]byte(expectedText))
	actualText := string(cryptolib.Decoder{}.FromBase64(encodedText))
	if actualText !=  expectedText {
		log.Fatalln("failed to decode")
    }
}

NOTE: please refer to:

About

a Go library to encrypt/decrypt data with RSA public/private key pair and do base85/base64 encoding/decoding.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published