FTP failing to upload file

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1210 Views - Last Post: 03 September 2012 - 05:11 AM Rate Topic: -----

#1 bladevetren  Icon User is offline

  • New D.I.C Head

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

FTP failing to upload file

Posted 02 September 2012 - 09:36 AM

hello, i am attempting to create a program which creates a xml file holding a publicKey, which then then uploads it to a FTP server. the program creates the xml file but does not upload it and gives the error Error: STOR publicKey.xml: 550 /publicKey.xml: Permission denied. therefore why is this problem occuring, and how may i fix it?
here is the code:
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.math.BigInteger;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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.Text;



import java.io.StringWriter;
import java.math.BigInteger;
import java.net.Socket;
import java.security.SecureRandom;


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 sun.net.ftp.*;

public class RSA extends JFrame
{
	 private static BigInteger N;
		private BigInteger phiN;
		private BigInteger p;
		private BigInteger q;
		private static BigInteger e;
		private static BigInteger d;
	private static final int WIDTH = 600;
	private static final int HEIGHT = 500;
	
	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("Encrypt");
		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
	{
		@SuppressWarnings("restriction")
		public void actionPerformed(ActionEvent n)
		{
			
	  		 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("C:/Users/John.dell2/Documents/Ryan/java test/xml publicKey generator/publicKey.xml"));
 	            DOMSource source = new DOMSource(doc);
 	            trans.transform(source, result);
 	            String xmlString = sw.toString();
 	            
 	           String hostname = "Johndell2-PC"; 
 	 	    	   String username = "system";//domain example 
 	 	    	   String password = "KVC12A"; 
 	 	    	
 	 	    	  
 	 	    	   FtpClient ftp = new FtpClient();
 	 	    	   try {
 	 	    	      ftp.openServer(hostname); 
 	 	    	      ftp.login(username, password); 
 	 	    	      ftp.binary(); 
 	 	    	     
 	 	    	      File publicKey=(new File("C:/Users/John.dell2/Documents/Ryan/java test/xml publicKey generator/publicKey.xml"));
 	 	    	      OutputStream out = ftp.put(publicKey.getName()); //Start upload
 	 	    	      InputStream in = new FileInputStream(publicKey);
 	 	    	      byte c[] = new byte[4096];
 	 	    	      int read = 0;
 	 	    	      while ((read = in.read(c)) != -1 ) {
 	 	    	         out.write(c, 0, read);
 	 	    	      } 
 	 	    	      in.close();
 	 	    	      out.close();
 	 	    	      ftp.closeServer(); 
 	 	    	   } catch (Exception e) {
 	 	    	      System.out.println("Error: " + e.getMessage());
 	 	    }
 	            
String privateKey = d+xmlString;
 	            
KeyTF.setText(""+privateKey);  
   } 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 < 1000)
            throw new IllegalArgumentException("Key size too small.");
        SecureRandom rand = new SecureRandom();
        generatePQ(keySize / 2, rand);
        N = rsa(p, q);
        phiN = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
        generateExponents(invertibleSet());
    }
BigInteger rsa(BigInteger i, BigInteger i1) {
		
    	
		if(p.compareTo(q)>0){
			BigInteger a=p.multiply(q);
		 i=a.subtract(q);
return i;
		}
		else{
			i=p.multiply(q);
			return i;
		}
 }
  
    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;
                //input the alternative value of the privateKey here
                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;
    }

  
    public static void main(String[] args){
    
    	RSA rectObj = new RSA();
    	int x = 1;
    	
    	 int keySize = 1024;
         RSA cipher = new RSA(keySize);
    }
}
    
    		
  
    

	
     	


    	
       
    	
   
     
        
    

 

     
 



Is This A Good Question/Topic? 0
  • +

Replies To: FTP failing to upload file

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: FTP failing to upload file

Posted 02 September 2012 - 10:51 AM

Check that ftp at the command line first with the Windows command line ftp program (if it still exists)
Was This Post Helpful? 0
  • +
  • -

#3 bladevetren  Icon User is offline

  • New D.I.C Head

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

Re: FTP failing to upload file

Posted 02 September 2012 - 12:04 PM

the ftp seems to be working fine, but i cant seem to login to my ftp server as the command line seems to be ignoring all chracters which i input into the password:

