DES Encryption

dear Experts i wrote this code for des encryption

and i
1 - dont know if it is write or wrong
2 - how to convert the output from byte[] to HEX

this is the code
public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            Rijndael alg = Rijndael.Create();
            alg.Key = Key;
            alg.IV = IV;
            CryptoStream cs = new CryptoStream(ms,alg.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(clearData, 0, clearData.Length);
            cs.Close();
            byte[] encryptedData = ms.ToArray();
            return encryptedData;
        }


and the data entry to call this method from textbox is :
               System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();                
                byte[] xx = encoding.GetBytes("");
                byte[] cc = RockEncryption.Encrypt(xx, encoding.GetBytes("E2A9E7A710F70669"), encoding.GetBytes("690CA02BA49666BB"));


i just want you to know that i dont have any Experience on this subject
so i nedd help please
thank you
RamzyEbeidAsked:
Who is Participating?
 
WelkinMazeConnect With a Mentor Commented:
Hi,
If you want to convert the byte[] to HEX string, the fastest way is to define this array

  public static final String[] HEX_NUMBERS = {
   "00", "01", "02", "03", "04", "05", "06", "07",
   "08", "09", "0A", "0B", "0C", "0D", "0E", "0F",
   "10", "11", "12", "13", "14", "15", "16", "17",
   "18", "19", "1A", "1B", "1C", "1D", "1E", "1F",
   "20", "21", "22", "23", "24", "25", "26", "27",
   "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
   "30", "31", "32", "33", "34", "35", "36", "37",
   "38", "39", "3A", "3B", "3C", "3D", "3E", "3F",
   "40", "41", "42", "43", "44", "45", "46", "47",
   "48", "49", "4A", "4B", "4C", "4D", "4E", "4F",
   "50", "51", "52", "53", "54", "55", "56", "57",
   "58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
   "60", "61", "62", "63", "64", "65", "66", "67",
   "68", "69", "6A", "6B", "6C", "6D", "6E", "6F",
   "70", "71", "72", "73", "74", "75", "76", "77",
   "78", "79", "7A", "7B", "7C", "7D", "7E", "7F",
   "80", "81", "82", "83", "84", "85", "86", "87",
   "88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
   "90", "91", "92", "93", "94", "95", "96", "97",
   "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",
   "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
   "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF",
   "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
   "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
   "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",
   "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF",
   "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
   "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF",
   "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7",
   "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
   "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7",
   "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF"
  };

and after that to construct a new string replacing each byte of the byte array, for example A[i] with HEX_NUMBERS[A[i]]
0
 
WelkinMazeCommented:
Your code could be something like this:

    public static final String getHex(byte[] b)
    {
      if( null == b ) {
        return "";
      }
      StringBuffer res = new StringBuffer();
      int liLength=b.length;
      byte lbByte = 0;
      res.ensureCapacity(liLength*2);

      for (int i=0;i<liLength;i++)
      {
        lbByte = b[i];
        res.append(HEX_NUMBERS[lbByte<0?256+lbByte:lbByte]);
      }
      return res.toString();
    }
0
 
WelkinMazeCommented:
You can also look here for other methods but the above has to be one of the fastest ways
http://forum.java.sun.com/thread.jspa?threadID=252591&messageID=2272668
0
 
nayernaguibConnect With a Mentor Commented:
First, your code works! The following piece of code encrypts then decrypts the contents of the cleartext array (put the code in the Load event handler of a Windows Form):

_____________________________________________

            byte[] cleardata = new ASCIIEncoding().GetBytes("hello");
            MemoryStream ms = new MemoryStream();
            MemoryStream ms2 = new MemoryStream();
            Rijndael alg = Rijndael.Create();

            alg.Key = new byte[] { 45, 43, 123, 167, 73, 24, 177, 36, 243, 123, 46, 54, 67, 88, 99, 42 };
            alg.IV = new ASCIIEncoding().GetBytes("cdfdjrjsjgkbkart");

            CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(cleardata, 0, cleardata.Length);
            cs.Close();
            byte[] encryptedData = ms.ToArray();

            CryptoStream cs2 = new CryptoStream(ms2, alg.CreateDecryptor(), CryptoStreamMode.Write);
            cs2.Write(encryptedData, 0, encryptedData.Length);
            cs2.Close();
            byte[] cleardata2 = ms2.ToArray();

            Text = new ASCIIEncoding().GetString(cleardata2);

_____________________________________________

The titlebar of the form will include the string "hello".

As for your other question, here's my quick attempt to convert a byte array to a hexadecimal string:

_____________________________________________

            byte[] data=new ASCIIEncoding().GetBytes("cdfcbmyr4gkbkart");
            string hex="";
            for (int i = 0; i < data.Length; i++)
                hex += string.Format("{0:x}", data[i]);

            Text = hex.ToUpper();

_____________________________________________

This code will put the hex. string in the titlebar of the form.

_______________

  Nayer Naguib
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.