Reputation: 65
I have a client file which is encrypted using PGP encryption. The encrypted file shared is in xlsx
format. The PGP private key is also shared as a text file(Note that i added .pgp
extension for the private key file).
I tried decrypting the file using the below code in the rails console:
crypto = GPGME::Crypto.new
GPGME::Key.import(File.open('private_key.pgp'))
cipthertext = GPGME::Data.new(File.open('encrypted_file.xlsx'))
data = crypto.decrypt cipthertext, {}
But the following error is coming when the last line is executed:
/.rvm/gems/ruby-3.1.2/gems/gpgme-2.0.24/lib/gpgme/ctx.rb:489:in `decrypt_verify': No secret key (GPGME::Error::NoSecretKey)
I'm using gpgme
gem version 2.0.24
Rails version: 7.0.8.1
I tried adding .gpg
extension for the encrypted file, renaming it to 'encrypted_file.xlsx.gpg' and tried the same steps again, but still showing the same error.
Is there any issues with the code or any issues with the file extensions that i'm using?
Upvotes: 1
Views: 172
Reputation: 834
I don't see example how you encrypt your data, I just have checked full process of encryption/decryption and this is how code can looks like
# loading public key for encryption
crypto = GPGME::Crypto.new
GPGME::Key.import(File.open(Rails.root.join('public_key.pgp')))
plaintext = GPGME::Data.new('input text')
# recipient should have your email, equal you used for generating keys
options = { sign: true, signers: '[email protected]', recipients: '[email protected]' }
data = crypto.encrypt plaintext, options
f = File.open(Rails.root.join('encrypted_file.gpg'), 'wb')
bytes_written = f.write(data)
f.close
# loading private key for decryption
GPGME::Key.import(File.open(Rails.root.join('private_key.pgp')))
cipthertext = GPGME::Data.new(File.open(Rails.root.join('encrypted_file.gpg')))
data = crypto.decrypt cipthertext, {}
puts data
And useful links for encryption/decryption - https://dev.to/humbertoa6/pgp-encryptdecrypt-file-with-ruby-on-rails-part-3-3log
And generating PGP keys - https://dev.to/humbertoa6/pgp-create-a-publicprivate-key-pairpart-2-4a7b
Upvotes: 0