Microsoft Windows [Version 6.1.7600]
Copyright © 2009 Microsoft Corporation. All rights reserved.

C:\Users\John.dell2>ftp
ftp> open Johndell2-PC
Connected to Johndell2-PC.
220 Serv-U FTP Server v12.1 ready...
User (Johndell2-PC:(none)): export1
331 User name okay, need password.
Password:
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: FTP failing to upload file

Posted 02 September 2012 - 03:07 PM

Well it certainly won't echo the password characters to the console - that's by design. Type them in anyway
Was This Post Helpful? 0
  • +
  • -

#5 bladevetren  Icon User is offline

  • New D.I.C Head

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

Re: FTP failing to upload file

Posted 02 September 2012 - 11:21 PM

ok i can login Via command prompt so the server is fine, but the program still throws the exception:

Error: STOR publicKey.xml: 550 /publicKey.xml: Permission denied.
Was This Post Helpful? 0
  • +
  • -

#6 bladevetren  Icon User is offline

  • New D.I.C Head

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

Re: FTP failing to upload file

Posted 02 September 2012 - 11:34 PM

i made the user administrator and now the program thows the exception:

Error: Not logged in
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: FTP failing to upload file

Posted 03 September 2012 - 01:42 AM

Quote

ok i can login Via command prompt so the server is fine, but the program still throws the exception:


Logging in alone is not sufficient. You must be able to put the file too. Did you?
Was This Post Helpful? 0
  • +
  • -

#8 bladevetren  Icon User is offline

  • New D.I.C Head

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

Re: FTP failing to upload file

Posted 03 September 2012 - 02:19 AM

i appoligise i do not understand the terminology 'put' in this context
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: FTP failing to upload file

Posted 03 September 2012 - 02:27 AM

Quote

i do not understand the terminology 'put' in this context

... and yet you use it in your code ;) :

Quote

OutputStream out = ftp.put(publicKey.getName());

Was This Post Helpful? 0
  • +
  • -

#10 bladevetren  Icon User is offline

  • New D.I.C Head

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

Re: FTP failing to upload file

Posted 03 September 2012 - 02:33 AM

ahh i see i thought you meant it in the usual gramatical sense not to upload data, ok so how may i deal with the program being unable to log me in to the ftp server?
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: FTP failing to upload file

Posted 03 September 2012 - 02:46 AM

Quote

i thought you meant it in the usual gramatical sense

No, hence the inline code markup.Well, you still haven't told me - are you able to put from the command line?
Was This Post Helpful? 0
  • +
  • -

#12 bladevetren  Icon User is offline

  • New D.I.C Head

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

Re: FTP failing to upload file

Posted 03 September 2012 - 03:00 AM

yes i am able to
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: FTP failing to upload file

Posted 03 September 2012 - 03:14 AM

Can you paste the console commands for your put from the beginning of your session (editing sensitive info as necessary)?
Was This Post Helpful? 0
  • +
  • -

#14 bladevetren  Icon User is offline

  • New D.I.C Head

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

Re: FTP failing to upload file

Posted 03 September 2012 - 03:51 AM

the command prompt cant seem to put the file specified in the directory:

Microsoft Windows [Version 6.1.7600]
Copyright © 2009 Microsoft Corporation. All rights reserved.

C:\Users\John.dell2>ftp
ftp> open Johndell2-PC
Connected to Johndell2-PC.
220 Serv-U FTP Server v12.1 ready...
User (Johndell2-PC:(none)): export1
331 User name okay, need password.
Password:
230 User logged in, proceed.
ftp> cd users john.dell2 documents system
250 Directory changed to /Users
ftp> cd John.dell2
250 Directory changed to /Users/John.dell2
ftp> cd documents
250 Directory changed to /Users/John.dell2/Documents
ftp> cd system
250 Directory changed to /Users/John.dell2/Documents/system
ftp> cd system.txt
550 /Users/John.dell2/Documents/system/system.txt: Is a file.
ftp> put system.txt
system.txt: File not found
ftp> put system
system: File not found
ftp>
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,640
  • Joined: 20-September 08

Re: FTP failing to upload file

Posted 03 September 2012 - 04:20 AM

You'd be better off starting with
cd /Users/John.dell2/Documents/system

Try that, then put and let me know how you get on
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2