Example #1
0
        private byte[] Decrypt(byte[] cipherTextBytes, out int byteCount)
        {
            string password = SessionKey.ToHexString();
            string salt     = Salt.ToHexString();

            if (cipherTextBytes == null)
            {
                byteCount = 0;
                return(null);
            }
            byte[] initialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
            byte[] saltValueBytes     = Encoding.ASCII.GetBytes(salt);
            var    derivedPassword    = new PasswordDeriveBytes(password, saltValueBytes, HashAlgorithm, PasswordIterations);
            var    keyBytes           = derivedPassword.GetBytes(KeySize / 8);
            var    symmetricKey       = new RijndaelManaged {
                Mode = CipherMode.CBC
            };
            var plainTextBytes = new byte[cipherTextBytes.Length];

            using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes))
            {
                using (var memStream = new MemoryStream(cipherTextBytes))
                {
                    using (var cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
                    {
                        byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                        memStream.Close();
                        cryptoStream.Close();
                    }
                }
            }
            symmetricKey.Clear();
            return(plainTextBytes);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public string InternalsToString()
        {
            string type = IsServer ? "server" : "client";
            string str  = string.Format("SRP {0} Internals:\n", type);

            str += string.Format("G      = {0}\n", Generator.ToHexString());
            str += string.Format("K      = {0}\n", Multiplier.ToHexString());
            str += string.Format("N      = {0}\n", Modulus.ToHexString());

            str += string.Format("I      = '{0}'\n", Credentials);
            str += string.Format("Hash(I)= {0}\n", Hash(Credentials).ToHexString());
            str += string.Format("X      = {0}\n", CredentialsHash.ToHexString());
            str += string.Format("V      = {0}\n", Verifier.ToHexString());
            if (m_Salt != null)
            {
                str += string.Format("Salt   = {0}\n", Salt.ToHexString());
            }
            if (null != m_publicEphemeralValueA && null != m_publicEphemeralValueB)
            {
                str += string.Format("u      = {0}\n", ScramblingParameter.ToHexString());
                str += string.Format("h(A)   = {0}\n", Hash(PublicEphemeralValueA).ToHexString());
                str += string.Format("h(B)   = {0}\n", Hash(PublicEphemeralValueB.GetBytes()).ToHexString());
            }

            if (IsServer == false || PublicEphemeralValueA != null)
            {
                str += string.Format("A      = {0}\n", PublicEphemeralValueA.ToHexString());
            }
            if (IsServer || PublicEphemeralValueB != null)
            {
                str += string.Format("B      = {0}\n", PublicEphemeralValueB.ToHexString());
                BigInteger tmp = Multiplier * Generator.ModPow(CredentialsHash, Modulus);
                str += string.Format("kg^x   = {0}\n", tmp.ToHexString());
                tmp  = PublicEphemeralValueB - tmp % Modulus;
                if (tmp < 0)
                {
                    tmp += Modulus;
                }
                str += string.Format("B-kg^x = {0}\n", tmp.ToHexString());
            }

            try
            {
                str += string.Format("S.key  = {0}\n", SessionKey.ToHexString());
            }
            catch
            {
                str += "S.key  = empty\n";
            }

            return(str);
        }
Example #3
0
        public byte[] Encrypt(byte[] plainTextBytes)
        {
            string password = SessionKey.ToHexString();
            string salt     = Salt.ToHexString();

            if (plainTextBytes == null)
            {
                return(null);
            }
            byte[] initialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
            byte[] saltValueBytes     = Encoding.ASCII.GetBytes(salt);
            var    derivedPassword    = new PasswordDeriveBytes(password, saltValueBytes, HashAlgorithm, PasswordIterations);

            byte[] keyBytes     = derivedPassword.GetBytes(KeySize / 8);
            var    symmetricKey = new RijndaelManaged {
                Mode = CipherMode.CBC
            };

            byte[] cipherTextBytes;
            using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes))
            {
                using (var memStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                        cryptoStream.FlushFinalBlock();
                        cipherTextBytes = memStream.ToArray();
                        memStream.Close();
                        cryptoStream.Close();
                    }
                }
            }
            symmetricKey.Clear();
            return(cipherTextBytes);
        }