Esempio n. 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);
        }
Esempio n. 2
0
        public string InternalsToString()
        {
            string str1 = string.Format("SRP {0} Internals:\n", IsServer ? "server" : "client") +
                          string.Format("G      = {0}\n", Generator.ToHexString()) +
                          string.Format("K      = {0}\n", Multiplier.ToHexString()) +
                          string.Format("N      = {0}\n", Modulus.ToHexString()) +
                          string.Format("I      = '{0}'\n", Credentials) +
                          string.Format("Hash(I)= {0}\n",
                                        Hash((HashUtilities.HashDataBroker)Credentials).ToHexString()) +
                          string.Format("X      = {0}\n", CredentialsHash.ToHexString()) +
                          string.Format("V      = {0}\n", Verifier.ToHexString());

            if (m_salt != null)
            {
                str1 += string.Format("Salt   = {0}\n", Salt.ToHexString());
            }
            if (null != m_publicEphemeralValueA && null != m_publicEphemeralValueB)
            {
                str1 = str1 + string.Format("u      = {0}\n", ScramblingParameter.ToHexString()) +
                       string.Format("h(A)   = {0}\n",
                                     Hash((HashUtilities.HashDataBroker)PublicEphemeralValueA)
                                     .ToHexString()) + string.Format("h(B)   = {0}\n",
                                                                     Hash((HashUtilities.HashDataBroker)PublicEphemeralValueB.GetBytes())
                                                                     .ToHexString());
            }
            if (!IsServer || PublicEphemeralValueA != null)
            {
                str1 += string.Format("A      = {0}\n", PublicEphemeralValueA.ToHexString());
            }
            if (IsServer || PublicEphemeralValueB != null)
            {
                string     str2        = str1 + string.Format("B      = {0}\n", PublicEphemeralValueB.ToHexString());
                BigInteger bigInteger1 = Multiplier * Generator.ModPow(CredentialsHash, Modulus);
                string     str3        = str2 + string.Format("kg^x   = {0}\n", bigInteger1.ToHexString());
                BigInteger bigInteger2 = PublicEphemeralValueB - bigInteger1 % Modulus;
                if (bigInteger2 < 0)
                {
                    bigInteger2 += Modulus;
                }
                str1 = str3 + string.Format("B-kg^x = {0}\n", bigInteger2.ToHexString());
            }

            string str4;

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

            return(str4);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
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);
        }