aes v1

This package provides functions to encrypt and decrypt data using AES encryption. It supports both byte slices and strings as input and output formats.

Repository

Installation

go get -u github.com/metadiv-io/aes

Usage

String Encryption/Decryption

package main

import (
    "fmt"
    "github.com/yourusername/aes"
)

func main() {
    // Your encryption key must be either 16, 24, or 32 bytes long
    key := "your-secret-key32-bytes-required!"
    
    // Encrypt a string
    plaintext := "Hello, World!"
    encrypted := aes.Encrypt.StringToBase64(plaintext, key)
    fmt.Printf("Encrypted: %s\n", encrypted)
    
    // Decrypt the string
    decrypted := aes.Decrypt.Base64ToString(encrypted, key)
    fmt.Printf("Decrypted: %s\n", decrypted)
}

Byte Slice Encryption/Decryption

package main

import (
    "fmt"
    "github.com/yourusername/aes"
)

func main() {
    key := "your-secret-key32-bytes-required!"
    
    // Encrypt byte slice
    data := []byte{1, 2, 3, 4, 5}
    encrypted := aes.Encrypt.BytesToBytes(data, key)
    
    // Decrypt byte slice
    decrypted := aes.Decrypt.BytesToBytes(encrypted, key)
    fmt.Printf("Original: %v\n", data)
    fmt.Printf("Decrypted: %v\n", decrypted)
}

Working with Base64 Encoding

package main

import (
    "fmt"
    "github.com/yourusername/aes"
)

func main() {
    key := "your-secret-key32-bytes-required!"
    
    // Encrypt string to base64
    plaintext := "Sensitive data"
    base64Encrypted := aes.Encrypt.StringToBase64(plaintext, key)
    fmt.Printf("Base64 Encrypted: %s\n", base64Encrypted)
    
    // Decrypt from base64
    decrypted := aes.Decrypt.Base64ToString(base64Encrypted, key)
    fmt.Printf("Decrypted: %s\n", decrypted)
}

API Reference

Encryption Methods

  • BytesToBytes(src []byte, key string) []byte

    • Encrypts a byte slice and returns encrypted bytes

  • BytesToString(src []byte, key string) string

    • Encrypts a byte slice and returns encrypted string

  • BytesToBase64(src []byte, key string) string

    • Encrypts a byte slice and returns base64 encoded string

  • StringToBytes(str, key string) []byte

    • Encrypts a string and returns encrypted bytes

  • StringToString(str, key string) string

    • Encrypts a string and returns encrypted string

  • StringToBase64(str, key string) string

    • Encrypts a string and returns base64 encoded string

Decryption Methods

  • BytesToBytes(src []byte, key string) []byte

    • Decrypts encrypted bytes and returns original bytes

  • BytesToString(src []byte, key string) string

    • Decrypts encrypted bytes and returns original string

  • StringToBytes(encrypted string, key string) []byte

    • Decrypts encrypted string and returns original bytes

  • StringToString(encrypted string, key string) string

    • Decrypts encrypted string and returns original string

  • Base64ToBytes(src string, key string) []byte

    • Decrypts base64 encoded string and returns original bytes

  • Base64ToString(encrypted string, key string) string

    • Decrypts base64 encoded string and returns original

Last updated