英文:
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:
<?php
$encrypted = file_get_contents('./encryptedString.txt');
$privKey = file_get_contents('./private.key');
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 > 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("private.key")
if err != nil {
log.Fatal("Failed to read private key - " + err.Error())
}
block, _ := pem.Decode(privateKeyB)
parseResult, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
log.Fatal("Failed to parse private key - " + err.Error())
}
privateKey := parseResult.(*rsa.PrivateKey)
encStringB, err := ioutil.ReadFile("encryptedString.txt")
if err != nil {
log.Fatal("Failed to read encrypted string - " + err.Error())
}
encString64, err := base64.StdEncoding.DecodeString(string(encStringB))
if err != nil {
log.Fatal("Failed to decode encrypted string to base64 - " + err.Error())
}
decryptedB, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encString64)
if err != nil {
log.Fatal("Failed to decrypt string - " + err.Error())
}
fmt.Println(string(decryptedB))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论