SoFunction
Updated on 2025-04-09

Analysis of the usage of go-crypto library, a Go language encryption and decryption tool

In software development, data security and privacy protection are increasingly valued. Go language has become the first choice for many developers with its concise and efficient features. However, when using encryption and decryption in actual projects, it is still necessary to make some encapsulation based on the standard library.go-cryptoThe library came into being. It is a library of encryption and decryption tools designed specifically for Golang, which provides support for a variety of encryption algorithms such as AES and RSA.

This article will introduce this library in a comprehensive way from multiple perspectives such as installation, features, basic and advanced functions, and practical application scenarios.

Introduction to go-crypto library

go-cryptoIt is a library of encryption and decryption tools designed for Golang. It implements a variety of commonly used encryption algorithms, including AES and RSA. Through this library, developers can easily implement data encryption and decryption in Go language projects, ensuring the security of data transmission and storage.

Install

To use it in your Go projectgo-crypto, first of all, you need to passgo getCommand Installation:

go get -u /pudongping/go-crypto

characteristic

go-cryptoThe library provides the following features:

  • AES encryption and decryption method: Supports codebook mode (ECB), password group link mode (CBC), calculator mode (CTR), password feedback mode (CFB) and output feedback mode (OFB).
  • RSA encryption and decryption method: Supports RSA encryption and decryption.

Next, I will demonstrate their usage using Go and PHP encryption and decryption respectively.

AES Encryption and Decryption

CBC mode

The CBC mode is a password group linking mode, which increases the security of encrypted data by XORing the encryption result of the previous block with the plaintext of the current block. The following is the usego-cryptoExamples of AES-CBC encryption and decryption for libraries:

Go encryption, PHP decryption (AES-128-CBC)

go encryption

package main

import (
	"fmt"
	"/pudongping/go-crypto"
)

func main() {
	plaintext := "hello world! My name is Alex Pu"
	key := "1234567890123456" // The key byte length must be 16 bytes
	ciphertext, err := go_crypto.AESCBCEncrypt(plaintext, key)
	if err != nil {
		("An error happened!", err)
	}
	(ciphertext)
}

PHP Decryption

<?php
$key = '1234567890123456';
$iv = mb_substr($key, 0, 16);
$s = 'BRK08I0OYOoFwhgIBT1qjFywFkLADdeLQfVZM7CPKJ8=';

$str = base64_decode($s);
$decrypted = openssl_decrypt($str, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
if (!$decrypted) {
    echo 'Decryption failed' . PHP_EOL;
} else {
    echo($decrypted) . PHP_EOL;
}
?>

php encryption, go decryption (AES-128-CBC)

PHP encryption

$string = 'hello world! alex';
$key = '1234567890123456';
$iv = mb_substr($key, 0, 16);

$encrypted = openssl_encrypt($string, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
$s = base64_encode($encrypted);

// output is: YAZkDJYi7e9O09TRNvUf+6sFMlI8AQvZ5GVU+xJIuOc=
echo $s . PHP_EOL;

Go Decryption

import "/pudongping/go-crypto"

func main() {
    ciphertext := "YAZkDJYi7e9O09TRNvUf+6sFMlI8AQvZ5GVU+xJIuOc="
    key := "1234567890123456"
    
    plaintext, err := go_crypto.AESCBCDecrypt(ciphertext, key)
    if err != nil {
        ("An error happened!", err)
    }
	
	// output is: decryption ==> hello world! alex    ("Decryption ==> ", plaintext)
}

ECB mode

ECB mode is the codebook mode, which is the simplest encryption mode, but is less secure and is usually not recommended. The following is the usego-cryptoExamples of AES-ECB encryption and decryption for libraries:

Go encryption, PHP decryption (AES-128-ECB)

go encryption

package main

import (
	"fmt"
	"/pudongping/go-crypto"
)

func main() {
	plaintext := "hello world! My name is Alex Pu"
	key := "1234567890123456" // The key byte length must be 16 bytes
	ciphertext, err := go_crypto.AESECBEncrypt(plaintext, key)
	if err != nil {
		("An error happened!", err)
	}
	(ciphertext)
}

php decryption

<?php
$key = '1234567890123456';
$s = 'sRFeHhndretZFZE9/7WdGuGw1QYl8l/IlI1XEtpVzxI=';

$str = base64_decode($s);
$decrypted = openssl_decrypt($str, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
if (!$decrypted) {
    echo 'Decryption failed' . PHP_EOL;
} else {
    echo($decrypted) . PHP_EOL;
}
?>

RSA encryption and decryption

go-cryptoThe library also provides RSA encryption and decryption capabilities. The following is the usego-cryptoExamples of RSA encryption and decryption for libraries:

package main

import (
	"fmt"
	"/pudongping/go-crypto"
)

func main() {
	privateKey := []byte(`-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----`)

	publicKey := []byte(`-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----`)

	plaintext := "hello world"
	("original ==> ", plaintext)
	ciphertext, err := go_crypto.RSAEncrypt(publicKey, []byte(plaintext))
	if err != nil {
		(err)
		return
	}

	plaintext1, err := go_crypto.RSADecrypt(privateKey, ciphertext)
	("Decryption ==> ", string(plaintext1))
	if err != nil {
		(err)
		return
	}
}

Application scenarios

Suppose you are developing a distributed system that requires secure communication,go-cryptoThe library can be used to encrypt sensitive data, such as user information, payment information, etc., to ensure the security of the data during transmission. By using AES encryption, you can protect data from unauthorized access, while RSA encryption can be used to securely transfer keys.

Conclusion

go-cryptoThe library provides Go language developers with a powerful and flexible encryption and decryption tool. Through the detailed introduction of this article, I hope you can understand and master it in depthgo-cryptoHow to use it, add a layer of security to your project. In actual development, the rational use of encryption technology can significantly improve the security and reliability of the system.

This is the article about the analysis of the usage and analysis of the Go-crypto library, a Go language encryption and decryption tool. For more relevant contents of the Go-crypto library, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!