private byte[] EncryptSessionInfo(byte[] sessionInfo, SecureRandom random) { if (pubKey.Algorithm != PublicKeyAlgorithmTag.EC) { IBufferedCipher cipher; switch (pubKey.Algorithm) { case PublicKeyAlgorithmTag.RsaGeneral: case PublicKeyAlgorithmTag.RsaEncrypt: cipher = CipherUtilities.GetCipher("RSA//PKCS1Padding"); break; case PublicKeyAlgorithmTag.ElGamalEncrypt: case PublicKeyAlgorithmTag.ElGamalGeneral: cipher = CipherUtilities.GetCipher("ElGamal/ECB/PKCS1Padding"); break; case PublicKeyAlgorithmTag.Dsa: throw new PgpException("Can't use DSA for encryption."); case PublicKeyAlgorithmTag.ECDsa: throw new PgpException("Can't use ECDSA for encryption."); default: throw new PgpException(string.Concat((object)"unknown asymmetric algorithm: ", (object)pubKey.Algorithm)); } AsymmetricKeyParameter parameters = pubKey.GetKey(); cipher.Init(forEncryption: true, new ParametersWithRandom(parameters, random)); return(cipher.DoFinal(sessionInfo)); } ECDHPublicBcpgKey eCDHPublicBcpgKey = (ECDHPublicBcpgKey)pubKey.PublicKeyPacket.Key; IAsymmetricCipherKeyPairGenerator keyPairGenerator = GeneratorUtilities.GetKeyPairGenerator("ECDH"); keyPairGenerator.Init(new ECKeyGenerationParameters(eCDHPublicBcpgKey.CurveOid, random)); AsymmetricCipherKeyPair asymmetricCipherKeyPair = keyPairGenerator.GenerateKeyPair(); ECPrivateKeyParameters eCPrivateKeyParameters = (ECPrivateKeyParameters)asymmetricCipherKeyPair.Private; ECPublicKeyParameters eCPublicKeyParameters = (ECPublicKeyParameters)asymmetricCipherKeyPair.Public; ECPublicKeyParameters eCPublicKeyParameters2 = (ECPublicKeyParameters)pubKey.GetKey(); ECPoint s = eCPublicKeyParameters2.Q.Multiply(eCPrivateKeyParameters.D).Normalize(); KeyParameter parameters2 = new KeyParameter(Rfc6637Utilities.CreateKey(pubKey.PublicKeyPacket, s)); IWrapper wrapper = PgpUtilities.CreateWrapper(eCDHPublicBcpgKey.SymmetricKeyAlgorithm); wrapper.Init(forWrapping: true, new ParametersWithRandom(parameters2, random)); byte[] array = PgpPad.PadSessionData(sessionInfo); byte[] array2 = wrapper.Wrap(array, 0, array.Length); byte[] encoded = new MPInteger(new BigInteger(1, eCPublicKeyParameters.Q.GetEncoded(compressed: false))).GetEncoded(); byte[] array3 = new byte[encoded.Length + 1 + array2.Length]; global::System.Array.Copy((global::System.Array)encoded, 0, (global::System.Array)array3, 0, encoded.Length); array3[encoded.Length] = (byte)array2.Length; global::System.Array.Copy((global::System.Array)array2, 0, (global::System.Array)array3, encoded.Length + 1, array2.Length); return(array3); }
private byte[] RecoverSessionData(PgpPrivateKey privKey) { byte[][] encSessionKey = keyData.GetEncSessionKey(); if (keyData.Algorithm == PublicKeyAlgorithmTag.EC) { ECDHPublicBcpgKey eCDHPublicBcpgKey = (ECDHPublicBcpgKey)privKey.PublicKeyPacket.Key; X9ECParameters x9ECParameters = ECKeyPairGenerator.FindECCurveByOid(eCDHPublicBcpgKey.CurveOid); byte[] array = encSessionKey[0]; int num = (((array[0] & 0xFF) << 8) + (array[1] & 0xFF) + 7) / 8; byte[] array2 = new byte[num]; global::System.Array.Copy((global::System.Array)array, 2, (global::System.Array)array2, 0, num); byte[] array3 = new byte[array[num + 2]]; global::System.Array.Copy((global::System.Array)array, 2 + num + 1, (global::System.Array)array3, 0, array3.Length); ECPoint eCPoint = x9ECParameters.Curve.DecodePoint(array2); ECPrivateKeyParameters eCPrivateKeyParameters = (ECPrivateKeyParameters)privKey.Key; ECPoint s = eCPoint.Multiply(eCPrivateKeyParameters.D).Normalize(); KeyParameter parameters = new KeyParameter(Rfc6637Utilities.CreateKey(privKey.PublicKeyPacket, s)); IWrapper wrapper = PgpUtilities.CreateWrapper(eCDHPublicBcpgKey.SymmetricKeyAlgorithm); wrapper.Init(forWrapping: false, parameters); return(PgpPad.UnpadSessionData(wrapper.Unwrap(array3, 0, array3.Length))); } IBufferedCipher keyCipher = GetKeyCipher(keyData.Algorithm); try { keyCipher.Init(forEncryption: false, privKey.Key); } catch (InvalidKeyException exception) { throw new PgpException("error setting asymmetric cipher", exception); } if (keyData.Algorithm == PublicKeyAlgorithmTag.RsaEncrypt || keyData.Algorithm == PublicKeyAlgorithmTag.RsaGeneral) { byte[] array4 = encSessionKey[0]; keyCipher.ProcessBytes(array4, 2, array4.Length - 2); } else { ElGamalPrivateKeyParameters elGamalPrivateKeyParameters = (ElGamalPrivateKeyParameters)privKey.Key; int size = (elGamalPrivateKeyParameters.Parameters.P.BitLength + 7) / 8; ProcessEncodedMpi(keyCipher, size, encSessionKey[0]); ProcessEncodedMpi(keyCipher, size, encSessionKey[1]); } try { return(keyCipher.DoFinal()); } catch (global::System.Exception exception2) { throw new PgpException("exception decrypting secret key", exception2); } }
private byte[] EncryptSessionInfo(byte[] sessionInfo, SecureRandom random) { if (pubKey.Algorithm != PublicKeyAlgorithmTag.ECDH) { IBufferedCipher c; switch (pubKey.Algorithm) { case PublicKeyAlgorithmTag.RsaEncrypt: case PublicKeyAlgorithmTag.RsaGeneral: c = CipherUtilities.GetCipher("RSA//PKCS1Padding"); break; case PublicKeyAlgorithmTag.ElGamalEncrypt: case PublicKeyAlgorithmTag.ElGamalGeneral: c = CipherUtilities.GetCipher("ElGamal/ECB/PKCS1Padding"); break; case PublicKeyAlgorithmTag.Dsa: throw new PgpException("Can't use DSA for encryption."); case PublicKeyAlgorithmTag.ECDsa: throw new PgpException("Can't use ECDSA for encryption."); default: throw new PgpException("unknown asymmetric algorithm: " + pubKey.Algorithm); } AsymmetricKeyParameter akp = pubKey.GetKey(); c.Init(true, new ParametersWithRandom(akp, random)); return(c.DoFinal(sessionInfo)); } ECDHPublicBcpgKey ecKey = (ECDHPublicBcpgKey)pubKey.PublicKeyPacket.Key; // Generate the ephemeral key pair IAsymmetricCipherKeyPairGenerator gen = GeneratorUtilities.GetKeyPairGenerator("ECDH"); gen.Init(new ECKeyGenerationParameters(ecKey.CurveOid, random)); AsymmetricCipherKeyPair ephKp = gen.GenerateKeyPair(); ECPrivateKeyParameters ephPriv = (ECPrivateKeyParameters)ephKp.Private; ECPublicKeyParameters ephPub = (ECPublicKeyParameters)ephKp.Public; ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey.GetKey(); ECPoint S = pub.Q.Multiply(ephPriv.D).Normalize(); KeyParameter key = new KeyParameter(Rfc6637Utilities.CreateKey(pubKey.PublicKeyPacket, S)); IWrapper w = PgpUtilities.CreateWrapper(ecKey.SymmetricKeyAlgorithm); w.Init(true, new ParametersWithRandom(key, random)); byte[] paddedSessionData = PgpPad.PadSessionData(sessionInfo); byte[] C = w.Wrap(paddedSessionData, 0, paddedSessionData.Length); byte[] VB = new MPInteger(new BigInteger(1, ephPub.Q.GetEncoded(false))).GetEncoded(); byte[] rv = new byte[VB.Length + 1 + C.Length]; Array.Copy(VB, 0, rv, 0, VB.Length); rv[VB.Length] = (byte)C.Length; Array.Copy(C, 0, rv, VB.Length + 1, C.Length); return(rv); }
private byte[] RecoverSessionData(PgpPrivateKey privKey) { byte[][] secKeyData = keyData.GetEncSessionKey(); if (keyData.Algorithm == PublicKeyAlgorithmTag.ECDH) { ECDHPublicBcpgKey ecKey = (ECDHPublicBcpgKey)privKey.PublicKeyPacket.Key; X9ECParameters x9Params = ECKeyPairGenerator.FindECCurveByOid(ecKey.CurveOid); byte[] enc = secKeyData[0]; int pLen = ((((enc[0] & 0xff) << 8) + (enc[1] & 0xff)) + 7) / 8; byte[] pEnc = new byte[pLen]; Array.Copy(enc, 2, pEnc, 0, pLen); byte[] keyEnc = new byte[enc[pLen + 2]]; Array.Copy(enc, 2 + pLen + 1, keyEnc, 0, keyEnc.Length); ECPoint publicPoint = x9Params.Curve.DecodePoint(pEnc); ECPrivateKeyParameters privKeyParams = (ECPrivateKeyParameters)privKey.Key; ECPoint S = publicPoint.Multiply(privKeyParams.D).Normalize(); KeyParameter key = new KeyParameter(Rfc6637Utilities.CreateKey(privKey.PublicKeyPacket, S)); IWrapper w = PgpUtilities.CreateWrapper(ecKey.SymmetricKeyAlgorithm); w.Init(false, key); return(PgpPad.UnpadSessionData(w.Unwrap(keyEnc, 0, keyEnc.Length))); } IBufferedCipher cipher = GetKeyCipher(keyData.Algorithm); try { cipher.Init(false, privKey.Key); } catch (InvalidKeyException e) { throw new PgpException("error setting asymmetric cipher", e); } if (keyData.Algorithm == PublicKeyAlgorithmTag.RsaEncrypt || keyData.Algorithm == PublicKeyAlgorithmTag.RsaGeneral) { byte[] bi = secKeyData[0]; cipher.ProcessBytes(bi, 2, bi.Length - 2); } else { ElGamalPrivateKeyParameters k = (ElGamalPrivateKeyParameters)privKey.Key; int size = (k.Parameters.P.BitLength + 7) / 8; ProcessEncodedMpi(cipher, size, secKeyData[0]); ProcessEncodedMpi(cipher, size, secKeyData[1]); } try { return(cipher.DoFinal()); } catch (Exception e) { throw new PgpException("exception decrypting secret key", e); } }