3DES (Triple-DES) in C#
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: 3DES (Triple-DES) in C#

  1. #1
    Join Date
    Oct 2005
    Posts
    62

    Unhappy 3DES (Triple-DES) in C#

    Hi,

    I want to write code for encryption in C#. can any one provide me link for 3DES (Triple-DES) in C# where i will get code & also it's explanation.

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: 3DES (Triple-DES) in C#

    Please rate my post if it was helpful for you.
    C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Mar 2004
    Location
    3311'18.10"N 9645'20.28"W
    Posts
    1,808

    Re: 3DES (Triple-DES) in C#

    Code:
    byte[] key = Encoding.ASCII.GetBytes("passwordDR0wSS@P6660juht");
    byte[] iv = Encoding.ASCII.GetBytes("password");
    byte[] data = Encoding.ASCII.GetBytes("Test");
    byte[] enc = new byte[0];
    byte[] dec = new byte[0];
    TripleDES tdes = TripleDES.Create();
    tdes.IV = iv;
    tdes.Key = key;
    tdes.Mode = CipherMode.CBC;
    tdes.Padding = PaddingMode.Zeros;
    // encryption
    ICryptoTransform ict = tdes.CreateEncryptor();
    enc = ict.TransformFinalBlock(data, 0, data.Length);
    // decryption
    ict = tdes.CreateDecryptor();
    dec = ict.TransformFinalBlock(enc, 0, enc.Length);
    general info: http://en.wikipedia.org/wiki/Triple_DES
    api for tripledes: http://msdn.microsoft.com/en-us/libr...tripledes.aspx
    api for icrypto: http://msdn.microsoft.com/en-us/libr...transform.aspx

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    Re: 3DES (Triple-DES) in C#

    Please use [code] and [/code] tags when you post.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Mar 2012
    Posts
    1

    Re: 3DES (Triple-DES) in C#

    Simulation 64bit des - 3des in java:

    Code:
    public class Filippakoc_Des {
        static int[][] sb1={
            {14 , 4 , 13 , 1 , 2 , 15 , 11 , 8 , 3 , 10 , 6 , 12 , 5 , 9 , 0 , 7 },
            {0  , 15,  7 , 4 , 14, 2  ,  13,  1, 10,   6, 12,  11,  9,  5,  3,  8},
            {4  ,  1,  14,  8, 13,   6,   2, 11, 15,  12,  9,   7,  3, 10,  5,  0},
            {15 , 12,   8,  2,  4,   9,   1,  7,  5,  11,  3,  14, 10,  0,  6,  13}
        };
         static int[][] sb2={
            {15 , 1 , 8 , 14, 6 ,  11,   3,  4, 9 , 7 ,  2, 13 , 12, 0, 5 ,  10},
            {3  , 13,  4,  7, 15,   2,   8, 14, 12,  0,  1,  10,  6, 9, 11,   5},
            {0  , 14,  7, 11, 10,   4,  13,  1, 5 ,  8, 12,   6,  9, 3,  2,  15},
            {13 , 8 , 10,  1,  3,  15,   4,  2, 11,  6,  7,  12, 0 , 5,  14,  9}
        };
         static int[][] sb3={
            {10 , 0 ,  9,14,  6,    3,  15,  5, 1, 13,  12, 7 , 11,  4,   2,   8},
            {13 ,  7,  0, 9,  3,    4,   6, 10, 2,  8,   5, 14, 12, 11,  15,   1},
            {13 ,  6,  4, 9,  8,   15,   3,  0,11,  1,   2, 12,  5, 10,  14,   7},
            {1  , 10 ,13, 0,  6,    9,   8,  7, 4, 15,  14,  3, 11,  5,   2,  12}
        };
           static int[][] sb4={
            { 7 , 13, 14, 3,   0,  6,   9, 10, 1, 2,  8,  5, 11, 12,  4,  15},
            {13 ,  8, 11, 5,   6, 15,   0,  3, 4, 7,  2, 12,  1, 10, 14,   9},
            {10 ,  6,  9, 0,  12, 11,   7, 13,15, 1,  3, 14,  5,  2,  8,   4},
            {3  , 15,  0, 6,  10,  1,   13, 8, 9, 4,  5, 11, 12,  7,  2,  14}
        };
             static int[][] sb5={
            { 2, 12,  4,  1,  7,  10, 11, 6,  8,  5,   3, 15, 13,  0, 14,  9},
            {14, 11,  2, 12,  4,   7, 13, 1,  5,  0,  15, 10,  3,  9,  8,  6},
            {4 ,  2,  1, 11, 10,  13,  7, 8, 15,  9,  12,  5,  6,  3,  0, 14},
            {11,  8, 12, 7 ,  1,  14,  2, 13, 6, 15,   0,  9, 10,  4,  5,  3}
        };
                   static int[][] sb6={
            { 12, 1, 10, 15,  9,  2,  6, 8,  0,  13,   3,  4, 14,  7,  5, 11},
            {10, 15,  4,  2,  7, 12,  9, 5,  6,   1,  13, 14,  0,  11, 3,  8},
            {9 , 14, 15,  5,  2,  8, 12, 3,  7,   0,   4, 10,  1,  13,11,  6},
            {4,   3, 12, 12,  9,  5, 15, 10,11,  14,   1,  7,  6,   0, 8, 13}
        };
                   static int[][] sb7={
            { 4,11,  2, 14,  15, 0,  8, 13,  3,  12,  9,  7,  5, 10, 6, 1},
            {13, 0, 11,  7,  4,  9,  1, 10, 14,   3,  5, 12,  2, 15, 8,  6},
            {1 , 4, 11, 13,  12, 3, 7,  14, 10,  15,  6,  8,  0,  5, 9,  2},
            {6, 11, 13,  8,  1,  4, 10,  7,  9,   5,  0, 15, 14,  2, 3, 12}
        };
                   static int[][] sb8={
            {13,  2,  8,  4,   6,  15, 11,  1, 10,  9,  3,14,  5, 0, 12,  7},
            { 1, 15, 13,  8,  10,   3,  7,  4, 12,  5,  6,11,  0, 14, 9,  2},
            {7 , 11,  4,  1,   9,  12, 14,  2,  0,  6, 10,13, 15,  3, 5,  8},
            { 2,  1, 14,  7,   4,  10,  8, 13, 15, 12,  9, 0,  3,  5, 6, 11}
        };
                   
             public String Filippakoc_Des(String ptxt,String key,boolean encdes){
                 //String a=String.format("%64s", Integer.toBinaryString(255)).replace(' ', '0');
                 //String b="1011011111001001100001110000101110110000010001110101111101001010";
                 String ipa=IPpermutation(ptxt);
                 String ipb=IPpermutation(ptxt);
                 String[] keys=Keygenerate(key);
                 String[] rounds=new String[16];
                 String cripttext="";
                 if(encdes==true){
                      for(int i=0;i<rounds.length;i++){
                     if(i<1){
                     rounds[i]=round(ipa,keys[i]);
                     }else{
                         rounds[i]=round(rounds[i-1],keys[i]);
                     }
                         
                     
                    }
                 
                 cripttext=IPpermutation_(rounds[15]);
                 //System.out.println(ptxt);
                 //System.out.println(cripttext);
                 }else{
                      for(int i=rounds.length-1;i>=0;i--){
                       if(i>14){
                     rounds[i]=dround(ipb,keys[i]);
                     }else{
                         rounds[i]=dround(rounds[i+1],keys[i]);
                     }
                    
                 }
                 cripttext=IPpermutation_(rounds[0]);
                 //System.out.println(ptxt);
                 //System.out.println(cripttext);
                 }
                
                 
                return cripttext;
                  
                 //String a=String.format("%32s", Integer.toBinaryString(255)).replace(' ', '0');
                 //String b=String.format("%48s", Integer.toBinaryString(21)).replace(' ', '0');
                 //System.out.println(functionf(a, b));
                 //System.out.println(xor(a,b));
             }
             public String Filippakoc_3des(String ptxt,String key1,String key2,String key3,boolean encdes){
                 String En="";
                 if(encdes){
                   En=Filippakoc_Des(ptxt, key1, true);
                 En=En=Filippakoc_Des(En, key2, false);
                  En=En=Filippakoc_Des(En, key3, true);
                 }else{
                     En=Filippakoc_Des(ptxt, key1, false);
                 En=En=Filippakoc_Des(En, key2, true);
                  En=En=Filippakoc_Des(En, key3, false);
                 }
                 
                  return En;
                 
             }
             public String round(String plain64,String keyi){
                 
                 String li=plain64.substring(0,32);
                 String ri=plain64.substring(32);
                 
                 String ro=xor(li,functionf(ri,keyi));
                 String lo=ri;
                 String resultround=lo+ro;
                 return resultround;
             }
             public String dround(String plain64,String keyi){
                      String li=plain64.substring(0,32);
                 String ri=plain64.substring(32);
                 
                 String lo=xor(ri,functionf(li,keyi));
                 String ro=li;
                 String resultround=lo+ro;
                 return resultround;
             }
             public String xor(String a,String b){
                 String res="";
                 for(int i=0;i<a.length();i++){
                     if(a.charAt(i)=='0' && b.charAt(i)=='0'){
                         res+='0';
                     }else  if(a.charAt(i)=='0' && b.charAt(i)=='1'){
                         res+='1';
                     }else  if(a.charAt(i)=='1' && b.charAt(i)=='0'){
                         res+='1';
                     }else  if(a.charAt(i)=='1' && b.charAt(i)=='1'){
                         res+='0';
                     }
                 }
                 return res;
             }
             public String sboxesrtn(int ff){
                 String sb=Integer.toBinaryString(ff);
                 if(ff<2){
                     sb="000"+sb;
                 }else if(ff<4){
                     sb="00"+sb;
                 }else if(ff<8){
                     sb="0"+sb;
                 }
                 return sb;
             }
             public String functionf(String r32,String k48){
                 String r48=Epermutation(r32);
                 String xorout=xor(r48, k48);
                 String[] splitto6=new String[8];
                 splitto6[0]=xorout.substring(0,6);
                 splitto6[1]=xorout.substring(6,12);
                 splitto6[2]=xorout.substring(12,18);
                 splitto6[3]=xorout.substring(18,24);
                 splitto6[4]=xorout.substring(24,30);
                 splitto6[5]=xorout.substring(30,36);
                 splitto6[6]=xorout.substring(36,42);
                 splitto6[7]=xorout.substring(42);
                 String rnew32="";
                 String row;
                 String column="";
                 for(int i=0;i<splitto6.length;i++){
                    row=""+splitto6[i].charAt(0)+splitto6[i].charAt(5);
                    column=splitto6[i].substring(1,5);
                    //System.out.println(splitto6[i]+"  "+row+"  "+column);
                    int irow=Integer.parseInt(row, 2);
                    int icolumn=Integer.parseInt(column, 2);
                    if(i==0){
                        //rnew32+=Integer.toBinaryString(sb1[irow][icolumn]);
                        rnew32+=String.format("%4s", Integer.toBinaryString(sb1[irow][icolumn])).replace(' ', '0');
                    }else if(i==1){
                        rnew32+=String.format("%4s", Integer.toBinaryString(sb2[irow][icolumn])).replace(' ', '0');
                    }else if(i==2){
                        rnew32+=String.format("%4s", Integer.toBinaryString(sb3[irow][icolumn])).replace(' ', '0');
                    }else if(i==3){
                        rnew32+=String.format("%4s", Integer.toBinaryString(sb4[irow][icolumn])).replace(' ', '0');
                    }else if(i==4){
                        rnew32+=String.format("%4s", Integer.toBinaryString(sb5[irow][icolumn])).replace(' ', '0');
                    }else if(i==5){
                        rnew32+=String.format("%4s", Integer.toBinaryString(sb6[irow][icolumn])).replace(' ', '0');
                    }else if(i==6){
                        rnew32+=String.format("%4s", Integer.toBinaryString(sb7[irow][icolumn])).replace(' ', '0');
                    }else if(i==7){
                        rnew32+=String.format("%4s", Integer.toBinaryString(sb8[irow][icolumn])).replace(' ', '0');
                    }
                 }
                 
                 
                //System.out.println(rnew32.length());
                //System.out.println(rnew32);
                // System.out.print(splitto6[0]+"  "+splitto6[1]+"  "+splitto6[2]+"  "+splitto6[3]+"  "+splitto6[4]+"  "+splitto6[5]+"  "+splitto6[6]+"  "+splitto6[7]+"\n");
                 rnew32=Ppermutation(rnew32);
                 return rnew32;
                 
             }
             public String Epermutation(String r32){
                 r32=" "+r32;
                 String r48=""+r32.charAt(31)+r32.charAt(1)+r32.charAt(2)+r32.charAt(3)+r32.charAt(4)+r32.charAt(5)+r32.charAt(4)+r32.charAt(5)+r32.charAt(6)+r32.charAt(7)+r32.charAt(8)+r32.charAt(9)+r32.charAt(8)+r32.charAt(9)+r32.charAt(10)+r32.charAt(11)+r32.charAt(12)+r32.charAt(13)+r32.charAt(12)+r32.charAt(13)+r32.charAt(14)+r32.charAt(15)+r32.charAt(16)+r32.charAt(17)+r32.charAt(16)+r32.charAt(17)+r32.charAt(18)+r32.charAt(19)+r32.charAt(20)+r32.charAt(21)+r32.charAt(20)+r32.charAt(21)+r32.charAt(22)+r32.charAt(23)+r32.charAt(24)+r32.charAt(25)+r32.charAt(24)+r32.charAt(25)+r32.charAt(26)+r32.charAt(27)+r32.charAt(28)+r32.charAt(29)+r32.charAt(28)+r32.charAt(29)+r32.charAt(30)+r32.charAt(31)+r32.charAt(32)+r32.charAt(1);
                 return r48;
             }
             public String Ppermutation(String r32){
                 r32=" "+r32;
                 String r32p=""+r32.charAt(17)+r32.charAt(7)+r32.charAt(20)+r32.charAt(21)+r32.charAt(29)+r32.charAt(12)+r32.charAt(28)+r32.charAt(17)+r32.charAt(1)+r32.charAt(15)+r32.charAt(23)+r32.charAt(26)+r32.charAt(5)+r32.charAt(18)+r32.charAt(31)+r32.charAt(10)+r32.charAt(2)+r32.charAt(8)+r32.charAt(24)+r32.charAt(14)+r32.charAt(32)+r32.charAt(27)+r32.charAt(3)+r32.charAt(9)+r32.charAt(19)+r32.charAt(13)+r32.charAt(30)+r32.charAt(6)+r32.charAt(22)+r32.charAt(11)+r32.charAt(4)+r32.charAt(25);
                 return r32p;
             }
              public String IPpermutation(String r64){
                 r64=" "+r64;
                 String r64p=""+r64.charAt(58)+r64.charAt(50)+r64.charAt(42)+ r64.charAt(34)+r64.charAt(26)+r64.charAt(18)+r64.charAt(10)+r64.charAt(2)
                         +r64.charAt(60)+r64.charAt(52)+r64.charAt(44)+r64.charAt(36)+r64.charAt(28)+r64.charAt(20)+r64.charAt(12)+r64.charAt(4)+
                         r64.charAt(62)+r64.charAt(54)+r64.charAt(46)+r64.charAt(38)+r64.charAt(30)+r64.charAt(22)+r64.charAt(14)+r64.charAt(6)+
                         r64.charAt(64)+r64.charAt(56)+r64.charAt(48)+r64.charAt(40)+r64.charAt(32)+r64.charAt(24)+r64.charAt(16)+r64.charAt(8)+
                         r64.charAt(57)+r64.charAt(49)+ r64.charAt(41)+r64.charAt(33)+r64.charAt(25)+r64.charAt(17)+r64.charAt(9)+r64.charAt(1)+
                         r64.charAt(59)+r64.charAt(51)+r64.charAt(43)+r64.charAt(35)+r64.charAt(27)+r64.charAt(19)+r64.charAt(11)+r64.charAt(3)+
                         r64.charAt(61)+r64.charAt(53)+r64.charAt(45)+r64.charAt(37)+r64.charAt(29)+r64.charAt(21)+r64.charAt(13)+r64.charAt(5)+
                         r64.charAt(63)+r64.charAt(55)+r64.charAt(47)+r64.charAt(39)+r64.charAt(31)+r64.charAt(23)+r64.charAt(15)+r64.charAt(7);
                 return r64p;
             }
               public String IPpermutation_(String r64){
                 r64=" "+r64;
                 String r64p=""+
                         r64.charAt(40)+r64.charAt(8)+r64.charAt(48)+ r64.charAt(16)+r64.charAt(56)+r64.charAt(24)+r64.charAt(64)+r64.charAt(32)+
                         r64.charAt(39)+r64.charAt(7)+r64.charAt(47)+r64.charAt(15)+r64.charAt(55)+r64.charAt(23)+r64.charAt(63)+r64.charAt(31)+
                         r64.charAt(38)+r64.charAt(6)+r64.charAt(46)+r64.charAt(14)+r64.charAt(54)+r64.charAt(22)+r64.charAt(62)+r64.charAt(30)+
                         r64.charAt(37)+r64.charAt(5)+r64.charAt(45)+r64.charAt(13)+r64.charAt(53)+r64.charAt(21)+r64.charAt(61)+r64.charAt(29)+
                         r64.charAt(36)+r64.charAt(4)+ r64.charAt(44)+r64.charAt(12)+r64.charAt(52)+r64.charAt(20)+r64.charAt(60)+r64.charAt(28)+
                         r64.charAt(35)+r64.charAt(3)+r64.charAt(43)+r64.charAt(11)+r64.charAt(51)+r64.charAt(19)+r64.charAt(59)+r64.charAt(27)+
                         r64.charAt(34)+r64.charAt(2)+r64.charAt(42)+r64.charAt(10)+r64.charAt(50)+r64.charAt(18)+r64.charAt(58)+r64.charAt(26)+
                         r64.charAt(33)+r64.charAt(1)+r64.charAt(41)+r64.charAt(9)+r64.charAt(49)+r64.charAt(17)+r64.charAt(57)+r64.charAt(25);
                 return r64p;
             }
             public String[] Keygenerate(String key){
            String[] keys=new String[16];
            String key56=pc_1(key);
            String lkey28=key56.substring(0,28);
            String rkey28=key56.substring(28);
              for(int i=0;i<keys.length;i++){
                    if(i==0 || i==1 || i==8 || i==15){
                        lkey28=Lbitrotate(lkey28, 1);
                        rkey28=Lbitrotate(rkey28, 1);
                        keys[i]=pc_2(lkey28+rkey28);
                    }else{
                        lkey28=Lbitrotate(lkey28, 2);
                        rkey28=Lbitrotate(rkey28, 2);
                        keys[i]=pc_2(lkey28+rkey28);
                    }
                }
            
            return keys;
            
        }
        public String pc_1(String key){
            String key56="";
            key56=""+key.charAt(56)+key.charAt(48)+key.charAt(40)+key.charAt(32)+key.charAt(24)+key.charAt(16)+key.charAt(8)+key.charAt(0)+key.charAt(57)+key.charAt(49)+key.charAt(41)+key.charAt(33)+key.charAt(25)+key.charAt(17)+key.charAt(9)+key.charAt(1)+key.charAt(58)+key.charAt(50)+key.charAt(42)+key.charAt(34)+key.charAt(26)+key.charAt(18)+key.charAt(10)+key.charAt(2)+key.charAt(59)+key.charAt(51)+key.charAt(43)+key.charAt(35)+key.charAt(62)+key.charAt(54)+key.charAt(46)+key.charAt(38)+key.charAt(30)+key.charAt(23)+key.charAt(14)+key.charAt(6)+key.charAt(61)+key.charAt(53)+key.charAt(45)+key.charAt(37)+key.charAt(29)+key.charAt(21)+key.charAt(13)+key.charAt(5)+key.charAt(60)+key.charAt(52)+key.charAt(44)+key.charAt(36)+key.charAt(28)+key.charAt(20)+key.charAt(12)+key.charAt(4)+key.charAt(27)+key.charAt(19)+key.charAt(11)+key.charAt(3);
            return key56;
        }
           public String pc_2(String key){
            String key48="";
            key48=""+key.charAt(13)+key.charAt(16)+key.charAt(10)+key.charAt(23)+key.charAt(0)+key.charAt(4)+key.charAt(2)+key.charAt(27)+key.charAt(14)+key.charAt(5)+key.charAt(20)+key.charAt(9)+key.charAt(22)+key.charAt(18)+key.charAt(11)+key.charAt(3)+key.charAt(25)+key.charAt(7)+key.charAt(15)+key.charAt(6)+key.charAt(26)+key.charAt(19)+key.charAt(12)+key.charAt(1)+key.charAt(40)+key.charAt(51)+key.charAt(30)+key.charAt(36)+key.charAt(46)+key.charAt(54)+key.charAt(29)+key.charAt(39)+key.charAt(50)+key.charAt(44)+key.charAt(32)+key.charAt(47)+key.charAt(43)+key.charAt(48)+key.charAt(38)+key.charAt(55)+key.charAt(33)+key.charAt(52)+key.charAt(45)+key.charAt(41)+key.charAt(49)+key.charAt(35)+key.charAt(28)+key.charAt(31);
            return key48;
        }
        
        
          public String Lbitrotate(String bin,int times){
           String binary=bin;
           String binary2="";
           String binarynew="";
           for(int i=0;i<times;i++){
               if(i<1){
               binary2=binary.substring(1)+binary.charAt(0);
               }else{
                   binary2=binarynew.substring(1)+binarynew.charAt(0);
               }
               binarynew=binary2;
           
           }
           return binary2;
           
          
       }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center