
#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.Server.EncryptionSink
{
    internal class 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 int32Position = (int)inStream.Position;

            int dataLen = (int)inStream.Length - int32Position;            
            
            if(dataLen != 0)
            {
                byte[] dataToEncrypt = new byte[dataLen];
                int pos = int32Position;
                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)
			                                      
        {
            // setup decryption properties
            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 int32Position = (int)inStream.Position;

            int dataLen = (int)inStream.Length - int32Position;
                        
            if(dataLen != 0)
            {
                byte[] dataToDecrypt = new byte[dataLen];
                int pos = int32Position;
                inStream.Read(dataToDecrypt,0,dataLen);                            

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

            return inStream;
        }
    }
}

