PHP 私钥解密代码转换为 Go 语言实现
下面是将给定的代码转换为Go语言的等效代码:\n\ngo\nimport (\n\t"crypto/rand"\n\t"crypto/rsa"\n\t"crypto/x509"\n\t"encoding/base64"\n\t"encoding/pem"\n\t"errors"\n)\n\nfunc Decrypt(encryptedData string, privateKey string) (string, error) {\n\t_encryptedData, err := base64.StdEncoding.DecodeString(encryptedData)\n\tif err != nil {\n\t\treturn "", err\n\t}\n\n\tblock, _ := pem.Decode([]byte(privateKey))\n\tif block == nil {\n\t\treturn "", errors.New("failed to decode PEM block")\n\t}\n\n\tprivKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)\n\tif err != nil {\n\t\treturn "", err\n\t}\n\n\tdecryptedList := make([]byte, 0)\n\tstep := 128\n\tif len(privateKey) > 1000 {\n\t\tstep = 256\n\t}\n\n\tfor i := 0; i < len(_encryptedData); i += step {\n\t\tend := i + step\n\t\tif end > len(_encryptedData) {\n\t\t\tend = len(_encryptedData)\n\t\t}\n\t\tdecrypted, err := rsa.DecryptPKCS1v15(rand.Reader, privKey, _encryptedData[i:end])\n\t\tif err != nil {\n\t\t\treturn "", err\n\t\t}\n\t\tdecryptedList = append(decryptedList, decrypted...)\n\t}\n\n\treturn string(decryptedList), nil\n}\n\n\n请注意,转换过程中需要导入所需的Go语言包,并且还需要使用适当的错误处理。此外,Go语言中没有直接提供对base64解码的函数,因此需要导入encoding/base64包并使用其StdEncoding.DecodeString函数来解码base64字符串。同样,Go语言中没有直接提供对openssl私钥解密的函数,因此需要使用crypto/rsa和crypto/x509包来分别解析私钥并解密数据。
原文地址: https://www.cveoy.top/t/topic/qeLm 著作权归作者所有。请勿转载和采集!