public void RijndaelManagedSymmetricEncryptionServiceEncryptDecryptTest()
        {
            var symmetricService = new RijndaelManagedSymmetricEncryptionService();
            var validKey = symmetricService.GenerateSymmetricKey(128);
            Assert.AreEqual(true, validKey.Success);

            var encryptionArgs = new SymmetricEncryptionArguments
            {
                BlockSize = 128,
                CipherMode = System.Security.Cryptography.CipherMode.CBC,
                KeySize = 256,
                PaddingMode = System.Security.Cryptography.PaddingMode.ISO10126,
                PlainText = "Text to be encoded",
                SymmetricPublicKey = validKey.SymmetricKey
            };

            var encryptionResult = symmetricService.Encrypt(encryptionArgs);
            Assert.AreEqual(true, encryptionResult.Success);

            var decryptionArgs = new SymmetricDecryptionArguments
            {
                BlockSize = encryptionArgs.BlockSize,
                CipherMode = encryptionArgs.CipherMode,
                CipherTextBase64Encoded = encryptionResult.CipherText,
                InitialisationVectorBase64Encoded = encryptionResult.InitialisationVector,
                KeySize = encryptionArgs.KeySize,
                PaddingMode = encryptionArgs.PaddingMode,
                SymmetricPublicKey = validKey.SymmetricKey
            };

            var decryptionResult = symmetricService.Decrypt(decryptionArgs);
            Assert.AreEqual(true, decryptionResult.Success);
        }
 public CipherTextDecryptionResult Decrypt(SymmetricDecryptionArguments arguments)
 {
     var res = new CipherTextDecryptionResult();
     try
     {
         var cipher = CreateCipher(arguments);
         cipher.IV = Convert.FromBase64String(arguments.InitialisationVectorBase64Encoded);
         ICryptoTransform cryptTransform = cipher.CreateDecryptor();
         byte[] cipherTextBytes = Convert.FromBase64String(arguments.CipherTextBase64Encoded);
         byte[] plainText = cryptTransform.TransformFinalBlock(cipherTextBytes, 0, cipherTextBytes.Length);
         res.DecodedText = Encoding.UTF8.GetString(plainText);
         res.Success = true;
     }
     catch (Exception ex)
     {
         res.ExceptionMessage = ex.Message;
     }
     return res;
 }
示例#3
0
        public CipherTextDecryptionResult Decrypt(SymmetricDecryptionArguments arguments)
        {
            var res = new CipherTextDecryptionResult();

            try
            {
                var cipher = CreateCipher(arguments);
                cipher.IV = Convert.FromBase64String(arguments.InitialisationVectorBase64Encoded);
                ICryptoTransform cryptTransform  = cipher.CreateDecryptor();
                byte[]           cipherTextBytes = Convert.FromBase64String(arguments.CipherTextBase64Encoded);
                byte[]           plainText       = cryptTransform.TransformFinalBlock(cipherTextBytes, 0, cipherTextBytes.Length);
                res.DecodedText = Encoding.UTF8.GetString(plainText);
                res.Success     = true;
            }
            catch (Exception ex)
            {
                res.ExceptionMessage = ex.Message;
            }
            return(res);
        }