OpenSSL解密

huangapple go评论84阅读模式
英文:

OpenSSL decryption

问题

我有一个加密的字符串和非对称RSA密钥。该字符串是使用PHP的openssl_public_encrypt函数和密钥的公共部分以及PKCS#1 v1.5填充进行加密的。我想使用Go语言和密钥的私有部分解密加密的字符串。

我知道如何在PHP中解密它:

<?php

$encrypted = file_get_contents('./encryptedString.txt');
$privKey = file_get_contents('./private.key');

openssl_private_decrypt(base64_decode($encrypted), $decrypted, $privKey);

print_r($decrypted);

我知道如何在Bash中解密它:

#!/bin/bash

cat encryptedString.txt | base64 -d > encryptedString64.txt
openssl rsautl -decrypt -in ./encryptedString64.txt -inkey ./private.key

我想以相同的方式在Go语言中解密字符串。我已经尝试了crypto/rsa包中的一些函数:

func DecryptString(privKey *rsa.PrivateKey, encryptedString []byte) ([]byte, error) {

	decryptedBytes, err := rsa.DecryptOAEP(sha256.New(), nil, privKey, encryptedString, nil)
	if err != nil {
		return nil, err
	}

	return decryptedBytes, nil
}

func GetPrivateKey() (*rsa.PrivateKey, error) {
	pemString := `******************`

	block, _ := pem.Decode([]byte(pemString))
	parseResult, _ := x509.ParsePKCS8PrivateKey(block.Bytes)
	key := parseResult.(*rsa.PrivateKey)

	return key, nil
}

...但我仍然收到错误消息"crypto/rsa: decryption error"或空结果。我漏掉了什么?

英文:

I have encrypted string and asymmetric RSA key. The string was encrypted by PHP and its function openssl_public_encrypt with public part of the key and PKCS#1 v1.5 padding. I want to decrypt encrypted string with Go lang and private part of the key.

I know how to decrypt it in PHP:

&lt;?php

$encrypted = file_get_contents(&#39;./encryptedString.txt&#39;);
$privKey = file_get_contents(&#39;./private.key&#39;);

openssl_private_decrypt(base64_decode($encrypted), $decrypted, $privKey);

print_r($decrypted);

I know how to decrypt it in Bash:

#!/bin/bash

cat encryptedString.txt | base64 -d &gt; encryptedString64.txt
openssl rsautl -decrypt -in ./encryptedString64.txt -inkey ./private.key

I want to decrypt the string in the same way in GO lang. I've already tried some function from crypto/rsa package:

func DecryptString(privKey *rsa.PrivateKey, encryptedString []byte) ([]byte, error) {

	decryptedBytes, err := rsa.DecryptOAEP(sha256.New(), nil, privKey, encryptedString, nil)
	if err != nil {
		return nil, err
	}

	return decryptedBytes, nil
}

func GetPrivateKey() (*rsa.PrivateKey, error) {
	pemString := `******************`

	block, _ := pem.Decode([]byte(pemString))
	parseResult, _ := x509.ParsePKCS8PrivateKey(block.Bytes)
	key := parseResult.(*rsa.PrivateKey)

	return key, nil
}

...but I'm still getting error "crypto/rsa: decryption error" or empty results. What am I missing?

答案1

得分: 1

谢谢大家的评论。我已经解决了问题,并在下面发布了解决方案。

func main() {
	privateKeyB, err := ioutil.ReadFile("private.key")
	if err != nil {
		log.Fatal("无法读取私钥 - " + err.Error())
	}
	block, _ := pem.Decode(privateKeyB)
	parseResult, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		log.Fatal("无法解析私钥 - " + err.Error())
	}
	privateKey := parseResult.(*rsa.PrivateKey)

	encStringB, err := ioutil.ReadFile("encryptedString.txt")
	if err != nil {
		log.Fatal("无法读取加密字符串 - " + err.Error())
	}
	encString64, err := base64.StdEncoding.DecodeString(string(encStringB))
	if err != nil {
		log.Fatal("无法将加密字符串解码为base64 - " + err.Error())
	}

	decryptedB, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encString64)
	if err != nil {
		log.Fatal("无法解密字符串 - " + err.Error())
	}

	fmt.Println(string(decryptedB))
}
英文:

Thank you all for your comments. I have solved it and I am posting the solution below.

func main() {
	privateKeyB, err := ioutil.ReadFile(&quot;private.key&quot;)
	if err != nil {
		log.Fatal(&quot;Failed to read private key - &quot; + err.Error())
	}
	block, _ := pem.Decode(privateKeyB)
	parseResult, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		log.Fatal(&quot;Failed to parse private key - &quot; + err.Error())
	}
	privateKey := parseResult.(*rsa.PrivateKey)

	encStringB, err := ioutil.ReadFile(&quot;encryptedString.txt&quot;)
	if err != nil {
		log.Fatal(&quot;Failed to read encrypted string - &quot; + err.Error())
	}
	encString64, err := base64.StdEncoding.DecodeString(string(encStringB))
	if err != nil {
		log.Fatal(&quot;Failed to decode encrypted string to base64 - &quot; + err.Error())
	}

	decryptedB, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encString64)
	if err != nil {
		log.Fatal(&quot;Failed to decrypt string - &quot; + err.Error())
	}

	fmt.Println(string(decryptedB))
}

huangapple
  • 本文由 发表于 2021年11月18日 17:45:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/70017596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定