GetBytes() private method

private GetBytes ( char chars, int charCount, byte bytes, int byteCount ) : int
chars char
charCount int
bytes byte
byteCount int
return int
Esempio n. 1
0
        private static int GetUnicodeCode(char character)
        {
            UTF32Encoding encoding = new UTF32Encoding();
            byte[] bytes = encoding.GetBytes(character.ToString().ToCharArray());

            return BitConverter.ToInt32(bytes, 0);
        }
Esempio n. 2
0
 public static string GetUtf32(string unicodeString)
 {
     var utf8 = new UTF32Encoding();
     byte[] encodeBytes = utf8.GetBytes(unicodeString);
     string decodedString = utf8.GetString(encodeBytes);
     return decodedString;
 }
Esempio n. 3
0
		/// <summary>hashing from string. Result in format of 000-000</summary>
		public static string FromString(string input)
		{
			if (input.IsNullOrEmpty())
				return FromBuffer(null);


			var utf32 = new UTF32Encoding();
			return FromBuffer(utf32.GetBytes(input));
		}
Esempio n. 4
0
        private void button2_Click(object sender, EventArgs e)
        {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            UTF32Encoding utf8 = new UTF32Encoding();
            string hashResult = BitConverter.ToString(sha1.ComputeHash(utf8.GetBytes(textBox2.Text)));

            label4.Text = hashResult;
            label3.Text = hashResult.Length.ToString();
        }
Esempio n. 5
0
        public static string Md5Encrypte(string inputString)
        {
            var u = new System.Text.UTF32Encoding();
            byte[] bytes = u.GetBytes(inputString);
            System.Security.Cryptography.MD5 md = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] result = md.ComputeHash(bytes);
            return Convert.ToBase64String(result);
        }
Esempio n. 6
0
        private void button1_Click(object sender, EventArgs e)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            UTF32Encoding utf8 = new UTF32Encoding();
            string hashResult = BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(textBox1.Text)));

            label1.Text = hashResult;
            label2.Text = hashResult.Length.ToString();
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a password hash from the details provided
        /// </summary>
        /// <param name="created">The time when the person was created</param>
        /// <param name="password">The desired password</param>
        /// <param name="salt">The login's salt</param>
        /// <returns>A Byte[] with the password hash</returns>
        public Byte[] CreateHash(DateTime created, string password, string salt)
        {
            if (password == null) throw new ArgumentNullException("password");
            if (salt == null) throw new ArgumentNullException("salt");

            SHA1 sha = new SHA1CryptoServiceProvider();
            UTF32Encoding enc = new UTF32Encoding();
            var stringValue = string.Concat(_configurationManager.HashConstant, created, password, salt);
            return sha.ComputeHash(enc.GetBytes(stringValue));
        }
Esempio n. 8
0
        public static string Md5Encrypte(string inputString)
        {
            var u = new System.Text.UTF32Encoding();

            byte[] bytes = u.GetBytes(inputString);
            System.Security.Cryptography.MD5 md = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] result = md.ComputeHash(bytes);
            return(Convert.ToBase64String(result));
        }
Esempio n. 9
0
        public string Manipulate(string s)
        {
            string res = string.Empty;

            System.Text.UTF32Encoding encoding = new System.Text.UTF32Encoding();
            byte[] bytes = encoding.GetBytes(s);

            for (int i = 0; i < bytes.Length; i += sizeof(int))
            {
                res += $@"\x{BitConverter.ToInt32(bytes, i):X}";
            }

            return(res);
        }
Esempio n. 10
0
        /// <summary>
        /// Compute the salted hash of the given password
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public string ComputeSaltedHash(string password)
        {
            if (password == null)
                throw new ArgumentNullException("password");

            //Encoder for password (text->bytes)
            UTF32Encoding encoder = new UTF32Encoding();

            //Put password bytes and then saly bytes into byte array
            var toHash = encoder.GetBytes(password)
                .Concat(BitConverter.GetBytes(Salt))
                .ToArray();

            // Hash the salted password
            using (SHA512Managed sha1 = new SHA512Managed())
                return BitConverter.ToString(sha1.ComputeHash(toHash)).Replace("-", "");
        }
Esempio n. 11
0
    public static string Md5Sum(string strToEncrypt)
    {
        System.Text.UTF32Encoding ue = new System.Text.UTF32Encoding();
        byte[] bytes = ue.GetBytes(strToEncrypt);

        // encrypt bytes
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hashBytes = md5.ComputeHash(bytes);

        // Convert the encrypted bytes back to a string (base 16)
        string hashString = "";

        for (int i = 0; i < hashBytes.Length; i++)
        {
            hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
        }

        return(hashString.PadLeft(32, '0'));
    }
