locked
Where to get AES, SHA2 library and example for C#.NET RRS feed

  • Question

  • Hi,

    Where to get standard AES library and SHA2 library and example for C#.NET.

    Tks in adv
    Wednesday, December 30, 2009 3:40 PM

All replies

  • Wednesday, December 30, 2009 4:29 PM
  • Hi,

    Tks for the info.

    Im actually using .net compact framework. I tried SHA2 as given but got an error "The type or namespace SHA256Managed could not be found(are you missing a directive or an assembly file)"

    I took the code from a blog site:

    public static string sha256encrypt(string phrase)
            {
                UTF8Encoding encoder = new UTF8Encoding();
                SHA256Managed sha256hasher = new SHA256Managed();
                byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
                return byteArrayToString(hashedDataBytes);
            }
    Wednesday, December 30, 2009 4:51 PM
  • Hi,

    Here is a sample of code (and how you shall implement it).

            
            ///
    <summary>

            /// Generates a hash using the SHA256 algorithm.

            /// Returns a string with the hash result.

            /// </summary>

            /// <param name="input"></param>

            /// <returns></returns>

            public string GetSHA256Hash(string input)

            { 

                SHA256 sha = new SHA256CryptoServiceProvider();

                byte[] inputbytes = UTF8Encoding.UTF8.GetBytes(input);

                byte[] hash = sha.ComputeHash(inputbytes);

     

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < hash.Length; i++)

                {

                    sb.Append(hash[i].ToString("X2")); 

                }

     

                return sb.ToString(); 

            }

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik


    Coder24.com
    Wednesday, December 30, 2009 6:17 PM
  • For the namespace type: System.Security.Cryptography and then another dot (.) and you'll get a list of encryption/decryption functions including
    algorithms to make hashes etc.
    Coder24.com
    Wednesday, December 30, 2009 6:19 PM
  • hmm strange but im still getting the error

    mayb i show you what i did:
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.WindowsMobile.Status;
    using System.IO;
    using System.Diagnostics;
    using OpenNETCF.IO; //required for FileSystemWatcher; require add reference OpenNetCF; OpenNetCF is a separate installation of OpenNetCF smart device community version
    using System.Security.Cryptography;
    
    namespace MediaCrypt
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                FileSystemWatcher myWatcher = new FileSystemWatcher();
                myWatcher.Path = "\\My Documents\\MediaCrypt";
    
                //Set the filter to only catch TXT files.
                //myWatcher.Filter = "*.txt";
    
                //Subscribe to the Created event.
                myWatcher.Renamed += new RenamedEventHandler(mywatcher_Renamed);
    
                //Enable the FileSystemWatcher events.
                myWatcher.EnableRaisingEvents = true;
    
                string cleartext = "helloworld";
                lblBefore.Text = cleartext;         
    
            }        
    
            private void btnSysState_Click(object sender, EventArgs e)
            {
                MessageBox.Show(SystemState.ConnectionsDesktopDescriptions.ToString());
            }
    
            void mywatcher_Renamed(object sender, FileSystemEventArgs e)
            {
               MessageBox.Show("A new *.txt file has been created!");
            }
    
            public string GetSHA256Hash(string cleartext)
            {
                SHA256 sha = new SHA256CryptoServiceProvider();
                byte[] inputbytes = UTF8Encoding.UTF8.GetBytes(cleartext);
                byte[] hash = sha.ComputeHash(inputbytes);
    
                StringBuilder sb = new StringBuilder();
                for(int i = 0; i < hash.Length; i++){
                    sb.Append(hash[i].ToString("X2"));
                }
    
                return sb.ToString();
            }
        }
    }
    For the namespace System.Security.Cryptography , i only get X509certificates after the dot.
    The references i've added are:
    - Microsoft.WindowsMobile
    - Microsoft.WindowsMobile.Status
    - mscorlib
    - OpenNETCF
    - System
    - System.Core
    - System.Data
    - System.Data.DataSetExtensions
    - System.Drawing
    - System.Windows.Forms
    - System.xml
    - System.Xml.Linq

    Thank you very much.

    Thursday, December 31, 2009 9:08 AM
  • Hi,

    Do you want to make a syncronizer for mobiles.
    Also, where are you calling the GetSHA256Hash() method?

    Please explain some, also I think it is far more smarter if you
    use a notifyicon component with BalloonTip instead.

    Also I must add, please add the FileSystemWatcher direct to the Form
    from the ToolBox (By drag-and-drop).

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Thursday, December 31, 2009 9:36 AM
  • i've yet to call GetSHA256Hash() but it will be done in public form1().

    as for the filesystemwatcher, i cant find it from the toolbox.

    sorry im kinda new.
    Thursday, December 31, 2009 7:26 PM
  • Hi,

    Please explain what you want to accomplish?

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Saturday, January 2, 2010 1:51 PM
  • Hi Coder23.com,

    Im doing a mobile application when transfer file from PC to mobile via active sync, it will encrypt with SHA2. after which, when we wanna open this file, for example a .txt with a text editor, it will decrypt then open the file.

    Please advise how should i accomplish this as well as what libraries are needed for the cryptography (it seems that only MD5 and SHA1 are available for compact framework).
    Saturday, January 9, 2010 5:13 PM
  • Hi Coder23.com,

    NOT 23??
    Coder24.com
    Saturday, January 9, 2010 5:14 PM