Enchantrix/examples/rsa/main.go
Claude c8531fa66b
Some checks failed
Go / build (push) Failing after 27s
Publish Docs / build (push) Failing after 1m0s
chore: migrate module path from github.com to forge.lthn.ai
Move module declaration and all internal imports from
github.com/Snider/Enchantrix to forge.lthn.ai/Snider/Enchantrix.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 21:34:26 +00:00

53 lines
1.5 KiB
Go

// Example: RSA encryption and decryption
//
// This example demonstrates RSA key generation, encryption, and decryption
// using the crypt service. RSA is suitable for encrypting small amounts of
// data or for key exchange protocols.
//
// Run with: go run examples/rsa/main.go
package main
import (
"encoding/base64"
"fmt"
"log"
"forge.lthn.ai/Snider/Enchantrix/pkg/crypt"
)
func main() {
fmt.Println("--- RSA Demo ---")
cryptService := crypt.NewService()
// 1. Generate RSA key pair
fmt.Println("Generating 2048-bit RSA key pair...")
publicKey, privateKey, err := cryptService.GenerateRSAKeyPair(2048)
if err != nil {
log.Fatalf("Failed to generate RSA key pair: %v", err)
}
fmt.Println("Key pair generated successfully.")
// 2. Encrypt a message
message := []byte("This is a secret message for RSA.")
fmt.Printf("\nOriginal message: %s\n", message)
ciphertext, err := cryptService.EncryptRSA(publicKey, message, nil)
if err != nil {
log.Fatalf("Failed to encrypt with RSA: %v", err)
}
fmt.Printf("Encrypted ciphertext (base64): %s\n", base64.StdEncoding.EncodeToString(ciphertext))
// 3. Decrypt the message
decrypted, err := cryptService.DecryptRSA(privateKey, ciphertext, nil)
if err != nil {
log.Fatalf("Failed to decrypt with RSA: %v", err)
}
fmt.Printf("Decrypted message: %s\n", decrypted)
// 4. Verify
if string(message) == string(decrypted) {
fmt.Println("\nSuccess! RSA decrypted message matches the original.")
} else {
fmt.Println("\nFailure! RSA decrypted message does not match the original.")
}
fmt.Println()
}