Esempio n. 12
0
 //Used to mimic the charCodeAt Javascript method
 static String CharCode(char chr)
 {
     UTF32Encoding encoding = new UTF32Encoding();
     byte[] bytes = encoding.GetBytes(chr.ToString().ToCharArray());
     return BitConverter.ToInt32(bytes, 0).ToString();
 }
 /// <summary>
 /// Provides encoding functionality in UniversalString way for a string.
 /// </summary>
 /// <param name="str"></param>
 /// <returns>The encoding result, in byte array form, big endian.</returns>
 private static byte[] UniversalStringEncode(string str)
 {
     UTF32Encoding be = new UTF32Encoding(true, false);
     return be.GetBytes(str);
 }
 private string EncodeBase64(string strValue)
 {
     try
     {
         System.Text.UTF32Encoding objUTF32 = new System.Text.UTF32Encoding();
         byte[] objbytes = objUTF32.GetBytes(strValue);
         return Convert.ToBase64String(objbytes);
     }
     catch (Exception ex)
     {
         VMuktiHelper.ExceptionHandler(ex, "EncodeBase64()", "bootStrapServiceDomain.cs");
         return null;
     }
 }
Esempio n. 15
0
 public static string Encrypte(string pString)
 {
     UTF32Encoding u = new UTF32Encoding();
     byte[] bytes = u.GetBytes(pString); //get original string
     MD5 md = new MD5CryptoServiceProvider(); // using md5 algorithm
     byte[] result = md.ComputeHash(bytes); // encrypted input bytes into encrypted bytes
     return Convert.ToBase64String(result); //return encrypted string
 }
        protected void NewPwChange_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(oldPw.Text))
            {
                oldPwStatus.ForeColor = System.Drawing.Color.Red;
                oldPwStatus.Text = "Old Password is required";
            }

            if (string.IsNullOrEmpty(newPw.Text))
            {
                newPwStatus.ForeColor = System.Drawing.Color.Red;
                newPwStatus.Text = "New Password is required";
            }

            if (string.IsNullOrEmpty(newPw2.Text))
            {
                newPw2Status.ForeColor = System.Drawing.Color.Red;
                newPw2Status.Text = "Repeat Password is required";
            }

            if (!string.IsNullOrEmpty(newPw.Text) && !string.IsNullOrEmpty(newPw2.Text))
            {
                int result = (string.Compare(newPw.Text, newPw2.Text));

                if (result == -1)
                {
                   ChangePwStatus.ForeColor = System.Drawing.Color.Red;
                   ChangePwStatus.Text = "Repeat Password not match with New Password";
                }
                else if (!string.IsNullOrEmpty(oldPw.Text))
                {
                    SqlConnection sqlConn = new SqlConnection(Shared.SqlConnString);
                    using (sqlConn)
                    {
                        try
                        {
                            SqlCommand checkoldpassword = new SqlCommand("SELECT * FROM dbo.[User] WHERE UserID = @userid AND Password = @password", sqlConn);
                            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
                            Byte[] hashedBytes;
                            UTF32Encoding encoder = new UTF32Encoding();

                            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(newPw.Text));

                            checkoldpassword.Parameters.AddWithValue("@userid", Session["UserID"].ToString());
                            checkoldpassword.Parameters.AddWithValue("@password", hashedBytes);

                            sqlConn.Open();
                            SqlDataReader reader = checkoldpassword.ExecuteReader();

                            if (!reader.HasRows)
                            {
                                ChangePwStatus.ForeColor = System.Drawing.Color.Red;
                                ChangePwStatus.Text = "Incorrect old password";
                            }
                            else
                            {
                                SqlConnection sqlConn2 = new SqlConnection(Shared.SqlConnString);
                                using (sqlConn2)
                                {
                                    try
                                    {
                                        SqlCommand changepw = new SqlCommand("UPDATE dbo.[User] SET Password = @password WHERE UserID = @userid", sqlConn2);
                                        changepw.Parameters.AddWithValue("@userid", Session["UserID"].ToString());
                                        changepw.Parameters.AddWithValue("@password", hashedBytes);

                                        sqlConn2.Open();
                                        changepw.ExecuteNonQuery();
                                    }
                                    catch
                                    {

                                    }
                                }
                            }
                        }
                        catch
                        {

                        }
                    }
                }
            }
        }
