Results 1 to 6 of 6

Thread: DES encryption and decryption using C or Java

  1. #1
    Join Date
    Apr 2009
    Location
    Kolkata
    Posts
    65

    question DES encryption and decryption using C or Java

    can anyone give me two programs for encryption and decryption using c or java!!!!!!!!!
    R.I.P. Ronnie James DIO

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: des encryption and decryption usind c or java

    This is for encryption and decryption of files using C programming-
    Code:
    encryption and decryption of files
    
    In  main function we declare required variable.
    
    printf(" enter choice
    
    	 1)encode
    	 2)view ecoded file
    	 3)
    decode
    	            4)view decoced file
    	 5)exit			 ------");
    // this is to print the main menu
    scanf("%d",&ch);                                             // entering
    of choice
    while(ch!=5)                                                    //  until
    choice is exit
    {
      if(ch==1)                                                        //if
    choice is to encrypt
       {
         scanf("%s",name);                                      // accept file
    name
         scanf("%c",&ff);                                         //accept the
    choice of change
    
    // the file position or not
         if(ff=='c'||ff=='C')
       {strcpy(encri,ruf);scanf("%s",encri);} // so as to change enter file path
    
         cscanf("%s",pass);                                     // enter
    password
         l=strlen(pass);                                            // l
    =string length of password
    
        st=fopen(name,"r");                                   //  open the
    file to be encrypted
         ed=fopen(encri,"w");                                // open the file
    to where to be
                                                                           //
    encrypted
         while(!feof(st))                                        // until end
    of file to be encrypted
         {  x=fgetc(st);                                         // read each
    character into X
            fputc(x+pass[i++%l],ed);                    // write the sum of X
    and password
        }                                                               //
    char
         fclose(st);                                        // close first
    file
         fclose(ed);                                      // close second file
    }
    if(ch==3)                                             // if choice is
    decryption
       {
          scanf(“%s”,encri);                        //enter the file name
    to be decrypted
          ed=fopen(encri,"r");                     //open  the above mentioned
    file
          scanf("%c",&ff);                          //accept the choice of
    change
                                                               // the file
    position or not
    
          if(ff=='c'||ff=='C')
        { strcpy(decri,ruf);
          scanf("%s",decri);                // so as to change enter file path
         }
          cscanf("%s",pass);                      // enter password
          de=fopen(decri,"w");                  //open a file to store decoded
    data
          while(!feof(ed))                          // until end of file to be
    encrypted
          {
             x=fgetc(ed);                             // read each character
    into X
       fputc(x-pass[i++%l],de);        // write the sum of X and password
        }                                                   // char
    
    
          fclose(ed);                                 // close first file
          fclose(de);                                // close second file
          }
    
     if(ch==2)                                 //if choice is to view the
    contents encoded file
      {
        ed=fopen(encri,"r");             //open that file and read each
    character
        while(!feof(ed))
        printf("%c",fgetc(ed));          //print them until end of file
        fclose(ed);                             // close this file
      }
     if(ch==4)                                 //if choice is to view the
    contents decoded file
     {
      ed=fopen(decri,"r");               //open that file and read each
    character
      while(!feof(ed))
      printf("%c",fgetc(ed)) ;          //print them until end of file
      fclose(ed) ;                              // close this file
      }
    
       scanf("%d",&ch);                  // enter the choice from menu
      }                                              // end while
    }                                                 // end main

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: des encryption and decryption usind c or java

    Heres the 3des encryption and decryption using Java:
    Code:
    import java.security.MessageDigest;import java.util.Arrays;
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;
    public class TripleDESTest {
           public static void main(String[] args) throws Exception {
                  String text = "kyle boon";
                  byte[] codedtext = new TripleDESTest().encrypt(text);
                  String decodedtext = new TripleDESTest().decrypt(codedtext); 
                  System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array        
                  System.out.println(decodedtext); // This correctly shows "kyle boon"
    
           }
           public byte[] encrypt(String message) throws Exception {
                  final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
                                     .getBytes("utf-8"));        
                  final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
                  for (int j = 0, k = 16; j < 8;) {
                             keyBytes[k++] = keyBytes[j++];
                  }
                  final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
                  final IvParameterSpec iv = new IvParameterSpec(new byte[8]);        
                  final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");        
                  cipher.init(Cipher.ENCRYPT_MODE, key, iv);        
                  final byte[] plainTextBytes = message.getBytes("utf-8");        
                  final byte[] cipherText = cipher.doFinal(plainTextBytes);        
                  // final String encodedCipherText = new sun.misc.BASE64Encoder()        
                  // .encode(cipherText);        
                  return cipherText;
           }
           public String decrypt(byte[] message) throws Exception {
                  final MessageDigest md = MessageDigest.getInstance("md5");
                  final byte[] digestOfPassword = md.digest("HG58YZ3CR9" 
                                  .getBytes("utf-8"));        
                  final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
                  for (int j = 0, k = 16; j < 8;) {  
                             keyBytes[k++] = keyBytes[j++];
                  }
                  final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
                  final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
                  final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); 
                  decipher.init(Cipher.DECRYPT_MODE, key, iv);
                  // final byte[] encData = new
                  // sun.misc.BASE64Decoder().decodeBuffer(message);
                  final byte[] plainText = decipher.doFinal(message);
                  return new String(plainText, "UTF-8");
            }
    }

  4. #4
    Join Date
    Apr 2009
    Location
    Kolkata
    Posts
    65

    Re: DES encryption and decryption using C or Java

    javax.crypto is not there.........
    R.I.P. Ronnie James DIO

  5. #5
    Join Date
    Nov 2010
    Location
    Famagusta
    Posts
    2

    Re: DES encryption and decryption using C or Java

    the java program has errors how can i fix it....

    errors are-----------------------------------------

    C:\Sun\AppServer\jdk\bin>javac TripleDESTest.java
    TripleDESTest.java:15: cannot resolve symbol
    symbol : variable md
    location: class TripleDESTest
    final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("u
    tf-8"));
    ^
    TripleDESTest.java:16: cannot resolve symbol
    symbol : method copyOf (byte[],int)
    location: class java.util.Arrays
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    ^
    TripleDESTest.java cannot resolve symbol
    symbol : method copyOf (byte[],int)
    location: class java.util.Arrays
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    ^
    3 errors

    C:\Sun\AppServer\jdk\bin>

  6. #6
    Join Date
    Nov 2010
    Location
    Famagusta
    Posts
    2

    Re: DES encryption and decryption using C or Java

    Could any1 pls help? Am new here

Similar Threads

  1. Replies: 3
    Last Post: 03-01-2011, 09:11 AM
  2. XML encryption and decryption
    By Anirvinya in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 03:31 AM
  3. Do you know difference between encryption and decryption?
    By rooki in forum Networking & Security
    Replies: 4
    Last Post: 30-11-2009, 12:24 PM
  4. Encryption in JavaScript, Decryption in PHP
    By Rail racer in forum Software Development
    Replies: 6
    Last Post: 11-10-2008, 03:42 PM
  5. Replies: 2
    Last Post: 22-02-2006, 03:53 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,719,574.09413 seconds with 17 queries