public override IList <byte[]> Encrypt(byte[] binaryData, AesKey usingKey)
 {
     return(new List <byte[]>()
     {
         EncryptionAlgo.Encrypt(binaryData, usingKey)
     });
 }
Example #2
0
        public override IList <byte[]> Encrypt(byte[] binaryData, RsaKey usingKey)
        {
            // Get maximum bytes allowed and encrypt segments of binaryData based on this
            // Refactor if you have the restriction with other encryption algorithms
            var maxBytestoEncrypt = _maxEncryptionCalc.GetMaxBytesThatCanBeEncrypted(usingKey);
            var ret = new List <byte[]>((int)Math.Max(1, Math.Ceiling((double)binaryData.Length / maxBytestoEncrypt)));

            for (int i = 0; i < binaryData.Length; i = i + maxBytestoEncrypt)
            {
                var currSegmentSize  = Math.Min(binaryData.Length - i, maxBytestoEncrypt);
                var segmentToEncrypt = new byte[currSegmentSize];
                Buffer.BlockCopy(binaryData, i, segmentToEncrypt, 0, currSegmentSize);

                ret.Add(EncryptionAlgo.Encrypt(segmentToEncrypt, usingKey));
            }

            return(ret);
        }
Example #3
0
        /// <summary>
        /// Creates a new instance of the <see cref="SecuritySettings"/> class.
        /// </summary>
        /// <param name="encryptionAlgo">The algorithm used to encrypt data in the archive.</param>
        /// <param name="keyDerivationFunction">The key derivation function (KDF) used to generate user keys.</param>
        /// <param name="keyDerivationWorkFactor">The work factor parameter (e.g. iteration count) for the KDF.</param>
        /// <param name="KeyIdSizeInBytes">The size (in bytes) of KeyIds generated for user keys.</param>
        public static SecuritySettings Create(
            EncryptionAlgo encryptionAlgo,
            KeyDerivationFunction keyDerivationFunction,
            int keyDerivationWorkFactor,
            int keyIdSizeInBytes)
        {
            ArgCheck.IsNot(EncryptionAlgo.Unknown, encryptionAlgo, nameof(encryptionAlgo));
            ArgCheck.IsNot(KeyDerivationFunction.Unknown, keyDerivationFunction, nameof(keyDerivationFunction));
            ArgCheck.GreaterThanZero(keyDerivationWorkFactor, nameof(keyDerivationWorkFactor));
            ArgCheck.GreaterThanZero(keyIdSizeInBytes, nameof(keyIdSizeInBytes));

            return(new SecuritySettings
            {
                EncryptionAlgo = encryptionAlgo,
                KeyDerivationFunction = keyDerivationFunction,
                KeyDerivationWorkFactor = keyDerivationWorkFactor,
                KeyIdSizeInBytes = keyIdSizeInBytes,
            });
        }
Example #4
0
        /// <summary>
        /// Reads a pure binary form <see cref="EncryptedPacket"/> from the given stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="encryptionAlgo">The algo used to create the encrypted packet.</param>
        /// <returns>The <see cref="EncryptedPacket"/> read from the stream.</returns>
        public static EncryptedPacket ReadFromBinaryStream(Stream stream, EncryptionAlgo encryptionAlgo)
        {
            byte[] iv         = null;
            byte[] authTag    = null;
            byte[] cipherText = null;

            var encryptionAlgoProperties = EncryptionAlgoProperties.For(encryptionAlgo);

            if (encryptionAlgoProperties.IvSizeInBytes > 0)
            {
                iv = new byte[encryptionAlgoProperties.IvSizeInBytes];

                var ivBytesRead = stream.Read(iv, 0, encryptionAlgoProperties.IvSizeInBytes);
                Debug.Assert(ivBytesRead == encryptionAlgoProperties.IvSizeInBytes);
            }

            if (encryptionAlgoProperties.IsAuthenticated && encryptionAlgoProperties.AuthTagSizeInBytes > 0)
            {
                authTag = new byte[encryptionAlgoProperties.AuthTagSizeInBytes];

                var authTagBytesRead = stream.Read(authTag);
                Debug.Assert(authTagBytesRead == encryptionAlgoProperties.AuthTagSizeInBytes);
            }

            var cipherTextSizeInBytes = stream.Length - stream.Position;

            if (cipherTextSizeInBytes > 0)
            {
                cipherText = new byte[cipherTextSizeInBytes];

                var cipherTextBytesRead = stream.Read(cipherText);
                Debug.Assert(cipherTextBytesRead == cipherTextSizeInBytes);
            }

            return(CreateNewEncryptedPacket(cipherText, iv, authTag));
        }
Example #5
0
 /// <summary>
 /// Gets the <see cref="ICryptoStrategy"/> associated with the given cryptographic algorithm.
 /// </summary>
 /// <param name="algo">The cryptographic algorithm.</param>
 /// <returns>The <see cref="ICryptoStrategy"/> associated with the algorithm.</returns>
 public static ICryptoStrategy GetCryptoStrategy(EncryptionAlgo algo) => algo switch
 {
Example #6
0
 /// <summary>
 /// Gets the <see cref="EncryptionAlgoProperties"/> for a given <see cref="EncryptionAlgo"/>.
 /// </summary>
 /// <param name="algo">The <see cref="EncryptionAlgo"/> to get properties for.</param>
 /// <returns>The <see cref="EncryptionAlgoProperties"/>.</returns>
 public static EncryptionAlgoProperties For(EncryptionAlgo algo) => algo switch
 {