With the rapid development of the Internet, people's requirements for security are becoming higher and higher. There are two classic algorithms in cryptography, one is symmetric encryption and decryption, and the other is asymmetric encryption and decryption. Here I will share the representative of asymmetric encryption algorithm: RSA encryption and decryption.
Implemented in GoRSA
Encryption and decryption are relatively simple. Many tutorials on the Internet are written based on the Go native standard library, and the code volume is large. Here is a useful library:/forgoer/openssl 。
Install
go get /forgoer/openssl
Key generation
The key can be generated in a file, and it is also generated in a buffer, as long as it is implementedJust do it.
import ( "io/ioutil" "os" "/forgoer/openssl" ) func main() { // Create a private key file priFile, err := ("") // You can also use buffer, priBuf := (nil) if err != nil { panic(err) } defer () // Generate private key to file _ = (1024, priFile) // Create a public key file pubFile, err := ("") if err != nil { panic(err) } defer () // Generate public key to file through private key priByte, _ := ("") _ = (priByte, pubFile) }
Encryption and decryption
Use public key to encrypt and private key to decrypt.
src := []byte("123456") pubByte, _ := ("") // Public key encryption dst, _ := (src, pubByte) priByte, _ := ("") // Decrypt the private key res, _ := (dst, priByte) (string(res)) // 123456
Signature and verification
Use private key signature, public key verification.
// Private key signature sign, err := ([]byte("123456"), priByte, crypto.SHA256) if err != nil { panic(err) } // Public key verification signature err = ([]byte("123456"), sign, pubByte, crypto.SHA256) if err != nil { panic(err) }
This encryption and decryption library:/forgoer/openssl, it also supportsAES
、DES
、RSA
、sha1
、Hmac-Sha1
、sha256
、Hmac-Sha256
Commonly used algorithms.
This is the end of this article about the detailed explanation of Go language implementation of RSA encryption and decryption algorithm. For more relevant Go language RSA encryption and decryption content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!