gpg and the Bouncy Castle C# OpenPGP library

240 views
Skip to first unread message

levi

unread,
Apr 13, 2007, 3:49:39 PM4/13/07
to GPGNET
I am using Bouncy Castle's C# libary to encrypt a file. The public
key that I use is a key that was generated with gpg. This works fine,
as in I can load the public key, use it to encrypt the data into a
file and save it. I can even open the file that I write out with the
private key and read it using the C# library. However, if I try to
decrypt the file that I wrote out using "gpg.exe --decrypt myfile.enc"
I get a "no valid OpenPGP data found." error message followed by a
"decrypt_message failed: eof". I am very new to cryptography, and was
wondering what I might be doing wrong. For anyone that knows C#, here
is how I am encrypting the data:

AsymmetricKeyParameter pubKey = null;

PgpPublicKeyRing pgpPub = new PgpPublicKeyRing(mypubkey /*
byte[] */);

PgpSecretKeyRing pgpPriv = new PgpSecretKeyRing(myprvkey /
* byte[] */);

pubKey = pgpPub.GetPublicKey().GetKey();

PgpSecretKey pgpPrivSecretKey = pgpPriv.GetSecretKey();
PgpPrivateKey pgpPrivateKey =
pgpPrivSecretKey.ExtractPrivateKey("password".ToCharArray());

IBufferedCipher c = CipherUtilities.GetCipher("ELGAMAL");

// c.Init(Cipher.ENCRYPT_MODE, pubKey);
c.Init(true, pubKey);

byte[] inBytes = Encoding.ASCII.GetBytes("Hello, can you
read this?");
c.ProcessBytes(inBytes); // encrypt the data
byte[] outBytes = c.DoFinal(); // finalize it

FileStream fs = File.Create("pgptest.enc");
fs.Write(outBytes, 0, outBytes.Length);
fs.Close();

Reply all
Reply to author
Forward
0 new messages