Пример #1
0
    // Decode the specified byte array using CryptoAPITranform.
    private static byte[] DecodeBytes(byte[] sourceBytes)
    {
        byte[] targetBytes     = new byte[1024];
        int    currentPosition = 0;

        // Create a DES decryptor from this instance to perform decryption.
        CryptoAPITransform cryptoTransform =
            (CryptoAPITransform)des.CreateDecryptor();

        int inputBlockSize   = cryptoTransform.InputBlockSize;
        int sourceByteLength = sourceBytes.Length;

        try
        {
            int numBytesRead = 0;
            while (sourceByteLength - currentPosition >= inputBlockSize)
            {
                // Transform the bytes from current position in the
                // sourceBytes array, writing the bytes to the targetBytes
                // array.
                numBytesRead = cryptoTransform.TransformBlock(
                    sourceBytes,
                    currentPosition,
                    inputBlockSize,
                    targetBytes,
                    currentPosition);

                // Advance the current position in the source array.
                currentPosition += numBytesRead;
            }

            // Transform the final block of bytes.
            byte[] finalBytes = cryptoTransform.TransformFinalBlock(
                sourceBytes,
                currentPosition,
                sourceByteLength - currentPosition);

            // Copy the contents of the finalBytes array to the targetBytes
            // array.
            finalBytes.CopyTo(targetBytes, currentPosition);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Caught unexpected exception:" + ex.ToString());
        }

        // Strip out the second block of bytes.
        Array.Copy(targetBytes, (inputBlockSize * 2), targetBytes, inputBlockSize, targetBytes.Length - (inputBlockSize * 2));

        // Trim the extra bytes in the array that were not used.
        return(TrimArray(targetBytes));
    }
Пример #2
0
        /// <summary>
        /// Extends TransformFinalBlock so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// cryptoapitransform.TransformFinalBlock(inputBuffer);
        /// </example>
        /// </summary>
        public static Byte[] TransformFinalBlock(this CryptoAPITransform cryptoapitransform, Byte[] inputBuffer)
        {
            if (cryptoapitransform == null)
            {
                throw new ArgumentNullException("cryptoapitransform");
            }

            if (inputBuffer == null)
            {
                throw new ArgumentNullException("inputBuffer");
            }

            return(cryptoapitransform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
        }
Пример #3
0
        /// <summary>
        /// Extends TransformBlock so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// cryptoapitransform.TransformBlock(inputBuffer, outputBuffer, outputOffset);
        /// </example>
        /// </summary>
        public static Int32 TransformBlock(this CryptoAPITransform cryptoapitransform, Byte[] inputBuffer, Byte[] outputBuffer, Int32 outputOffset)
        {
            if (cryptoapitransform == null)
            {
                throw new ArgumentNullException("cryptoapitransform");
            }

            if (inputBuffer == null)
            {
                throw new ArgumentNullException("inputBuffer");
            }

            return(cryptoapitransform.TransformBlock(inputBuffer, 0, inputBuffer.Length, outputBuffer, outputOffset));
        }
Пример #4
0
        public void KeyHandle()
        {
            CryptoAPITransform transform = null;
            Type t = typeof(CryptoAPITransform);

            // Mono has a default, internal, constructor
            ConstructorInfo ci = t.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, Type.EmptyTypes, null);

            if (ci != null)
            {
                transform = (CryptoAPITransform)ci.Invoke(null);
            }
            else
            {
                // while MS use CryptoAPITransform for all unmanaged crypto
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                transform = (CryptoAPITransform)des.CreateEncryptor();
            }

            Assert.AreEqual(IntPtr.Zero, transform.KeyHandle, "KeyHandle");
        }
Пример #5
0
    // Encode the specified byte array by using CryptoAPITranform.
    private static byte[] EncodeBytes(byte[] sourceBytes)
    {
        int currentPosition = 0;

        byte[] targetBytes      = new byte[1024];
        int    sourceByteLength = sourceBytes.Length;

        // Create a DES encryptor from this instance to perform encryption.
        CryptoAPITransform cryptoTransform =
            (CryptoAPITransform)des.CreateEncryptor();

        // Retrieve the block size to read the bytes.
        int inputBlockSize = cryptoTransform.InputBlockSize;

        // Retrieve the key handle.
        IntPtr keyHandle = cryptoTransform.KeyHandle;

        // Retrieve the block size to write the bytes.
        int outputBlockSize = cryptoTransform.OutputBlockSize;

        try
        {
            // Determine if multiple blocks can be transformed.
            if (cryptoTransform.CanTransformMultipleBlocks)
            {
                int numBytesRead = 0;
                while (sourceByteLength - currentPosition >= inputBlockSize)
                {
                    // Transform the bytes from currentPosition in the
                    // sourceBytes array, writing the bytes to the targetBytes
                    // array.
                    numBytesRead = cryptoTransform.TransformBlock(
                        sourceBytes,
                        currentPosition,
                        inputBlockSize,
                        targetBytes,
                        currentPosition);

                    // Advance the current position in the sourceBytes array.
                    currentPosition += numBytesRead;
                }

                // Transform the final block of bytes.
                byte[] finalBytes = cryptoTransform.TransformFinalBlock(
                    sourceBytes,
                    currentPosition,
                    sourceByteLength - currentPosition);

                // Copy the contents of the finalBytes array to the
                // targetBytes array.
                finalBytes.CopyTo(targetBytes, currentPosition);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Caught unexpected exception:" + ex.ToString());
        }

        // Determine if the current transform can be reused.
        if (!cryptoTransform.CanReuseTransform)
        {
            // Free up any used resources.
            cryptoTransform.Clear();
        }

        // Trim the extra bytes in the array that were not used.
        return(TrimArray(targetBytes));
    }