views:

518

answers:

2

hi i need to pass the public key and private key in string format for encryption and decryption in pgp.i ve generated the keys like this but i am not able to use those.so can anyone tell me how to get the public key and private key in string format from this.and also the rsakeygenerator has not given the passphrase for private key. so where do i get passphrase for private key.

private void button2_Click(object sender, EventArgs e)
    {
        // keyPair = createASymRandomCipher();
        //CipherPublicKey publicKey = getCipherPublicKey(keyPair);
        AsymmetricCipherKeyPair keyPair = createASymRandomCipher();
        Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters pubkey = (Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)keyPair.Public;
        Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters privkey = (Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters)keyPair.Private;
        CipherPublicKey pbkey = getCipherPublicKey(pubkey);
        CipherPrivateKey prvkey = getCipherPrivateKey(privkey);

    }

private static AsymmetricCipherKeyPair createASymRandomCipher() 
    {
        RsaKeyPairGenerator r = new RsaKeyPairGenerator();
        r.Init(new KeyGenerationParameters(new SecureRandom(),
          1024));
        AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
        return keys;
    }

    [Serializable]
    private struct CipherPrivateKey
    {
        public byte[] modulus; 
        public byte[] publicExponent; 
        public byte[] privateExponent; 
        public byte[] p; 
        public byte[] q; 
        public byte[] dP; 
        public byte[] dQ; 
        public byte[] qInv;
    }
    [Serializable]
    private struct CipherPublicKey 
    { 
        public bool isPrivate; 
        public byte[] modulus; 
        public byte[] exponent;
    }

    private static CipherPublicKey getCipherPublicKey(Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters cPublic) 
    { 
        CipherPublicKey cpub = new CipherPublicKey(); cpub.modulus = cPublic.Modulus.ToByteArray(); 
        cpub.exponent = cPublic.Exponent.ToByteArray(); 
        return cpub; 
    }
    private static CipherPrivateKey getCipherPrivateKey(Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters cPrivate)
    {
        CipherPrivateKey cpri = new CipherPrivateKey(); 
        cpri.dP = cPrivate.DP.ToByteArray(); 
        cpri.dQ = cPrivate.DQ.ToByteArray(); 
        cpri.modulus = cPrivate.Modulus.ToByteArray(); 
        cpri.p = cPrivate.P.ToByteArray(); 
        cpri.privateExponent = cPrivate.Exponent.ToByteArray(); 
        cpri.publicExponent = cPrivate.PublicExponent.ToByteArray(); 
        cpri.q = cPrivate.Q.ToByteArray(); 
        cpri.qInv = cPrivate.QInv.ToByteArray(); 
        return cpri;
    }
A: 

You need to ask the user for the passphrase. The whole point of having a passphrase is that you won't be able to work out the private key without it, and only the user can supply it.

(I haven't looked at the rest of your code, not being familiar with the BouncyCastle API. I do question the wisdom of a mutable struct with lots of byte arrays though...)

Jon Skeet
A: 

The answer to just your converting question is to convert them to Base64Strings

If you want it in hex (so a user can enter it easier), you can use the System.Runtime.Remoting.Metadata.W3cXsd2001 namespace to get convert to/from a HEX rep. Here is an example in C#.

I will also say that there may be a security flaw in your though process, but I am not sure that I am qualified to address it. (See Jon's post)

JasonRShaver