{"record":{"id":"66efe384538b5b92","repo":"AlexxIT/go2rtc","slug":"failed-to-decode-pem-block","errorCode":null,"errorMessage":"failed to decode PEM block","messagePattern":"failed to decode PEM block","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/tuya/helper.go","lineNumber":28,"sourceCode":"\t\"errors\"\n\t\"net/http\"\n\t\"net/http/cookiejar\"\n\t\"regexp\"\n\t\"time\"\n\n\t\"golang.org/x/net/publicsuffix\"\n)\n\nfunc EncryptPassword(password, pbKey string) (string, error) {\n\t// Hash password with MD5\n\thasher := md5.New()\n\thasher.Write([]byte(password))\n\thashedPassword := hex.EncodeToString(hasher.Sum(nil))\n\n\t// Decode PEM public key\n\tblock, _ := pem.Decode([]byte(\"-----BEGIN PUBLIC KEY-----\\n\" + pbKey + \"\\n-----END PUBLIC KEY-----\"))\n\tif block == nil {\n\t\treturn \"\", errors.New(\"failed to decode PEM block\")\n\t}\n\n\tpubKey, err := x509.ParsePKIXPublicKey(block.Bytes)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\trsaPubKey, ok := pubKey.(*rsa.PublicKey)\n\tif !ok {\n\t\treturn \"\", errors.New(\"not an RSA public key\")\n\t}\n\n\t// Encrypt with RSA\n\tencrypted, err := rsa.EncryptPKCS1v15(cryptoRand.Reader, rsaPubKey, []byte(hashedPassword))\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/tuya/helper.go#L10-L46","documentation":"Tuya EncryptPassword guard: pem.Decode returned nil for the constructed '-----BEGIN PUBLIC KEY-----' wrapper around the provided pbKey string. The server-supplied key blob is not valid PEM/base64 DER, so the RSA key cannot be parsed and password encryption fails.","triggerScenarios":"Thrown at pkg/tuya/helper.go:28 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check pbKey for copy corruption/truncation (it should be base64 DER SPKI)","Strip whitespace/newlines incorrectly inserted into the key","Fetch a fresh key from the Tuya endpoint if the cached one is stale"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"c245815e75e2a5fd60b4290f12bfc04e55a984d3","analyzedAt":"2026-09-07T11:47:02.965Z","contentChangedAt":"2026-09-07T11:47:02.965Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}