public SaltedBytesCache(
            SecureHead secureHead,
            SecurePassword securePassword)
        {
            if (secureHead == null)
            {
                throw new ArgumentException("secureHead should NOT be null.");
            }
            if (securePassword == null)
            {
                throw new ArgumentException("securePassword should NOT be null.");
            }
            byte[] saltedPasswordBytes = GetSaltedPassowrdBytes(secureHead, securePassword);
            if (BytesHelper.IsNullOrEmptyArray(saltedPasswordBytes) == true)
            {
                throw new ArgumentException("saltedPasswordBytes should NOT be null.");
            }
            byte[] saltedInitialVectorBytes = GetSaltedInitialVectorBytes(secureHead);
            if (BytesHelper.IsNullOrEmptyArray(saltedInitialVectorBytes) == true)
            {
                throw new ArgumentException("saltedInitialVectorBytes should NOT be null.");
            }

            SecureHead              = secureHead;
            SaltedPasswordBytes     = saltedPasswordBytes;
            SaltedInitiaVectorBytes = saltedInitialVectorBytes;
        }
        public byte[] Decrypt(byte[] secureBytes, uint offset, uint count, SecureHead secureHead)
        {
            string error = null;

            byte[] bytes = null;

            if (isDisposed == true)
            {
                error = "security manager has been disposed.";
            }
            if (BytesHelper.IsNullOrEmptyArray(secureBytes) == true)
            {
                error = "secureBytes array should NOT be null or empty.";
            }
            if (Config.IsValid == false)
            {
                error = "Config is not valid.";
            }
            if (secureHead == null)
            {
                error = "secureHead should NOT be null.";
            }

            if (error == null)
            {
                try
                {
                    SaltedBytesCache decryptionCache = GetDecryptionSaltedBytesCache(secureHead);
                    bytes = AesDecrypt(
                        secureBytes,
                        offset,
                        count,
                        decryptionCache.SaltedPasswordBytes,
                        decryptionCache.SaltedInitiaVectorBytes);
                }
                catch (Exception e)
                {
#if DEBUG
                    Env.Instance.ShowMessage($"decrypt exception desc={e.Message}  stack trace={e.StackTrace}");
#endif
                }
            }
            else
            {
#if DEBUG
                Env.Instance.ShowMessage(error);
#endif
            }



            return(bytes);
        }
Example #3
0
        //class
        public static void DecodeByteCounts(byte[] bytes, uint offset, out uint saltBytesCount, out uint initiaVectorBytesCount)
        {
            if (BytesHelper.IsNullOrEmptyArray(bytes) == true ||
                bytes.Length - offset < 2 * sizeof(uint))
            {
                throw new ArgumentException(
                          string.Format("bytes array should have {0} bytes at least.",
                                        2 * sizeof(uint)));
            }

            saltBytesCount         = BytesHelper.GetUInt32(bytes, offset);
            initiaVectorBytesCount = BytesHelper.GetUInt32(bytes, offset + sizeof(uint));
        }
Example #4
0
        public static SecureHead Decode(byte[] bytes, uint offset, uint saltBytesCount, uint initiaVectorBytesCount)
        {
            uint totalBytesCount = GetPart2BytesCount() + saltBytesCount + initiaVectorBytesCount;

            if (BytesHelper.IsNullOrEmptyArray(bytes) == true ||
                bytes.Length - offset < totalBytesCount)
            {
                throw new ArgumentException(
                          string.Format("bytes array should have {0} bytes at least.",
                                        totalBytesCount));
            }

            //part 2 (4 uints):
            uint beginIndex     = offset;
            uint length         = sizeof(uint);
            uint saltIterations = BytesHelper.GetUInt32(bytes, beginIndex);

            beginIndex += length;
            length      = sizeof(uint);
            uint saltedInitiaVectorBytesCount = BytesHelper.GetUInt32(bytes, beginIndex);

            beginIndex += length;
            length      = sizeof(uint);
            uint saltedPasswordBytesCount = BytesHelper.GetUInt32(bytes, beginIndex);

            beginIndex += length;
            length      = sizeof(uint);
            uint secureDataBytes = BytesHelper.GetUInt32(bytes, beginIndex);

            //part 3 (2 byte arraries):
            beginIndex += length;
            length      = saltBytesCount;
            string salt = BytesHelper.GetUTF8String(bytes, beginIndex, length);

            beginIndex += length;
            length      = initiaVectorBytesCount;
            string initiaVector = BytesHelper.GetUTF8String(bytes, beginIndex, length);

            return(new SecureHead(
                       salt,
                       initiaVector,
                       saltIterations,
                       saltedInitiaVectorBytesCount,
                       saltedPasswordBytesCount,
                       secureDataBytes));
        }
        private byte[] EncryptPlainBytes(byte[] plainBytes, uint offset, uint count)
        {
            if (isDisposed == true)
            {
                throw new ArgumentException("security manager has been disposed.");
            }

            if (BytesHelper.IsNullOrEmptyArray(plainBytes) == true)
            {
                throw new ArgumentException("plainBytes array should NOT be null or empty.");
            }

            if (Config.IsValid == false)
            {
                throw new ArgumentException("Config is not valid.");
            }

            if (encryptionCache == null)
            {
                encryptionCache = new SaltedBytesCache(
                    new SecureHead(
                        Config.Salt,
                        Config.InitiaVector,
                        Config.SaltIterations,
                        Config.SaltedInitialVectorBytesCount,
                        Config.SaltedPasswordBytesCount,
                        0),
                    Config.Password);
            }
            if (encryptionCache == null)
            {
                throw new ArgumentException("encryptionCache array should NOT be null.");
            }

            return(AesEncrypt(
                       plainBytes,
                       offset,
                       count,
                       encryptionCache.SaltedPasswordBytes,
                       encryptionCache.SaltedInitiaVectorBytes));
        }