148 lines
3.4 KiB
Go
148 lines
3.4 KiB
Go
package rsa
|
|
|
|
import (
|
|
"fmt"
|
|
"errors"
|
|
"os"
|
|
"log"
|
|
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
)
|
|
|
|
|
|
var (
|
|
privateKeyName = "rsa_private_key.pem"
|
|
publicKeyName = "rsa_public_key.pem"
|
|
)
|
|
|
|
func GenerateKeys(path string) (*rsa.PrivateKey, *rsa.PublicKey, error) {
|
|
// Generate a new RSA key pair
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// Save the private key to a file
|
|
privateKeyPath := path + privateKeyName
|
|
privateKeyFile, err := os.Create(privateKeyPath)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer privateKeyFile.Close()
|
|
|
|
privateKeyPEM := pem.EncodeToMemory(&pem.Block{
|
|
Type: "RSA PRIVATE KEY",
|
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
|
})
|
|
_, err = privateKeyFile.Write(privateKeyPEM)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
fmt.Printf("Private key saved to %s\n", privateKeyPath)
|
|
|
|
// Extract and save the public key
|
|
publicKeyPath := path + publicKeyName
|
|
publicKeyFile, err := os.Create(publicKeyPath)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer publicKeyFile.Close()
|
|
|
|
publicKey := &privateKey.PublicKey
|
|
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
publicKeyPEM := pem.EncodeToMemory(&pem.Block{
|
|
Type: "RSA PUBLIC KEY",
|
|
Bytes: publicKeyBytes,
|
|
})
|
|
_, err = publicKeyFile.Write(publicKeyPEM)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
fmt.Printf("Public key saved to %s\n", publicKeyPath)
|
|
|
|
return privateKey, publicKey, nil
|
|
}
|
|
|
|
func LoadKeys(path string) (*rsa.PrivateKey, *rsa.PublicKey, error) {
|
|
// Load private key from file
|
|
privateKeyPath := path + privateKeyName
|
|
privateKeyData, err := os.ReadFile(privateKeyPath)
|
|
if err != nil {
|
|
log.Fatalf("Failed to read private key: %v", err)
|
|
return nil, nil, err
|
|
}
|
|
|
|
block, _ := pem.Decode(privateKeyData)
|
|
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
|
log.Fatalf("Failed to decode PEM block containing private key")
|
|
return nil, nil, err
|
|
}
|
|
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
if err != nil {
|
|
log.Fatalf("Failed to parse private key: %v", err)
|
|
return nil, nil, err
|
|
}
|
|
|
|
// Load public key from file
|
|
publicKeyPath := path + publicKeyName
|
|
publicKeyData, err := os.ReadFile(publicKeyPath)
|
|
if err != nil {
|
|
log.Fatalf("Failed to read public key: %v", err)
|
|
return nil, nil, err
|
|
}
|
|
|
|
block, _ = pem.Decode(publicKeyData)
|
|
if block == nil || block.Type != "RSA PUBLIC KEY" {
|
|
log.Fatalf("Failed to decode PEM block containing public key")
|
|
return nil, nil, err
|
|
}
|
|
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
if err != nil {
|
|
log.Fatalf("Failed to parse public key: %v", err)
|
|
return nil, nil, err
|
|
}
|
|
publicKey := publicKeyInterface.(*rsa.PublicKey)
|
|
|
|
return privateKey, publicKey, nil
|
|
}
|
|
|
|
|
|
func LoadOrGenerateKeys(path string) (*rsa.PrivateKey, *rsa.PublicKey, error) {
|
|
_, err := os.Open(path + privateKeyName)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
fmt.Println("Not Exists - Generate Keys")
|
|
return GenerateKeys(path)
|
|
}
|
|
|
|
return LoadKeys(path)
|
|
}
|
|
|
|
|
|
func Encrypt(data []byte, key *rsa.PublicKey) ([]byte, error) {
|
|
ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, key, data)
|
|
if err != nil {
|
|
log.Fatalf("Failed to encrypt: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return ciphertext, nil
|
|
}
|
|
|
|
func Decrypt(data []byte, key *rsa.PrivateKey) ([]byte, error) {
|
|
decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, key, data)
|
|
if err != nil {
|
|
log.Fatalf("Failed to decrypt: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return decrypted, nil
|
|
|
|
}
|