2 Replies - 3390 Views - Last Post: 16 September 2012 - 03:24 AM Rate Topic: -----

#1 bladevetren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-August 12

byte[ ] fileinput stream error

Posted 15 September 2012 - 11:29 PM

hello, i am creating a file enryptor which converts the contents of a file to a byte array and then encrypts it via RSA encryption. the problem wich i am encoutering is that when i write the byte array to the file after decrypting it, it still seems to be encrypted. therefore why is this problem occuring and how may i resolve it?
here is the code:
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;







public class file_Encryptor extends JFrame
{
	private static String key1;
	private static String exponent1;
	private static int keySize3;
	private static String Keysize3;


	private static final int WIDTH = 400;
	private static final int HEIGHT = 300;
	
	private JLabel exportL, cipherTextL,export1L;
	private static JTextField exportTF;
	private static JTextField export1TF;
	private JTextField cipherTextTF;

	
	
	private JButton calculateB, exitB;
	private CalculateButtonHandler cbHandler;
	private ExitButtonHandler ebHandler;
	public file_Encryptor (int keySize)
    {
        if (keySize < keySize3)
            throw new IllegalArgumentException("Key size too small.");
        SecureRandom rand = new SecureRandom();
    }
	public file_Encryptor ()
	{
		exportL = new JLabel("Enter file name: ", SwingConstants.RIGHT);
	export1L=new JLabel("Enter file extension: ", SwingConstants.RIGHT);
		cipherTextL = new JLabel("cipherText: ", SwingConstants.RIGHT);
		
		
		
		exportTF = new JTextField(10);
		export1TF =new JTextField(10);
		cipherTextTF = new JTextField(10);
		
	
	
		calculateB = new JButton("Encrypt");
		cbHandler = new CalculateButtonHandler();
		calculateB.addActionListener(cbHandler);
		exitB = new JButton("Exit");
		ebHandler = new ExitButtonHandler();
		exitB.addActionListener(ebHandler);
		
		setTitle("encryption gui");
		Container pane = getContentPane();
		pane.setLayout(new GridLayout(4, 3));
		
		pane.add(exportL);
		pane.add(exportTF);
		
		pane.add(export1L);
		pane.add(export1TF);
	
		pane.add(cipherTextL);
		pane.add(cipherTextTF);
		
		
		
		pane.add(calculateB);
		pane.add(exitB);
		
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	private class CalculateButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			try {
				 
				File fXmlFile = new File("KeySize.xml");
				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
				Document doc = dBuilder.parse(fXmlFile);
				doc.getDocumentElement().normalize();
		 
				NodeList nList = doc.getElementsByTagName("root");
			
		 
				for (int temp = 0; temp < nList.getLength(); temp++) {
		 
				   Node nNode = nList.item(temp);
				   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
		 
				      Element eElement = (Element) nNode;
	         
				  Keysize3 =   getTagValue("KeySize", eElement);
				 keySize3 = new Integer(Keysize3);
			      
		 
				   }
				}
			  } catch (Exception e1) {
				e1.printStackTrace();
			  }
			try {
				 
				File fXmlFile = new File("publicKey.xml");
				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
				Document doc = dBuilder.parse(fXmlFile);
				doc.getDocumentElement().normalize();
		 
				NodeList nList = doc.getElementsByTagName("publicKey");
			
		 
				for (int temp = 0; temp < nList.getLength(); temp++) {
		 
				   Node nNode = nList.item(temp);
				   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
		 
				      Element eElement = (Element) nNode;
	         
				  key1 =   getTagValue("key", eElement);
				 exponent1=getTagValue("exponent", eElement);
			       
		 
				   }
				}
			  } catch (Exception n) {
				n.printStackTrace();
			  }
			int keySize = keySize3;
			file_Encryptor  cipher = new file_Encryptor (keySize);
			
    		
    		
    	
    		
    		 try {
    			String data =exportTF.getText();
    			String data1 = export1TF.getText();
    			 String msg = exportTF.getText()+export1TF.getText();
    	    		File f1 = new File(msg);
    				String ciphertext = cipher.encrypt(f1);
    	    		
    	            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    	            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
    	            Document doc = docBuilder.newDocument();

    	         
    	            Element root = doc.createElement("root");
    	            doc.appendChild(root);

    	            
    	            Comment comment = doc.createComment("cipherText");
    	            root.appendChild(comment);

    	           
    	            Element Key = doc.createElement("cipherText");
    	    		Key.appendChild(doc.createTextNode(ciphertext));
    	    		root.appendChild(Key);
    	            
    	    		Comment comment1 = doc.createComment("key");
    	            root.appendChild(comment1);

    	            Element Key8 = doc.createElement("key");
    	    		Key8.appendChild(doc.createTextNode(key1));
    	    		root.appendChild(Key8);
    	    		
    	    		Comment comment2 = doc.createComment("file data");
    	            root.appendChild(comment2);
    	            
    	            Element file10 = doc.createElement("fileName");
    	    		file10.appendChild(doc.createTextNode(data+""));
    	    		root.appendChild(file10);
    	    		
    	    		   Element file8 = doc.createElement("fileExtension");
       	    		file8.appendChild(doc.createTextNode(data1));
       	    		root.appendChild(file8);
    	     
    	            TransformerFactory transfac = TransformerFactory.newInstance();
    	            Transformer trans = transfac.newTransformer();
    	            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    	            trans.setOutputProperty(OutputKeys.INDENT, "yes");

    	        
    	            StringWriter sw = new StringWriter();
    	            StreamResult result = new StreamResult(new File("fileData.xml"));
    	            DOMSource source = new DOMSource(doc);
    	            trans.transform(source, result);
    	            String xmlString = sw.toString();

    	            cipherTextTF.setText(ciphertext);
    	      
      } catch (Exception k) {
    	            System.out.println(k);
    	        }
    		 
    	    }
    }
 
		
	
	public class ExitButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
	}
	 public static void main(String[] args){
		 File f1 = new File("cipherText.xml");
	   	 f1.delete();
	   	file_Encryptor  rectObj = new file_Encryptor ();
	   	decryption rectObj3= new decryption();
	   	RSA rectObj4= new RSA();
	   	KeySize rectObj5= new KeySize();
		 
		 
	 }
	 private static String getTagValue(String sTag, Element eElement) {
			NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
		 
		        Node nValue = (Node) nlList.item(0);
		 
			return nValue.getNodeValue();
	 }
	
	
	private String encrypt(File plainText) throws IOException
    {
		
	 byte[] msg = byteArray(plainText);
	 
	  
 

		BigInteger Key = new BigInteger(key1);
		BigInteger exponent = new BigInteger(exponent1);
   
	  BigInteger a = new BigInteger(msg);
   
        byte[] encrypted = a.modPow(exponent, Key).toByteArray();
        return toHex(encrypted);
    }
	
	private byte[] byteArray(File plainText) {
		FileInputStream fileInputStream=null;
		 
      
 
        byte[] bFile = new byte[(int) plainText.length()];
 
        try {
            
	    fileInputStream = new FileInputStream(plainText);
	    fileInputStream.read(bFile);
	    fileInputStream.close();
 
	
        }catch(Exception e){
            e.printStackTrace();
        }
		return bFile;
	}
	static String toHex(byte[] bytes)
    {
        BigInteger bi = new BigInteger(1, bytes);
        return String.format("%0" + (bytes.length << 1) + "X", bi);
    }

	
	
	
	 public static class decryption extends JFrame
		{
			private static String File;
			private static String fileExtension;
			private static String Keysize3;
			private static int keySize3;
			private static final int WIDTH = 400;
			private static final int HEIGHT = 300;
			private static String N3;
			private static String cipherText;
			private JLabel  cipherTextL, exponentL;
		
			private JTextField cipherTextTF;
			
			private static JTextField exponentTF;
			
			private JButton calculateB, exitB;
			private CalculateButtonHandler cbHandler;
			private ExitButtonHandler ebHandler;
			public decryption(int keySize)
		    {
		        if (keySize < 1)
		            throw new IllegalArgumentException("Key size too small.");
		        SecureRandom rand = new SecureRandom();
		    }
			public decryption()
			{
				
				exponentL = new JLabel ("privateKey: ", SwingConstants.RIGHT);
				cipherTextL = new JLabel("plainText: ", SwingConstants.RIGHT);
			
				
				exponentTF= new JTextField(10);
				cipherTextTF = new JTextField(10);
				
			
			exponentTF=new JTextField(10);
			
			
				calculateB = new JButton("decrypt");
				cbHandler = new CalculateButtonHandler();
				calculateB.addActionListener(cbHandler);
				exitB = new JButton("Exit");
				ebHandler = new ExitButtonHandler();
				exitB.addActionListener(ebHandler);
				
				setTitle("decrypyion gui");
				Container pane = getContentPane();
				pane.setLayout(new GridLayout(4, 3));
				
			
				pane.add(exponentL);
				pane.add(exponentTF);
				
				pane.add(cipherTextL);
				pane.add(cipherTextTF);
				
				
				pane.add(calculateB);
				pane.add(exitB);
				
				setSize(WIDTH, HEIGHT);
				setVisible(true);
				setDefaultCloseOperation(EXIT_ON_CLOSE);
			}
			
		
			 
			private class CalculateButtonHandler implements ActionListener
			{
				public void actionPerformed(ActionEvent e)
				{
					 try {
						 
							File fXmlFile = new File("KeySize.xml");
							DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
							DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
							Document doc = dBuilder.parse(fXmlFile);
							doc.getDocumentElement().normalize();
					 
							NodeList nList = doc.getElementsByTagName("root");
						
					 
							for (int temp = 0; temp < nList.getLength(); temp++) {
					 
							   Node nNode = nList.item(temp);
							   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					 
							      Element eElement = (Element) nNode;
				         
							  Keysize3 =   getTagValue("KeySize", eElement);
							 keySize3 = new Integer(Keysize3);
						       
					 
							   }
							}
						  } catch (Exception e1) {
							e1.printStackTrace();
						  }
					 decryption cipher = new decryption(keySize3);
					 try {
						 
							File fXmlFile = new File("fileData.xml");
							DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
							DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
							Document doc = dBuilder.parse(fXmlFile);
							doc.getDocumentElement().normalize();
					 
							NodeList nList = doc.getElementsByTagName("root");
						
					 
							for (int temp = 0; temp < nList.getLength(); temp++) {
					 
							   Node nNode = nList.item(temp);
							   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					 
							      Element eElement = (Element) nNode;
				         
							  N3 =   getTagValue("key", eElement);
							 cipherText=getTagValue("cipherText", eElement);
							 File= getTagValue("fileName", eElement);
					 fileExtension=getTagValue("fileExtension", eElement);
							   }
							}
						  } catch (Exception e2) {
							e2.printStackTrace();
						  }
					 
					 try{
					 
					 
	byte[] original2=cipher.decrypt(cipherText);
	BigInteger original = new BigInteger (original2);
	cipherTextTF.setText("" + original);
	

					 } catch (Exception e2) {
							e2.printStackTrace();
						  }
				}

			}
			 public byte[] decrypt(String cipherText) throws Exception
			    {
				 String d2=exponentTF.getText();
		    		
		    		BigInteger N1 = new BigInteger(N3);
		    		BigInteger d1 =  new BigInteger(d2);
		        BigInteger encrypted = new BigInteger(cipherText, 16);
		        byte[] original = encrypted.modPow(d1, N1).toByteArray();
			       FileOutputStream fileOuputStream = 
			               new FileOutputStream(""+File+"2"+fileExtension); 
			     fileOuputStream.write(original);
			     fileOuputStream.close();
			     return original;
			        
					
			 
			        
			    }
			 private static String getTagValue(String sTag, Element eElement) {
					NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
				 
				        Node nValue = (Node) nlList.item(0);
				 
					return nValue.getNodeValue();
			 }
			
			public class ExitButtonHandler implements ActionListener
			{
				public void actionPerformed(ActionEvent e)
				{
					System.exit(0);
				}
			}
			
		}
	 public static class RSA extends JFrame
	 {
	 	private static String Keysize1;
	 private static int keySize1;
	      private static BigInteger N;
	         private BigInteger phiN;
	         private static BigInteger p;
	         private static BigInteger q;
	         private static BigInteger e;
	         private static BigInteger d;
	     private static final int WIDTH = 400;
	     private static final int HEIGHT = 300;
	      
	     private JLabel   KeyL;
	     
	  
	      
	     private static JTextField KeyTF;

	 private JButton calculateB, exitB;
	     private CalculateButtonHandler cbHandler;
	     private ExitButtonHandler ebHandler;
	      
	     public RSA()
	     {
	      
	         KeyL= new JLabel ("privateKey: ", SwingConstants.RIGHT);
	    
	          
	          
	          
	          
	         KeyTF =new JTextField(10);

	         calculateB = new JButton("generate");
	         cbHandler = new CalculateButtonHandler();
	         calculateB.addActionListener(cbHandler);
	         exitB = new JButton("Exit");
	         ebHandler = new ExitButtonHandler();
	         exitB.addActionListener(ebHandler);
	          
	         setTitle("publicKey gui");
	         Container pane = getContentPane();
	         pane.setLayout(new GridLayout(3, 2));
	          
	      
	         pane.add(KeyL);
	         pane.add(KeyTF);
	         
	         
	          
	         pane.add(calculateB);
	         pane.add(exitB);
	      
	     setSize(WIDTH, HEIGHT);
	         setVisible(true);
	         setDefaultCloseOperation(EXIT_ON_CLOSE);
	 }
	      
	     private class CalculateButtonHandler implements ActionListener
	     {
	         
	         public void actionPerformed(ActionEvent n)
	         {
	         	 
	        	 try {
	 			 
	 				File fXmlFile = new File("KeySize.xml");
	 				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	 				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	 				Document doc = dBuilder.parse(fXmlFile);
	 				doc.getDocumentElement().normalize();
	 		 
	 				NodeList nList = doc.getElementsByTagName("root");
	 			
	 		 
	 				for (int temp = 0; temp < nList.getLength(); temp++) {
	 		 
	 				   Node nNode = nList.item(temp);
	 				   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
	 		 
	 				      Element eElement = (Element) nNode;
	 	         
	 				  Keysize1 =   getTagValue("KeySize", eElement);
	 				 keySize1 = new Integer(Keysize1);
	 			       
	 		 
	 				   }
	 				}
	 			  } catch (Exception e) {
	 				e.printStackTrace();
	 			  }
	        	RSA cipher = new RSA(keySize1);    
	              try {
	             	 
	             	
	                 String key = N.toString();
	                 String exponent = e.toString();
	                 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
	                 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
	                 Document doc = docBuilder.newDocument();
	         
	               
	                 Element root = doc.createElement("publicKey");
	                 doc.appendChild(root);
	  
	                  
	                 Comment comment = doc.createComment("publicKey");
	                 root.appendChild(comment);
	  
	                 
	                 Element Key = doc.createElement("key");
	                 Key.appendChild(doc.createTextNode(key));
	                 root.appendChild(Key);
	                  
	                 Comment comment1 = doc.createComment("exponent");
	                 root.appendChild(comment1);
	                  
	                 Element lastname = doc.createElement("exponent");
	                 lastname.appendChild(doc.createTextNode(exponent));
	                 root.appendChild(lastname);
	           
	                 TransformerFactory transfac = TransformerFactory.newInstance();
	                 Transformer trans = transfac.newTransformer();
	                 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
	                 trans.setOutputProperty(OutputKeys.INDENT, "yes");
	  
	              
	                 StringWriter sw = new StringWriter();
	                 StreamResult result = new StreamResult(new File("publicKey.xml"));
	                 DOMSource source = new DOMSource(doc);
	                 trans.transform(source, result);
	                 String xmlString = sw.toString();
	                  
	           
	                  
	             	     
	 KeyTF.setText(d+""); 
	    } catch (Exception k) {
	                 System.out.println(k);
	           
	           
	           
	     }
	     }
	     }
	     public class ExitButtonHandler implements ActionListener
	     {
	         public void actionPerformed(ActionEvent e)
	         {
	             System.exit(0);
	         }
	     }
	       
	     
	  
	    
	     public RSA(int keySize)
	     {
	         if (keySize < 1)
	             throw new IllegalArgumentException("Key size too small.");
	         SecureRandom rand = new SecureRandom();
	         generatePQ(keySize / 2, rand);
	         N = p.multiply(q);
	         phiN = (p.subtract(BigInteger.ONE)).multiply((q.subtract(BigInteger.ONE)));
	         generateExponents(invertibleSet());
	     }
