Exemple #1
0
        public void KeyIsNotNull()
        {
            // Arrange
            var rc2 = new RC2();

            // Act
            var wrong = rc2.Encrypt(_valueDecrypt, "");

            // Assert - Expects exception
        }
Exemple #2
0
        public void ExpectedCrypto()
        {
            // Arrange
            var rc2 = new RC2();

            // Act
            var right = rc2.Encrypt(_valueDecrypt, _key);

            // Assert
            Assert.AreEqual(_valueEncrypt, right);
        }
Exemple #3
0
        public static void RC2RoundTrip(CipherMode cipherMode, PaddingMode paddingMode, string key, string iv, string textHex, string expectedDecrypted, string expectedEncrypted)
        {
            byte[] expectedDecryptedBytes = expectedDecrypted == null?textHex.HexToByteArray() : expectedDecrypted.HexToByteArray();

            byte[] expectedEncryptedBytes = expectedEncrypted.HexToByteArray();
            byte[] keyBytes = key.HexToByteArray();

            using (RC2 alg = RC2Factory.Create())
            {
                alg.Key     = keyBytes;
                alg.Padding = paddingMode;
                alg.Mode    = cipherMode;
                if (iv != null)
                {
                    alg.IV = iv.HexToByteArray();
                }

                byte[] cipher = alg.Encrypt(textHex.HexToByteArray());
                Assert.Equal <byte>(expectedEncryptedBytes, cipher);

                byte[] decrypted = alg.Decrypt(cipher);
                Assert.Equal <byte>(expectedDecryptedBytes, decrypted);

                if (RC2Factory.OneShotSupported)
                {
                    byte[] oneShotEncrypt = cipherMode switch
                    {
                        CipherMode.ECB => alg.EncryptEcb(textHex.HexToByteArray(), paddingMode),
                        CipherMode.CBC => alg.EncryptCbc(textHex.HexToByteArray(), iv.HexToByteArray(), paddingMode),
                        _ => throw new NotImplementedException(),
                    };

                    Assert.Equal(expectedEncryptedBytes, oneShotEncrypt);

                    byte[] oneShotDecrypt = cipherMode switch
                    {
                        CipherMode.ECB => alg.DecryptEcb(cipher, paddingMode),
                        CipherMode.CBC => alg.DecryptCbc(cipher, iv.HexToByteArray(), paddingMode),
                        _ => throw new NotImplementedException(),
                    };

                    Assert.Equal(expectedDecryptedBytes, oneShotDecrypt);
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var iv      = this.EncryptIv.Text;
                var key     = this.EncryptCryptoKey.Text;
                var message = this.EncryptNomalMessage.Text;

                var crp = new RC2(key, message);
                crp.IV = iv;

                crp.Encrypt();

                this.EncryptResult.Text = crp.EncryptMessage;
            }catch (Exception ex)
            {
                this.EncryptResult.Text = ex.Message;
            }
        }
Exemple #5
0
        public static void RC2RoundTrip(CipherMode cipherMode, PaddingMode paddingMode, string key, string iv, string textHex, string expectedDecrypted, string expectedEncrypted)
        {
            byte[] expectedDecryptedBytes = expectedDecrypted == null?textHex.HexToByteArray() : expectedDecrypted.HexToByteArray();

            byte[] expectedEncryptedBytes = expectedEncrypted.HexToByteArray();
            byte[] keyBytes = key.HexToByteArray();

            using (RC2 alg = RC2Factory.Create())
            {
                alg.Key     = keyBytes;
                alg.Padding = paddingMode;
                alg.Mode    = cipherMode;
                if (iv != null)
                {
                    alg.IV = iv.HexToByteArray();
                }

                byte[] cipher = alg.Encrypt(textHex.HexToByteArray());
                Assert.Equal <byte>(expectedEncryptedBytes, cipher);

                byte[] decrypted = alg.Decrypt(cipher);
                Assert.Equal <byte>(expectedDecryptedBytes, decrypted);
            }
        }