Reputation: 25
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
Upvotes: 2
Views: 271
Reputation: 50
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.
Upvotes: 3