//work in progress
	 public static BigInteger rsa(BigInteger i, BigInteger i1){
	 if(i.compareTo(i1)>=0){
	 	i = (p.multiply(q)).subtract(p);
	 	return i;
	 }
	 else{
	 	i = (p.multiply(q)).add(q);
	 	return i;
	 }
	 }
	 public static BigInteger phi(BigInteger x){
	 if(p.compareTo(q)>=0){
	 	x = ((p.multiply(q)).subtract(p)).subtract(q.add(BigInteger.ONE)).subtract(p).add(BigInteger.ONE);
	 	return x;
	 }
	 else{
	 	x=((p.multiply(q)).add(q)).subtract(p.add(BigInteger.ONE)).subtract(q).add(BigInteger.ONE);
	 	return x;
	 }
	 }
	 //
	     private void generatePQ(int bitLength, Random rand)
	     {
	         while (true)
	         {
	             p = generateOddPrime(bitLength, rand);
	         q = generateOddPrime(bitLength, rand);
	             if (!p.equals(q))
	             return;
	         }
	 }
	  
	    
	     private BigInteger generateOddPrime(int bitLength, Random rand)
	     {
	         BigInteger two = new BigInteger("2");
	         while (true)
	         {
	             BigInteger prime = BigInteger.probablePrime(bitLength, rand);
	             if (!prime.mod(two).equals(BigInteger.ZERO))
	                 return prime;
	     }
	     }
	  
	   
	     private void generateExponents(BigInteger[] invertibleSet)
	     {
	         Random rand = new Random();
	         while (true)
	         {
	             BigInteger invertible = invertibleSet[rand
	                     .nextInt(invertibleSet.length)];
	             BigInteger inverse = invertible.modInverse(phiN);
	             if (invertible.multiply(inverse).mod(phiN)
	                     .equals(BigInteger.ONE.mod(phiN)))
	             {
	                 e = invertible;
	                 
	                 d = inverse;
	                 return;
	             }
	    }
	     }
	  
	    
	     private BigInteger[] invertibleSet()
	     {
	         final int maxSize = 100000;
	         Set<BigInteger> invertibles = new HashSet<BigInteger>();
	         BigInteger end = N.subtract(BigInteger.ONE);
	         for (BigInteger i = new BigInteger("5"); i.compareTo(end) < 0; i = i
	                 .add(BigInteger.ONE))
	         {
	             if (i.gcd(phiN).equals(BigInteger.ONE))
	             {
	             invertibles.add(i);
	                 if (invertibles.size() == maxSize)
	                     break;
	         }
	         }
	         return invertibles.toArray(new BigInteger[invertibles.size()]);
	     }
	  
	  
	    

	      
	      
	      

	  
	     private String toHex(byte[] bytes)
	     {
	         BigInteger bi = new BigInteger(1, bytes);
	         return String.format("%0" + (bytes.length << 1) + "X", bi);
	     }

	   
	     public BigInteger getModulus()
	     {
	     return N;
	     }
	  
	    
	     public BigInteger getPublicKeyExponent()
	     {
	         return e;
	     }
	  
	   
	     public BigInteger getPrivateKeyExponent()
	     {
	     return d;
	     }
	  
	     private static String getTagValue(String sTag, Element eElement) {
	 		NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	 	 
	 	        Node nValue = (Node) nlList.item(0);
	 	 
	 		return nValue.getNodeValue();
	  }
}
	   public static class KeySize extends JFrame
   	{
   	    
   	    private static final int WIDTH = 400;
   	    private static final int HEIGHT = 300;
   	     
   	    private JLabel   KeyL;
   	    
   	 
   	     
   	    private static JTextField KeyTF;
   	
       private JButton calculateB, exitB;
   	    private CalculateButtonHandler cbHandler;
   	    private ExitButtonHandler ebHandler;
   	     
   	    public KeySize()
   	    {
   	     
   	        KeyL= new JLabel ("enter key size: ", SwingConstants.RIGHT);
   	   
   	         
   	         
   	         
   	         
   	        KeyTF =new JTextField(10);

   	        calculateB = new JButton("generate");
   	        cbHandler = new CalculateButtonHandler();
   	        calculateB.addActionListener(cbHandler);
   	        exitB = new JButton("Exit");
   	        ebHandler = new ExitButtonHandler();
   	        exitB.addActionListener(ebHandler);
   	         
   	        setTitle("Keysize gui");
   	        Container pane = getContentPane();
   	        pane.setLayout(new GridLayout(3, 2));
   	         
            
   	        pane.add(KeyL);
   	        pane.add(KeyTF);
   	        
   	        
   	         
   	        pane.add(calculateB);
   	        pane.add(exitB);
            
           setSize(WIDTH, HEIGHT);
   	        setVisible(true);
   	        setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
   	     
   	    private class CalculateButtonHandler implements ActionListener
   	    {
   	        
   	        public void actionPerformed(ActionEvent n)
   	        {
   	        	
   	     
   	             
   	             try {
   	            	 
   	            	
   	                DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
   	                DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
   	                Document doc = docBuilder.newDocument();
               
   	              
   	                Element root = doc.createElement("root");
   	                doc.appendChild(root);
   	 
   	                 
   	                
   	 
   	                
   	                Element Key = doc.createElement("KeySize");
   	                Key.appendChild(doc.createTextNode(KeyTF.getText()));
   	                root.appendChild(Key);
   	                 
   	           
   	          
   	                TransformerFactory transfac = TransformerFactory.newInstance();
   	                Transformer trans = transfac.newTransformer();
   	                trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
   	                trans.setOutputProperty(OutputKeys.INDENT, "yes");
   	 
   	             
   	                StringWriter sw = new StringWriter();
   	                StreamResult result = new StreamResult(new File("Keysize.xml"));
   	                DOMSource source = new DOMSource(doc);
   	                trans.transform(source, result);
   	                String xmlString = sw.toString();
   	                 
   	          
   	                 
   	                 

   	   } catch (Exception k) {
   	                System.out.println(k);
   	          
   	          
   	          
   	    }
   	    }
   	    }
   	    public class ExitButtonHandler implements ActionListener
   	    {
   	        public void actionPerformed(ActionEvent e)
   	        {
   	            System.exit(0);
   	        }
   	    }
   	  
   	    
   	}
}



    		








Is This A Good Question/Topic? 0
  • +

Replies To: byte[ ] fileinput stream error

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: byte[ ] fileinput stream error

Posted 16 September 2012 - 12:02 AM

I would try converting it to a string by using new String(byte[]); and then printing it out to the file using a BufferedWriter.
Was This Post Helpful? 1
  • +
  • -

#3 bladevetren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-August 12

Re: byte[ ] fileinput stream error

Posted 16 September 2012 - 03:24 AM

i tried your soloution but it still does'nt seem to work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1