引言

在当今数字化时代,数据安全已成为软件开发的核心要素之一。Go语言(又称Golang)以其简洁、高效的特性,在网络安全和数据保护领域中被广泛应用。本文将深入探讨在Golang中实现的常用加密解密算法与数字签名技术,帮助开发者轻松实现高效的安全编程。

Golang加密解密算法

对称加密

对称加密是指加密和解密使用相同的密钥。以下是对称加密中常用的几种算法:

AES

AES(高级加密标准)是一种对称分组密码算法,旨在取代DES成为广泛使用的标准。AES加密过程涉及四种操作:字节替代(SubBytes)、行移位(ShiftRows)、列混淆(MixColumns)和轮密钥加(AddRoundKey)。

package main

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

func main() {
	key := "1234567890123456" // 16字节的密钥
	plaintext := "Hello, World!"
	block, err := aes.NewCipher([]byte(key))
	if err != nil {
		fmt.Println("AES cipher initialization error:", err)
		return
	}

	ciphertext := make([]byte, aes.BlockSize+len(plaintext))
	iv := ciphertext[:aes.BlockSize]
	copy(iv, plaintext[:aes.BlockSize])

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

	encrypted := base64.StdEncoding.EncodeToString(ciphertext)
	fmt.Println("Encrypted:", encrypted)

	decrypted := make([]byte, len(plaintext))
	decrypted = base64.StdEncoding.DecodeString(encrypted)

	block, err = aes.NewCipher([]byte(key))
	if err != nil {
		fmt.Println("AES cipher initialization error:", err)
		return
	}

	stream = cipher.NewCFBDecrypter(block, iv)
	stream.XORKeyStream(decrypted[aes.BlockSize:], decrypted[aes.BlockSize:])

	fmt.Println("Decrypted:", string(decrypted))
}

DES

DES(数据加密标准)是一种早期的对称加密算法,采用64位密钥和64位数据块。以下是一个简单的DES加密示例:

package main

import (
	"crypto/des"
	"encoding/base64"
	"fmt"
)

func main() {
	key := "12345678" // 8字节的密钥
	plaintext := "Hello, World!"
	block, err := des.NewCipher([]byte(key))
	if err != nil {
		fmt.Println("DES cipher initialization error:", err)
		return
	}

	ciphertext := make([]byte, des.BlockSize+len(plaintext))
	iv := ciphertext[:des.BlockSize]
	copy(iv, plaintext[:des.BlockSize])

	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(ciphertext[des.BlockSize:], plaintext[des.BlockSize:])

	encrypted := base64.StdEncoding.EncodeToString(ciphertext)
	fmt.Println("Encrypted:", encrypted)

	decrypted := make([]byte, len(plaintext))
	decrypted = base64.StdEncoding.DecodeString(encrypted)

	block, err = des.NewCipher([]byte(key))
	if err != nil {
		fmt.Println("DES cipher initialization error:", err)
		return
	}

	stream = cipher.NewCFBDecrypter(block, iv)
	stream.XORKeyStream(decrypted[des.BlockSize:], decrypted[des.BlockSize:])

	fmt.Println("Decrypted:", string(decrypted))
}

非对称加密

非对称加密是指加密和解密使用不同的密钥。以下是非对称加密中常用的几种算法:

RSA

RSA是一种非对称加密算法,其安全性基于大质数的乘积难以分解。以下是一个简单的RSA加密示例:

”`go package main

import (

"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"encoding/asn1"
"fmt"

)

func main() {

// 生成密钥对
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
	fmt.Println("RSA key generation error:", err)
	return
}

publicKey := &privateKey.PublicKey

// 保存私钥
privateKeyPEM, err := x509.MarshalPKCS1PrivateKey(privateKey)
if err != nil {
	fmt.Println("Private key marshaling error:", err)
	return
}
privateKeyFile := "privateKey.pem"
err = ioutil.WriteFile(privateKeyFile, privateKeyPEM, 0644)
if err != nil {
	fmt.Println("Private key file writing error:", err)
	return
}

// 保存公钥
publicKeyPEM, err := x509.MarshalPKIXPublicKey(publicKey)