
#region Copyright
// (c) Copyright Axalto Inc., 2006.                    
// All rights reserved.
//
// This file is part of the SmartCard.NET Framework SDK Code Samples.
//
// This source code is intended only as a supplement to Axalto
// Development Tools and/or on-line documentation.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
#endregion

using System;
using System.IO;
using System.Security.Cryptography;

namespace MyCompany.Client.EncryptionSink
{
	internal class EncryptionHelper
	{
        private EncryptionHelper() { }

		public static Stream ProcessOutboundStream(
			Stream inStream, 
			String algorithm,
			byte[] encryptionkey)
			
		{
			// setup the encryption properties
			SymmetricAlgorithm alg = SymmetricAlgorithm.Create(algorithm);
		    
            byte[] ICV = null;
            if(algorithm == "Rijndael")
            {
                ICV = new byte[16];
            }
            else
            {
                ICV = new byte[8];
            }

            ICryptoTransform transform = alg.CreateEncryptor(encryptionkey,ICV);

            int dataLen = (int)inStream.Length;
            
            if(dataLen != 0)
            {
                byte[] dataToEncrypt = new byte[dataLen];
                long pos = inStream.Position;
                inStream.Read(dataToEncrypt,0,dataLen);
            
                byte[] output = transform.TransformFinalBlock(dataToEncrypt,0,dataLen);
            
                inStream.Position = pos;
                inStream.Write(output,0,output.Length);
                inStream.Position = pos;
            }
		
			return inStream;
		}

		public static Stream ProcessInboundStream(Stream inStream,
			                                      String algorithm,
			                                      byte[] encryptionkey)
			                                      
		{
			SymmetricAlgorithm alg = SymmetricAlgorithm.Create(algorithm);

            byte[] ICV = null;
            if(algorithm == "Rijndael")
            {
                ICV = new byte[16];
            }
            else
            {
                ICV = new byte[8];
            }
		
            ICryptoTransform transform = alg.CreateDecryptor(encryptionkey,ICV);
            
            int dataLen = (int)(inStream.Length - inStream.Position);
            
            if(dataLen != 0)
            {
                byte[] dataToDecrypt = new byte[dataLen];
                long pos = inStream.Position;
                inStream.Read(dataToDecrypt,0,dataLen);            

                byte[] output = transform.TransformFinalBlock(dataToDecrypt,0,dataLen);
                inStream.Position = pos;
            
                inStream.Write(output,0,output.Length);
                inStream.Position = pos;
            }
            else
            {
                inStream.Position = 0;
            }

			return inStream;
		}

	}
}

