예제 #1
0
        /// <summary>
        /// Gets an <see cref="ICryptoTransform"/> instance to use for a given cryptographic key.
        /// </summary>
        /// <param name="key">The cryptographic key to use in the transform.</param>
        /// <param name="iv">The initialization vector to use, when applicable.</param>
        /// <param name="encrypting"><c>true</c> if encrypting; <c>false</c> if decrypting.</param>
        /// <returns>An instance of <see cref="ICryptoTransform"/>.</returns>
        private static ICryptoTransform GetTransformForBlockMode(CryptographicKey key, byte[] iv, bool encrypting)
        {
            Requires.NotNull(key, "key");

            var bufferOperation = encrypting
               ? new Func <byte[], byte[]>(input => Platform.Core.CryptographicEngine.Encrypt(key.Key, input.ToBuffer(), iv.ToBuffer()).ToArray())
               : new Func <byte[], byte[]>(input => Platform.Core.CryptographicEngine.Decrypt(key.Key, input.ToBuffer(), iv.ToBuffer()).ToArray());

            return(new BufferingCryptoTransform(bufferOperation));
        }
예제 #2
0
 /// <summary>
 /// Checks whether the given length is a valid one for an input buffer to the symmetric algorithm.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="lengthInBytes">The length of the input buffer in bytes.</param>
 /// <returns>
 ///   <c>true</c> if the size is allowed; <c>false</c> otherwise.
 /// </returns>
 private bool IsValidInputSize(CryptographicKey key, int lengthInBytes)
 {
     Requires.NotNull(key, "key");
     return(lengthInBytes > 0 && lengthInBytes % key.SymmetricAlgorithmProvider.BlockLength == 0);
 }
예제 #3
0
 /// <summary>
 /// Checks whether the given length is a valid one for an input buffer to the symmetric algorithm.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="lengthInBytes">The length of the input buffer in bytes.</param>
 /// <returns>
 ///   <c>true</c> if the size is allowed; <c>false</c> otherwise.
 /// </returns>
 private bool IsValidInputSize(CryptographicKey key, int lengthInBytes)
 {
     Requires.NotNull(key, "key");
     return lengthInBytes > 0 && lengthInBytes % key.SymmetricAlgorithmProvider.BlockLength == 0;
 }
예제 #4
0
        /// <summary>
        /// Gets an <see cref="ICryptoTransform"/> instance to use for a given cryptographic key.
        /// </summary>
        /// <param name="key">The cryptographic key to use in the transform.</param>
        /// <param name="iv">The initialization vector to use, when applicable.</param>
        /// <param name="encrypting"><c>true</c> if encrypting; <c>false</c> if decrypting.</param>
        /// <returns>An instance of <see cref="ICryptoTransform"/>.</returns>
        private static ICryptoTransform GetTransformForBlockMode(CryptographicKey key, byte[] iv, bool encrypting)
        {
            Requires.NotNull(key, "key");

            var bufferOperation = encrypting
               ? new Func<byte[], byte[]>(input => Platform.Core.CryptographicEngine.Encrypt(key.Key, input.ToBuffer(), iv.ToBuffer()).ToArray())
               : new Func<byte[], byte[]>(input => Platform.Core.CryptographicEngine.Decrypt(key.Key, input.ToBuffer(), iv.ToBuffer()).ToArray());
            return new BufferingCryptoTransform(bufferOperation);
        }