/// <summary>Return the decrypted session data for the packet.</summary> private static byte[] GetSessionData(SymmetricKeyEncSessionPacket keyData, ReadOnlySpan <byte> rawPassword) { byte[] key = Array.Empty <byte>(); try { key = new byte[PgpUtilities.GetKeySize(keyData.EncAlgorithm) / 8]; S2kBasedEncryption.MakeKey(rawPassword, keyData.S2k.HashAlgorithm, keyData.S2k.GetIV(), keyData.S2k.IterationCount, key); if (keyData.SecKeyData?.Length > 0) { using var keyCipher = PgpUtilities.GetSymmetricAlgorithm(keyData.EncAlgorithm); using var keyDecryptor = new ZeroPaddedCryptoTransform(keyCipher.CreateDecryptor(key, new byte[(keyCipher.BlockSize + 7) / 8])); return(keyDecryptor.TransformFinalBlock(keyData.SecKeyData, 0, keyData.SecKeyData.Length)); } else { var sessionData = new byte[key.Length + 1]; sessionData[0] = (byte)keyData.EncAlgorithm; key.CopyTo(sessionData, 1); return(sessionData); } } finally { CryptographicOperations.ZeroMemory(key); } }
/// <summary>Add a PBE encryption method to the encrypted object.</summary> public void AddMethod(ReadOnlySpan <byte> rawPassPhrase, PgpHashAlgorithm s2kDigest) { S2k s2k = PgpUtilities.GenerateS2k(s2kDigest, 0x60); byte[] key = new byte[PgpUtilities.GetKeySize(defAlgorithm) / 8]; S2kBasedEncryption.MakeKey(rawPassPhrase, s2kDigest, s2k.GetIV(), s2k.IterationCount, key); methods.Add(new PbeMethod(defAlgorithm, s2k, key)); }