Esempio n. 17
0
		public void TestUtf32PtrToString ()
		{
			var utf32NativeEndianNoBom = new UTF32Encoding(
				bigEndian: !BitConverter.IsLittleEndian,
				byteOrderMark: false,
				throwOnInvalidCharacters: true
			);

			// assemble a buffer that contains:
			// 1. eight garbage bytes
			// 2. the native-endian UTF-32 string "Hello, World" without BOM
			// 3. four 0 bytes (as a C wide string terminator)
			// 4. the native-endian UTF-32 string "broken" without BOM
			// 5. eight 0 bytes
			// 6. four garbage bytes
			var buf = new List<byte>();
			for (int i = 0; i < 2; ++i) {
				buf.Add((byte)0x12);
				buf.Add((byte)0x34);
				buf.Add((byte)0x56);
				buf.Add((byte)0x78);
			}

			buf.AddRange(utf32NativeEndianNoBom.GetBytes("Hello, World"));

			for (int i = 0; i < 4; ++i) {
				buf.Add((byte)0x00);
			}

			buf.AddRange(utf32NativeEndianNoBom.GetBytes("broken"));

			for (int i = 0; i < 8; ++i) {
				buf.Add((byte)0x00);
			}

			buf.Add((byte)0x12);
			buf.Add((byte)0x34);
			buf.Add((byte)0x56);
			buf.Add((byte)0x78);

			// get the array version of this
			var bufArr = buf.ToArray();

			// allocate a buffer that will contain this string
			IntPtr bufPtr = UnixMarshal.AllocHeap(bufArr.Length);
			string returned;
			try
			{
				// copy it in
				Marshal.Copy(bufArr, 0, bufPtr, bufArr.Length);

				// try getting it back
				returned = UnixMarshal.PtrToString(bufPtr + 8, utf32NativeEndianNoBom);
			}
			finally
			{
				UnixMarshal.FreeHeap(bufPtr);
			}

			Assert.AreEqual("Hello, World", returned);
		}
Esempio n. 18
0
 public string EncryptPasswordMD5(string Password)
 {
     try
     {
         UTF32Encoding u = new UTF32Encoding();
         byte[] bytes = u.GetBytes(Password);
         MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
         byte[] strmd5 = MD5.ComputeHash(bytes);
         byte[] strSHA1 = u.GetBytes(Convert.ToBase64String(strmd5));
         SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
         byte[] kq = sha1.ComputeHash(strSHA1);
         return Convert.ToBase64String(kq);
     }
     catch
     {
         return "";
     }
 }
