Golang加密解密之RSA
RSA是非对称加密中常用的算法。首先要了解非对称加密和对称加密的区别:
- 对称加密:加解密使用同一把密钥
- 非对称加密:加解密使用一对公私钥
非对称加密最常见的方式就是RSA,接下来我们就主要介绍RSA的加密解密过程。
生成RSA公私钥对
Golang
Golang中使用crypto库来生成RSA公私钥对。下面是生成2048位RSA公私钥对的示例代码:
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
)
func main() {
bitSize := 2048
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
panic(err)
}
derPrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: derPrivateKey,
}
privateKeyFile, err := os.Create("private.pem")
if err != nil {
panic(err)
}
defer privateKeyFile.Close()
err = pem.Encode(privateKeyFile, &privateKeyBlock)
if err != nil {
panic(err)
}
publicKey := privateKey.PublicKey
derPublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
panic(err)
}
publicKeyBlock := pem.Block{
Type: "PUBLIC KEY",
Bytes: derPublicKey,
}
publicKeyFile, err := os.Create("public.pem")
if err != nil {
panic(err)
}
defer publicKeyFile.Close()
err = pem.Encode(publicKeyFile, &publicKeyBlock)
if err != nil {
panic(err)
}
fmt.Println("RSA public and private keys generated")
}
运行上述代码会在当前目录下生成名为public.pem和private.pem的PEM格式公私钥文件。
PHP
在PHP中也可以通过openssl扩展来生成RSA公私钥对,示例代码如下:
$privateKey = openssl_pkey_new([
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
]);
if (!$privateKey) {
die("Failed to generate private key");
}
openssl_pkey_export_to_file($privateKey, "private.pem");
$details = openssl_pkey_get_details($privateKey);
$publicKey = $details['key'];
openssl_pkey_export_to_file($publicKey, "public.pem");
echo "RSA public and private keys generated";
运行上述代码会在当前目录下生成名为public.pem和private.pem的PEM格式公私钥文件。
RSA加密解密
Golang
Golang中使用crypto/rsa库来进行RSA加密解密。下面是示例代码:
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
)
func main() {
message := "Hello, RSA"
privateKeyPEM, err := ioutil.ReadFile("private.pem")
if err != nil {
panic(err)
}
privateKeyBlock, _ := pem.Decode(privateKeyPEM)
privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
if err != nil {
panic(err)
}
ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, &privateKey.PublicKey, []byte(message))
if err != nil {
panic(err)
}
fmt.Printf("RSA encrypted message: %x\n", ciphertext)
publicKeyPEM, err := ioutil.ReadFile("public.pem")
if err != nil {
panic(err)
}
publicKeyBlock, _ := pem.Decode(publicKeyPEM)
publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)
if err != nil {
panic(err)
}
publicKey := publicKeyInterface.(*rsa.PublicKey)
plaintext, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, ciphertext)
if err != nil {
panic(err)
}
fmt.Printf("RSA decrypted message: %s\n", plaintext)
}
运行上述代码会将"Hello, RSA"字符串进行RSA加密后再解密,并输出加密解密后的结果。
PHP
PHP也可以使用openssl扩展来实现RSA加密解密,示例代码如下:
$message = "Hello, RSA";
$publicKeyPEM = file_get_contents("public.pem");
$publicKey = openssl_pkey_get_public($publicKeyPEM);
if (!$publicKey) {
die("Failed to get public key");
}
$ciphertext = "";
$success = openssl_public_encrypt($message, $ciphertext, $publicKey, OPENSSL_PKCS1_PADDING);
if (!$success) {
die("Failed to encrypt message");
}
echo "RSA encrypted message: " . bin2hex($ciphertext) . "\n";
$privateKeyPEM = file_get_contents("private.pem");
$privateKey = openssl_pkey_get_private($privateKeyPEM);
if (!$privateKey) {
die("Failed to get private key");
}
$plaintext = "";
$success = openssl_private_decrypt($ciphertext, $plaintext, $privateKey, OPENSSL_PKCS1_PADDING);
if (!$success) {
die("Failed to decrypt message");
}
echo "RSA decrypted message: " . $plaintext . "\n";
运行上述代码会将"Hello, RSA"字符串进行RSA加密后再解密,并输出加密解密后的结果。
总结
本文介绍了如何使用Golang和PHP来生成RSA公私钥对,以及如何使用Golang和PHP来进行RSA加密解密。通过学习本文,读者应该对RSA算法的特点和使用方法有所掌握。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Golang加密解密之RSA(附带php) - Python技术站