溫馨提示×

Linux環境下Golang日志如何加密

小樊
41
2025-05-24 19:14:37
欄目: 編程語言

在Linux環境下,對Golang日志進行加密可以通過以下幾種方法實現:

  1. 使用對稱加密算法(如AES):

首先,你需要安裝一個加密庫,例如github.com/cryptix/go-cryptix。然后,你可以使用這個庫來實現日志的加密和解密。

示例代碼:

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io"
)

func encrypt(plainText string, key []byte) (cipherText string, err error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}

	plainTextBytes := []byte(plainText)
	plainTextBytes = pkcs5Padding(plainTextBytes, aes.BlockSize)

	cipherTextBytes := make([]byte, aes.BlockSize+len(plainTextBytes))
	iv := cipherTextBytes[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		return "", err
	}

	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(cipherTextBytes[aes.BlockSize:], plainTextBytes)

	cipherText = base64.StdEncoding.EncodeToString(cipherTextBytes)
	return cipherText, nil
}

func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize
	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(ciphertext, padtext...)
}

func main() {
	key := []byte("your-secret-key")
	plainText := "Hello, World!"

	encryptedText, err := encrypt(plainText, key)
	if err != nil {
		fmt.Println("Error encrypting:", err)
		return
	}

	fmt.Println("Encrypted text:", encryptedText)
}
  1. 使用非對稱加密算法(如RSA):

首先,你需要安裝一個加密庫,例如github.com/tidwall/gjson。然后,你可以使用這個庫來實現日志的加密和解密。

示例代碼:

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"fmt"
)

func generateRSAKeyPair() (*rsa.PrivateKey, *rsa.PublicKey, error) {
	privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		return nil, nil, err
	}

	publicKey := &privateKey.PublicKey
	return privateKey, publicKey, nil
}

func encryptWithPublicKey(plainText string, publicKey *rsa.PublicKey) (cipherText string, err error) {
	encryptedBytes, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(plainText))
	if err != nil {
		return "", err
	}

	cipherText = base64.StdEncoding.EncodeToString(encryptedBytes)
	return cipherText, nil
}

func decryptWithPrivateKey(cipherText string, privateKey *rsa.PrivateKey) (plainText string, err error) {
	encryptedBytes, err := base64.StdEncoding.DecodeString(cipherText)
	if err != nil {
		return "", err
	}

	plainTextBytes, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encryptedBytes)
	if err != nil {
		return "", err
	}

	plainText = string(plainTextBytes)
	return plainText, nil
}

func main() {
	privateKey, publicKey, err := generateRSAKeyPair()
	if err != nil {
		fmt.Println("Error generating RSA key pair:", err)
		return
	}

	plainText := "Hello, World!"
	encryptedText, err := encryptWithPublicKey(plainText, publicKey)
	if err != nil {
		fmt.Println("Error encrypting:", err)
		return
	}

	fmt.Println("Encrypted text:", encryptedText)

	decryptedText, err := decryptWithPrivateKey(encryptedText, privateKey)
	if err != nil {
		fmt.Println("Error decrypting:", err)
		return
	}

	fmt.Println("Decrypted text:", decryptedText)
}

注意:在實際應用中,你需要妥善保管加密密鑰,避免泄露。另外,你可以根據需要選擇合適的加密算法和庫。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女