Results 1 to 10 of 10
  1. #1
    DemoX is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Post Encription problem

    hey, i developed encription code

    Java Code:
    try {
        File file = new File( fileName );
        FileInputStream fileInputStream =
        new FileInputStream( file );
    
        CipherInputStream in =
        new CipherInputStream( fileInputStream, cipher );
    
    
        byte contents = ( byte ) in.read();
    
        while ( contents != -1 ) {
        fileBytes.add( new Byte( contents ) );
        contents = ( byte ) in.read();
         }
        in.close();
    
         }
    just taking byte by byte and encript

    Java Code:
    private static final byte[] salt = {
       ( byte )0xf5, ( byte )0x33, ( byte )0x01, ( byte )0x2a,
       ( byte )0xb2, ( byte )0xcc, ( byte )0xe4, ( byte )0x7f
       };
    dat function for the algorithm of encription it goes well with the text but any thing other no it seems to be brocken

    any one can help?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    There is no encryption in the sample. We have to getInstance() and do several things. The salt is not enciperment.

    Don't send any political contributions with your code, it is not hiding anything.

    String has several methods that return byte array, but some of them do weird things due to character conversion conventions. Use byte[] all the way through.

    Are you actually trying to protect something or just studying.

  3. #3
    DemoX is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    hey i am just studying but i am already using byte[] and have dat problem i dont if the salt function have to change it with another to can encrypt any thing other with the string? i dunno, do u wanna see my code complete to help?

  4. #4
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    please Post the complete Code

    Java Code:
    new CipherInputStream( fileInputStream, [B][COLOR="Red"]cipher [/COLOR][/B]);
    i am the future

  5. #5
    DemoX is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    sorry my code isnot organized:) and dat code only encrypt text i wanna know how can i encript any file

    Java Code:
     import java.awt.*;
       import java.awt.event.*;
       import java.io.*;
       import java.util.*;
       import java.security.*;
       import java.security.spec.*;
    
    
       import com.sun.crypto.provider.SunJCE;
    
    
       import javax.swing.*;
       import javax.crypto.*;
       import javax.crypto.spec.*;
    
       public class EncipherDecipher extends JFrame {
    
    
       private static final byte[] salt = {
       ( byte )0xf5, ( byte )0x33, ( byte )0x01, ( byte )0x2a,
       ( byte )0xb2, ( byte )0xcc, ( byte )0xe4, ( byte )0x7f
       };
    
    
       private int iterationCount = 100;
       private JTextField  passwordTextField;
       private JTextField  fromfileNameTextField;
       private JEditorPane fileContentsEditorPane;
       private JTextField  tofileNameTextField;
    
       public EncipherDecipher() {
    
    
       Security.addProvider( new SunJCE() );
    
    
       setSize( new Dimension( 400, 400 ) );
       setTitle( "Encryption and Decryption Example" );
    
    
       JPanel topPanel = new JPanel();
       topPanel.setBorder( BorderFactory.createLineBorder(
       Color.black ) );
       topPanel.setLayout( new BorderLayout() );
    
       // panel where password and file name labels will be placed
       JPanel labelsPanel = new JPanel();
       labelsPanel.setLayout( new GridLayout( 3, 1 ) );
       JLabel passwordLabel = new JLabel( " Password: " );
       JLabel fromfileNameLabel = new JLabel( " File Name: " );
       JLabel tofileNameLabel = new JLabel( " Destination: " );
       labelsPanel.add( fromfileNameLabel );
       labelsPanel.add( tofileNameLabel );    
       labelsPanel.add( passwordLabel );
       topPanel.add( labelsPanel, BorderLayout.WEST );
    
    
       JPanel textFieldsPanel = new JPanel();
       textFieldsPanel.setLayout( new GridLayout( 3, 1 ) );
       passwordTextField = new JPasswordField();
       fromfileNameTextField = new JTextField();
       tofileNameTextField = new JTextField();
       textFieldsPanel.add( fromfileNameTextField );
       textFieldsPanel.add( tofileNameTextField );
       textFieldsPanel.add( passwordTextField );
       topPanel.add( textFieldsPanel, BorderLayout.CENTER );
    
           JPanel ButtonsPanel = new JPanel();
           ButtonsPanel.setLayout(new GridLayout(3,1));
           JButton pass = new JButton("...");
            JButton pass1 = new JButton("...");
           JButton pass2 = new JButton("...");
           ButtonsPanel.add(pass);
           ButtonsPanel.add(pass1);
           ButtonsPanel.add(pass2);
           topPanel.add(ButtonsPanel, BorderLayout.EAST);
    
       JPanel middlePanel = new JPanel();
       middlePanel.setLayout( new BorderLayout() );
    
    
       JLabel fileContentsLabel = new JLabel();
       fileContentsLabel.setText( " File Contents" );
       middlePanel.add( fileContentsLabel, BorderLayout.NORTH );
    
    
       fileContentsEditorPane = new JEditorPane();
       middlePanel.add(
       new JScrollPane( fileContentsEditorPane ),
       BorderLayout.CENTER );
    
    
       JPanel bottomPanel = new JPanel();
       JButton readButton =
       new JButton( "Read from file" );
      readButton.addActionListener(
    
       new ActionListener() {
    
       public void actionPerformed( ActionEvent event )
        {
          readFromFile();
        }
        }
        );
        bottomPanel.add( readButton );
    
       JButton encryptButton =
       new JButton( "Encrypt and Write to File" );
       encryptButton.addActionListener(
    
       new ActionListener() {
    
       public void actionPerformed( ActionEvent event )
        {
       encryptAndWriteToFile();
        }
        }
        );
        bottomPanel.add( encryptButton );
    
    
        JButton decryptButton =
        new JButton( "Read from File and Decrypt" );
        decryptButton.addActionListener(
    
        new ActionListener() {
    
        public void actionPerformed( ActionEvent event )
        {
        readFromFileAndDecrypt();
        }
        }
        );
        bottomPanel.add( decryptButton );
    
    
    
    
        JPanel contentPane = ( JPanel ) this.getContentPane();
        contentPane.setLayout( new BorderLayout() );
        contentPane.add( topPanel, BorderLayout.NORTH );
        contentPane.add( middlePanel, BorderLayout.CENTER );
        contentPane.add( bottomPanel, BorderLayout.SOUTH );
    
        }
    
    
        private void encryptAndWriteToFile()
       {
    
    
       String originalText = fileContentsEditorPane.getText();
       String password = passwordTextField.getText();
       String fileName = fromfileNameTextField.getText();
    
    
       Cipher cipher = null;
    
       try {
    
        PBEKeySpec keySpec =
        new PBEKeySpec( password.toCharArray() );
    
    
        SecretKeyFactory keyFactory =
        SecretKeyFactory.getInstance( "PBEWithMD5AndDES" );
    
    
        SecretKey secretKey = keyFactory.generateSecret( keySpec );
    
    
        PBEParameterSpec parameterSpec =
        new PBEParameterSpec( salt, iterationCount );
    
    
        cipher = Cipher.getInstance( "PBEWithMD5AndDES" );
    
    
        cipher.init( Cipher.ENCRYPT_MODE, secretKey,
        parameterSpec );
        }
    
    
        catch ( NoSuchAlgorithmException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
        }
    
    
        catch ( InvalidKeySpecException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
        }
    
    
       catch ( InvalidKeyException exception ) {
       exception.printStackTrace();
       System.exit( 1 );
        }
    
    
        catch ( NoSuchPaddingException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
        }
    
    
        catch ( InvalidAlgorithmParameterException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
        }
    
    
        byte[] outputArray = null;
    
        try {
        outputArray = originalText.getBytes( "ISO-8859-1" );
        }
    
    
        catch ( UnsupportedEncodingException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
        }
    
    
       File file = new File( fileName );
       FileOutputStream fileOutputStream = null;
    
       try {
       fileOutputStream = new FileOutputStream( file );
       }
    
    
        catch ( IOException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
         }
    
    
         CipherOutputStream out =
         new CipherOutputStream( fileOutputStream, cipher );
    
    
        try {
        out.write( outputArray );
        out.flush();
        out.close();
        }
    
    
        catch ( IOException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
         }
       Vector fileBytes = new Vector();
    
    
       try {
       FileInputStream in = new FileInputStream( file );
    
    
        byte contents;
    
        while ( in.available() > 0 ) {
        contents = ( byte )in.read();
        fileBytes.add( new Byte( contents ) );
         }
    
        in.close();
         }
    
    
        catch ( IOException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
        }
    
    
        byte[] encryptedText = new byte[ fileBytes.size() ];
    
        for ( int i = 0; i < fileBytes.size(); i++ ) {
        encryptedText[ i ] =
        ( ( Byte ) fileBytes.elementAt( i ) ).byteValue();
        }
    
    
        fileContentsEditorPane.setText( new String( encryptedText ) );
        }
    
    
        private void readFromFileAndDecrypt()
        {
            Vector fileBytes = new Vector();
    
    
        String password = passwordTextField.getText();
        String fileName = fromfileNameTextField.getText();
    
    
        Cipher cipher = null;
        try {
    
     PBEKeySpec keySpec =
        new PBEKeySpec( password.toCharArray() );
    
    
        SecretKeyFactory keyFactory =
        SecretKeyFactory.getInstance( "PBEWithMD5AndDES" );
    
    
        SecretKey secretKey = keyFactory.generateSecret( keySpec );
    
    
        PBEParameterSpec parameterSpec =
        new PBEParameterSpec( salt, iterationCount );
    
    
        cipher = Cipher.getInstance( "PBEWithMD5AndDES" );
    
    
        cipher.init( Cipher.DECRYPT_MODE, secretKey,
        parameterSpec );
         }
    
    
        catch ( NoSuchAlgorithmException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
         }
    
    
        catch ( InvalidKeySpecException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
         }
    
    
        catch ( InvalidKeyException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
         }
    
    
        catch ( NoSuchPaddingException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
        }
    
    
        catch ( InvalidAlgorithmParameterException exception ) {
        exception.printStackTrace();
       System.exit( 1 );
        }
    
    
    
        try {
        File file = new File( fileName );
        FileInputStream fileInputStream =
        new FileInputStream( file );
    
        CipherInputStream in =
        new CipherInputStream( fileInputStream, cipher );
    
    
        byte contents = ( byte ) in.read();
    
        while ( contents != -1 ) {
        fileBytes.add( new Byte( contents ) );
        contents = ( byte ) in.read();
         }
        in.close();
    
         }
    
    
        catch ( IOException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
         }
    
    
        byte[] decryptedText = new byte[ fileBytes.size() ];
    
        for ( int i = 0; i < fileBytes.size(); i++ ) {
        decryptedText[ i ] =
        ( ( Byte )fileBytes.elementAt( i ) ).byteValue();
         }
    
    
    
        fileContentsEditorPane.setText( new String( decryptedText ) );
            File file = new File( "C:\\Documents and Settings\\DemoX\\Desktop\\image2" );
       FileOutputStream fileOutputStream = null;
    
       try {
       fileOutputStream = new FileOutputStream( file );
       }
    
    
        catch ( IOException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
         }
    
    
        OutputStream out;
            out =   fileOutputStream;
    
    
            try {
        out.write(decryptedText);
        out.flush();
        out.close();
        }
        // handle IOException
        catch ( IOException exception ) {
        exception.printStackTrace();
        System.exit( 1 );
        }
           
    
        
        }
    
          private void readFromFile() {
              Vector fileBytes = new Vector();
              String fileName = fromfileNameTextField.getText();
    
    
              byte[] decryptedText = new byte[0];
              try {
                  File file = new File(fileName);
                  FileInputStream fileInputStream =
                          new FileInputStream(file);
                  InputStream in;
                  in = fileInputStream;
                  byte contents = (byte) in.read();
    
    
                   int m = (int) file.length();
    
    
                  decryptedText = new byte[m-1];
    
    
                  in.read(decryptedText);
    
                  in.close();
              }
              // handle IOException
              catch (IOException exception) {
                  exception.printStackTrace();
                  System.exit(1);
              }
              fileContentsEditorPane.setText(new String(decryptedText));
          }

  6. #6
    DemoX is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by rjuyal View Post
    please Post the complete Code

    Java Code:
    new CipherInputStream( fileInputStream, [B][COLOR="Red"]cipher [/COLOR][/B]);
    hey u will get dat code from my complete code

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Overall design changes

    I pulled the encipherment code out of the JFrame, it will work as you had it but to make the encipherment / encryption as subject of study I suggest we move all the code to a dedicated class. We then just declare an instance of that class in the JFrame code and call the methods on it as needed. There is some way of separating the DataModel - which is what this is for now - from the View and Controller. For now I just write a class that has a main() and call that my Controller. We then may be able to declare a JFrame and show it and so on, the way that links up with the crypto class is difficult to disentangle { :eek:} I have been able to do it but our time is better spent right now studying the crypto work at hand. Doing all the calc's in an actionPerformed(ActionEvent) is possibly the way to do it, I would suggest calling setText and so on in a Controller Class, using a JFrame instance and a EncipherDecipher extends Example instance. That makes the driving of the code more convoluted but perhaps an OO'er may grace us with a suggestion. I tried extending some of the classes in java.security and javax.crypto but most of them are final and making one of them the base class may or may not be helpful for you. Probably for now just start a base class yourself, giving it a name that makes sense to you, just use anything you can get to run. Salt is not part of the protected data.

    Java Code:
    // Encryption preliminary efforts. Just studying. 
    // All caveats apply, except as noted in the caveat errata.
    // Salt is not a function. It is to 'get things going'
    // Do not worry about organized code just yet, focus on 
    // effectiveness in studying crypto. It is not easy.
    
    // I suggest moving all the crypto to a dedicated class. 
    // I am not a cryptographer, I am studying your work as
    // a way of learning also, do not use the code except as such.
    
    import com.sun.crypto.provider.SunJCE;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    
    public class EncipherDecipher extends Example {
        
        /* should not be necessary to Security.addProvider( new SunJCE() ); */
    
        private void encryptAndWriteToFile()
        {
            try 
            {
                // Get plaintext to encipher.
                String originalText = fileContentsEditorPane.getText();
                String password = passwordTextField.getText();
                String fileName = fromfileNameTextField.getText();
    
                Cipher cipher = = Cipher.getInstance( "PBEWithMD5AndDES" );
    
                PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
                SecretKeyFactory keyFactory =
                SecretKeyFactory.getInstance( "PBEWithMD5AndDES" );
                SecretKey secretKey = keyFactory.generateSecret( keySpec );
                PBEParameterSpec parameterSpec =
                new PBEParameterSpec( salt, iterationCount );
                // If code thusfar compiles, we initalize the Cipher.
                cipher.init( Cipher.ENCRYPT_MODE, secretKey,
                parameterSpec );
                // Intermediate byte[] probably done here:
                CipherOutputStream out =
                new CipherOutputStream( fileOutputStream, cipher );
                /* large chunk skipped. */
            }
            // Keep bulky catch() chain as you have it, but put it here.
            catch(){}
            catch(){}
            catch(){}
            catch(){}
            catch(){}
        }
        private void readFromFile() {
            ;// put decryption here.
        }
    }
    Here is a good list of links, not all of them work but the list drills past a bunch of books that are mostly interested in keeping you buying books rather than making progress. Note the style of the site, that is signature work: html 1.1
    ron rivest
    Last edited by Nicholas Jordan; 06-20-2008 at 11:20 PM. Reason: good list of links

  8. #8
    DemoX is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    oh many thnxxxxxxxxx i am trying every thing u saied :) many thnxx

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Nintey nine cat tails await your rocking rays.

    ( It's natural for real cryptographers, sifting silliness to see what is what. Watch to see if you find yourself doing some of that. )

  10. #10
    DemoX is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    hey Nicholas i am tried every thing and i am really made the program but i want to know if the idea i have now is right or wrong is every type of files have different type of encription

Posting Permissions

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