[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DES Encryption

Posted on 2006-05-02
6
Medium Priority
?
519 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:RamzyEbeid
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
WelkinMaze earned 1000 total points
ID: 16586748
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
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16586779
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
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16586817
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
 
LVL 14

Assisted Solution

by:nayernaguib
nayernaguib earned 1000 total points
ID: 16587459
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Six Sigma Control Plans
Starting up a Project

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question