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.

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

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题