dongzhong7443 2016-05-05 14:14
浏览 36
已采纳

从文件读入

I am working on making a basic password keeper in Golang and want to be able to store the passwords encrypted using RSA. My encryption function and decryption functions both work and will encrypt and decrypt correctly. However after storing the password in a file and then reading the password back from the file the decryption function fails. I have checked to make sure that the reading in of the RSA key is correct and that is not my problem as reading in the RSA key works correctly. Here is how I am writing my encrypted password to the file

ioutil.WriteFile(filename, encPassword, 0644)

and here is how I am reading back the password

encrypted, err = ioutil.ReadFile(encryptedFileName)

When I run my program I am currently receiving this error code

failed in decrypt_oaep: crypto/rsa: decryption error
exit status 1

My belief is that Read or Write file is adding something to the contents of the file because if I try and decrypt the password before the encrypted password is written or read from a file it will work fine. Any help would be appreciated.

If you need more code I can post more of it later.

Edit: here is a link to codeshare with my entire code: https://codeshare.io/PtMxk

  • 写回答

1条回答 默认 最新

  • douzhou7656 2016-05-05 15:03
    关注

    In line 167 you are calling your encrypt function with label = []byte(product).

    In line 120 you are calling your decrypt function with a label variable that has been defined but not initialized (i.e. you are sending an empty byte array)

    decrypted = decrypt_oaep(private_key, encrypted, label)
    

    Because of that, your decryption won't work. From the docs:

    The label parameter must match the value given when encrypting - https://golang.org/pkg/crypto/rsa/#DecryptOAEP

    Solution: In the decryption call (line 120) send []byte(product) as the label parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?