private static void TestTripleDESTransformDirectKey( CipherMode cipherMode, PaddingMode paddingMode, byte[] key, byte[] iv, byte[] plainBytes, byte[] cipherBytes, int?feedbackSize = default) { byte[] liveEncryptBytes; byte[] liveDecryptBytes; byte[] liveOneShotDecryptBytes = null; byte[] liveOneShotEncryptBytes = null; using (TripleDES tdes = TripleDESFactory.Create()) { tdes.Mode = cipherMode; tdes.Padding = paddingMode; tdes.Key = key; if (feedbackSize.HasValue) { tdes.FeedbackSize = feedbackSize.Value; } liveEncryptBytes = TripleDESEncryptDirectKey(tdes, key, iv, plainBytes); liveDecryptBytes = TripleDESDecryptDirectKey(tdes, key, iv, cipherBytes); if (cipherMode == CipherMode.ECB) { liveOneShotDecryptBytes = tdes.DecryptEcb(cipherBytes, paddingMode); liveOneShotEncryptBytes = tdes.EncryptEcb(plainBytes, paddingMode); } else if (cipherMode == CipherMode.CBC) { liveOneShotDecryptBytes = tdes.DecryptCbc(cipherBytes, iv, paddingMode); liveOneShotEncryptBytes = tdes.EncryptCbc(plainBytes, iv, paddingMode); } else if (cipherMode == CipherMode.CFB) { liveOneShotDecryptBytes = tdes.DecryptCfb(cipherBytes, iv, paddingMode, feedbackSizeInBits: feedbackSize.Value); liveOneShotEncryptBytes = tdes.EncryptCfb(plainBytes, iv, paddingMode, feedbackSizeInBits: feedbackSize.Value); } if (liveOneShotDecryptBytes is not null) { Assert.Equal(plainBytes, liveOneShotDecryptBytes); } if (liveOneShotEncryptBytes is not null) { Assert.Equal(cipherBytes, liveOneShotEncryptBytes); } } Assert.Equal(cipherBytes, liveEncryptBytes); Assert.Equal(plainBytes, liveDecryptBytes); }
public static void EcbRoundtrip(byte[] plaintext, byte[] ciphertext, PaddingMode padding) { using (TripleDES tdes = TripleDESFactory.Create()) { tdes.Key = s_tdes192OneShotKey; // Even though we have set the instance to use CFB, the Ecb one shots should // always be done in ECB. tdes.FeedbackSize = 8; tdes.Mode = CipherMode.CFB; tdes.Padding = padding == PaddingMode.None ? PaddingMode.PKCS7 : PaddingMode.None; byte[] encrypted = tdes.EncryptEcb(plaintext, padding); byte[] decrypted = tdes.DecryptEcb(encrypted, padding); if (padding == PaddingMode.Zeros) { Assert.Equal(plaintext, decrypted[..plaintext.Length]);