/// <param name="cipherMode">The cipher mode.</param>
 /// <param name="paddingMode">Padding algorithm to use</param>
 public BlowfishEncryptor(
     BlowfishCipherMode cipherMode,
     PaddingMode paddingMode = DefaultPaddingMode
     ) : this(cipherMode, paddingMode, FastRandom.StaticInstance,
              SafeOrbitCore.Current.Factory.Get <IPadderFactory>())
 {
 }
Пример #2
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="BlowfishEncryptor" /> class with a defined
 ///     <see cref="BlowfishCipherMode" /> and <see cref="ICryptoRandom" />.
 /// </summary>
 /// <param name="chiperMode">The chiper mode.</param>
 /// <param name="random">The random generator to be used for creation of IV's.</param>
 /// <exception cref="UnexpectedEnumValueException{BlowfishCipherMode}">
 ///     <paramref name="chiperMode" /> is not defined in <see cref="BlowfishCipherMode" />.
 /// </exception>
 /// <seealso cref="BlowfishCipherMode" />
 /// <seealso cref="IvSize" />
 /// <seealso cref="ICryptoRandom" />
 public BlowfishEncryptor(BlowfishCipherMode chiperMode, ICryptoRandom random) : base(random)
 {
     if (((int)chiperMode != 0) && ((int)chiperMode != 1))
     {
         throw new UnexpectedEnumValueException <BlowfishCipherMode>(chiperMode);
     }
     CipherMode = chiperMode;
 }
 /// <exception cref="UnexpectedEnumValueException{TEnum}">
 ///     <paramref name="cipherMode" /> is not defined in
 ///     <see cref="BlowfishCipherMode" />.
 /// </exception>
 /// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" /></exception>
 internal BlowfishEncryptor(BlowfishCipherMode cipherMode, PaddingMode paddingMode, ICryptoRandom random,
                            IPadderFactory padderFactory) : base(random, paddingMode, padderFactory)
 {
     if ((int)cipherMode != 0 && (int)cipherMode != 1)
     {
         throw new UnexpectedEnumValueException <BlowfishCipherMode>(cipherMode);
     }
     CipherMode = cipherMode;
 }
        public async Task Encrypt_DifferentDataSizes_CanDecrypt(int dataSize,
                                                                BlowfishCipherMode cipherMode) // a.k.a. padding works
        {
            // Arrange
            var expected = FastRandom.StaticInstance.GetBytes(dataSize);
            var sut      = new BlowfishEncryptor(cipherMode, PaddingMode.PKCS7);
            var key      = new byte[sut.MinKeySizeInBits];

            // Act
            var encrypted = await sut.EncryptAsync(expected, key);

            var actual = await sut.DecryptAsync(encrypted, key);

            // Assert
            Assert.That(actual, Is.EqualTo(expected));
        }
Пример #5
0
        public async Task Decrypt_KeySizeIsBetweenMinKeyAndLastKey_CanDecrypt(BlowfishCipherMode cipherMode)
        {
            // Arrange
            var input = (byte[])ByteCases.ByteArray32Length[0];
            var sut   = GetSut(cipherMode);

            for (var i = sut.MinKeySizeInBits / 8; i < sut.MaxKeySizeInBits / 8; i++)
            {
                //act
                var key       = new byte[i];
                var encrypted = await sut.EncryptAsync(input, key);

                var plain = await sut.DecryptAsync(encrypted, key);

                //assert
                Assert.That(input, Is.EqualTo(plain));
            }
        }
Пример #6
0
 private IFastEncryptor GetSut(BlowfishCipherMode cipherMode) => new BlowfishEncryptor(cipherMode, Stubs.Get <IFastRandom>());
Пример #7
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="BlowfishEncryptor" /> class with a defined
 ///     <see cref="BlowfishCipherMode" />.
 /// </summary>
 /// <param name="chiperMode">The chiper mode.</param>
 /// <exception cref="UnexpectedEnumValueException{BlowfishCipherMode}">
 ///     <paramref name="chiperMode" /> is not defined in
 ///     <see cref="BlowfishCipherMode" />.
 /// </exception>
 /// <seealso cref="BlowfishCipherMode" />
 /// <seealso cref="IvSize" />
 public BlowfishEncryptor(BlowfishCipherMode chiperMode) : this(chiperMode, FastRandom.StaticInstance)
 {
 }