Esempio n. 19
0
        protected void btnCreateUser_Click(object sender, EventArgs e)
        {
            // Shared.SqlConnString is obtained from Assets/Shared.cs file
            // At beginning of source code, add using statement "using KeysightMOR.Assets"
            bool checkselectdiv = false;
            foreach (GridViewRow row in DivisionList.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox SelectDivision = (row.Cells[0].FindControl("SelectDivision") as CheckBox);
                    if (SelectDivision.Checked)
                    {
                        checkselectdiv = true;
                    }
                }
            }

            lblAddUserStatus.Text = null;
            if (Position.Text == "Employee" && checkselectdiv == false)
            {
                lblAddUserStatus.Text = "Please select at least 1 division";
            }

            SqlConnection sqlConn = new SqlConnection(Shared.SqlConnString);
            using (sqlConn)
            {
                try
                {
                    SqlCommand CheckEmail = new SqlCommand("SELECT * FROM dbo.[User] WHERE UserEmail = @UserEmail AND Status = 1", sqlConn);
                    CheckEmail.Parameters.AddWithValue("@UserEmail", usrEmail.Text);
                    sqlConn.Open();

                    SqlDataReader reader = CheckEmail.ExecuteReader();
                    EmailExist.Text = null;

                    if (reader.HasRows)
                    {
                        EmailExist.Text = "This email exists. Use another email";
                    }
                    else
                    {
                        string SqlAddUserCmd;
                        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
                        Byte[] hashedBytes;
                        UTF32Encoding encoder = new UTF32Encoding();

                        hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes("123456789"));

                        SqlAddUserCmd = "INSERT INTO dbo.[User](UserName, UserEmail, Password, Position, Status) VALUES (@UserName, @UserEmail, @Password, @Position, 1)";

                        SqlConnection sqlConn2 = new SqlConnection(Shared.SqlConnString);
                        SqlCommand sqlCmd = new SqlCommand(SqlAddUserCmd, sqlConn2);

                        sqlCmd.Parameters.AddWithValue("@UserName", usrName.Text);
                        sqlCmd.Parameters.AddWithValue("@UserEmail", usrEmail.Text);
                        sqlCmd.Parameters.AddWithValue("@Password", hashedBytes);
                        sqlCmd.Parameters.AddWithValue("@Position", Position.Text);

                        using (sqlConn2)
                        {
                            try
                            {
                                if (!string.IsNullOrEmpty(usrName.Text) && !string.IsNullOrEmpty(usrEmail.Text) && ((Position.Text == "Employee" && checkselectdiv == true) || Position.Text == "Admin"))
                                {
                                    sqlConn2.Open();

                                    sqlCmd.ExecuteNonQuery();

                                    if (Position.Text == "Employee" && checkselectdiv == true)
                                    {
                                        SqlConnection sqlConn3 = new SqlConnection(Shared.SqlConnString);
                                        string userid = "";
                                        using (sqlConn3)
                                        {
                                            try
                                            {
                                                //if (!string.IsNullOrEmpty(txtAddCm.Text))
                                                //{
                                                SqlCommand getUserIDCmd = new SqlCommand("SELECT UserID FROM dbo.[User] WHERE UserEmail = @UserEmail", sqlConn3);
                                                getUserIDCmd.Parameters.AddWithValue("@UserEmail", usrEmail.Text);
                                                sqlConn3.Open();

                                                SqlDataReader reader1 = getUserIDCmd.ExecuteReader();

                                                if (reader1.HasRows)
                                                {
                                                    while (reader1.Read())
                                                    {
                                                        Console.WriteLine(String.Format("{0}", reader1[0]));
                                                        userid = reader1[0].ToString();
                                                    }
                                                }
                                                else
                                                {
                                                    Console.WriteLine("No rows found.");
                                                }
                                                reader1.Close();
                                            }
                                            //}
                                            catch (Exception ex)
                                            {
                                                // TODO: Use label to print any exception occurred at this stage
                                                lblAddUserStatus.Text = ex.Message;
                                            }
                                        }

                                        foreach (GridViewRow row in DivisionList.Rows)
                                        {
                                            if (row.RowType == DataControlRowType.DataRow)
                                            {
                                                CheckBox SelectDivision = (row.Cells[0].FindControl("SelectDivision") as CheckBox);
                                                if (SelectDivision.Checked)
                                                {
                                                    string SqlAddUserDivCmd = "INSERT INTO dbo.[UserDivID](DivID, UserID, Status) VALUES (@divisionid, @userid, 1)";
                                                    SqlConnection sqlConn4 = new SqlConnection(Shared.SqlConnString);
                                                    SqlCommand sqlCmd3 = new SqlCommand(SqlAddUserDivCmd, sqlConn4);
                                                    sqlCmd3.Parameters.AddWithValue("@userid", userid);
                                                    sqlCmd3.Parameters.AddWithValue("@divisionid", DivisionList.DataKeys[row.RowIndex]["DivID"]);

                                                    using (sqlConn4)
                                                    {
                                                        try
                                                        {
                                                            sqlConn4.Open();
                                                            sqlCmd3.ExecuteNonQuery();
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            //TODO: Use label to print any exception occurred at this stage
                                                            lblAddUserStatus.Text = ex.Message;
                                                        }

                                                    }
                                                }
                                                else
                                                {
                                                    string SqlAddUserDivCmd = "INSERT INTO dbo.[UserDivID](DivID, UserID, Status) VALUES (@divisionid, @userid, 0)";
                                                    SqlConnection sqlConn4 = new SqlConnection(Shared.SqlConnString);
                                                    SqlCommand sqlCmd3 = new SqlCommand(SqlAddUserDivCmd, sqlConn4);
                                                    sqlCmd3.Parameters.AddWithValue("@userid", userid);
                                                    sqlCmd3.Parameters.AddWithValue("@divisionid", DivisionList.DataKeys[row.RowIndex]["DivID"]);

                                                    using (sqlConn4)
                                                    {
                                                        try
                                                        {
                                                            sqlConn4.Open();
                                                            sqlCmd3.ExecuteNonQuery();
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            //TODO: Use label to print any exception occurred at this stage
                                                            lblAddUserStatus.Text = ex.Message;
                                                        }

                                                    }
                                                }
                                            }
                                        }
                                    }

                                    StringBuilder sb = new StringBuilder();
                                    sb.Append("User " + usrName.Text + " successfully submitted");

                                    // TODO: Add confirmation message here
                                    lblAddUserStatus.ForeColor = System.Drawing.Color.Green;
                                    lblAddUserStatus.Text = sb.ToString();
                                    // Finally, clear the textbox
                                    usrName.Text = null;
                                    usrEmail.Text = null;
                                    Position.ClearSelection();
                                    Table1.Rows[6].Visible = false;
                                }
                            }
                            catch (Exception ex)
                            {
                                // TODO: Use label to print any exception occurred at this stage
                                lblAddUserStatus.Text = ex.Message;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblAddUserStatus.Text = ex.Message;
                }
            }
        }