Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-13-2008, 12:00 PM
Member
 
Join Date: Apr 2008
Posts: 10
DemoX is on a distinguished road
Encription problem
hey, i developed encription code

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

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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-13-2008, 03:34 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 559
Nicholas Jordan is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-15-2008, 01:01 PM
Member
 
Join Date: Apr 2008
Posts: 10
DemoX is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-15-2008, 01:36 PM
rjuyal's Avatar
Senior Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
rjuyal is on a distinguished road
please Post the complete Code

Code:
new CipherInputStream( fileInputStream, cipher );
__________________
i am the future
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-15-2008, 01:41 PM
Member
 
Join Date: Apr 2008
Posts: 10
DemoX is on a distinguished road
sorry my code isnot organized and dat code only encrypt text i wanna know how can i encript any file

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)); }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-15-2008, 01:43 PM
Member
 
Join Date: Apr 2008
Posts: 10
DemoX is on a distinguished road
Quote:
Originally Posted by rjuyal View Post
please Post the complete Code

Code:
new CipherInputStream( fileInputStream, cipher );
hey u will get dat code from my complete code
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-20-2008, 09:16 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 559
Nicholas Jordan is on a distinguished road
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 { } 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.

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-21-2008 at 12:20 AM. Reason: good list of links
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-21-2008, 08:04 AM
Member
 
Join Date: Apr 2008
Posts: 10
DemoX is on a distinguished road
oh many thnxxxxxxxxx i am trying every thing u saied many thnxx
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-22-2008, 07:49 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 559
Nicholas Jordan is on a distinguished road
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. )
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-29-2008, 02:34 AM
Member
 
Join Date: Apr 2008
Posts: 10
DemoX is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 04:03 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org