linux

Linux环境下Golang日志如何加密

小樊
40
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
看了该问题的人还看了