java.io.FileNotFoundException: <my path> (The system cannot find the file specified). Please guide -
following code throws exception:
java.io.filenotfoundexception: c:\cloud project\resource freeing attacks in cloud performance code , db\ftp\ftp\src\public.key (the system cannot find file specified)
i not able figure out why key file not found. doubt key files not getting written why system not find them.
package com.util; import java.net.*; import java.io.*; import java.security.keyfactory; import java.security.keypair; import java.security.keypairgenerator; import java.security.nosuchalgorithmexception; import java.security.privatekey; import java.security.publickey; import java.security.spec.invalidkeyspecexception; import java.security.spec.pkcs8encodedkeyspec; import java.security.spec.x509encodedkeyspec; import javax.crypto.cipher; import javax.crypto.cipherinputstream; import javax.crypto.cipheroutputstream; import javax.crypto.spec.secretkeyspec; public class simpleftpclient { public static string path = "c:/cloud project/resource freeing attacks in cloud performance code , db/ftp/ftp/src/"; private urlconnection m_client; private string host; private string user; private string password; private string remotefile; private string ermesg; private string succmesg; public simpleftpclient() { } public void sethost(string host) { this.host = host; } public void setuser(string user) { this.user = user; } public void setpassword(string p) { this.password = p; } public void setremotefile(string d) { this.remotefile = d; } public synchronized string getlastsuccessmessage() { if (succmesg == null) { return ""; } return succmesg; } public synchronized string getlasterrormessage() { if (ermesg == null) { return ""; } return ermesg; } public synchronized boolean uploadfile(inputstream is) { try { bufferedinputstream bis = new bufferedinputstream(is); outputstream os = m_client.getoutputstream(); bufferedoutputstream bos = new bufferedoutputstream(os); string algo = "rsa/ecb/pkcs5padding"; simpleftpclient sftpcx = new simpleftpclient(); keypair kpr = sftpcx.loadkeypair(path, algo); publickey pubkey = kpr.getpublic(); cipher encrypt = cipher.getinstance(algo); encrypt.init(cipher.encrypt_mode, pubkey); cipheroutputstream cout=new cipheroutputstream(bos, encrypt); byte[] buffer = new byte[1024]; int readcount; while ((readcount = bis.read(buffer)) != -1) { cout.write(buffer, 0, readcount);//cout instead of bos } cout.close(); bis.close(); bos.flush(); bos.close(); this.succmesg = "uploaded!"; return true; } catch (exception ex) { ex.printstacktrace(); stringwriter sw0 = new stringwriter(); printwriter p0 = new printwriter(sw0, true); ex.printstacktrace(p0); ermesg = sw0.getbuffer().tostring(); return false; } } public synchronized boolean downloadfile(string localfilename) { try { inputstream = m_client.getinputstream(); bufferedinputstream bis = new bufferedinputstream(is); system.out.println(">>>>>>>>>>>"+localfilename); outputstream os = new fileoutputstream(localfilename); bufferedoutputstream bos = new bufferedoutputstream(os); string algo = "rsa/ecb/pkcs5padding"; simpleftpclient sftpc = new simpleftpclient(); keypair kpr = sftpc.loadkeypair(path, algo); privatekey prvkey = kpr.getprivate(); cipher decrypt = cipher.getinstance(algo); decrypt.init(cipher.decrypt_mode, prvkey); cipherinputstream cin=new cipherinputstream(bis, decrypt); byte[] buffer = new byte[1024]; int readcount; while ((readcount = cin.read(buffer)) != -1) { bos.write(buffer, 0, readcount); } cin.close(); bis.close(); bos.flush(); bos.close(); this.succmesg = "downloaded!"; return true; } catch (exception ex) { ex.printstacktrace(); stringwriter sw0 = new stringwriter(); printwriter p0 = new printwriter(sw0, true); ex.printstacktrace(p0); ermesg = sw0.getbuffer().tostring(); return false; } } public synchronized boolean connect() { try { url url = new url("ftp://" + user + ":" + password + "@" + host + "/" + remotefile + ";type=i"); m_client = url.openconnection(); system.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.."+"ftp://" + user + ":" + password + "@" + host + "/" + remotefile + ";type=i"); return true; } catch (exception ex) { ex.printstacktrace(); stringwriter sw0 = new stringwriter(); printwriter p0 = new printwriter(sw0, true); ex.printstacktrace(p0); ermesg = sw0.getbuffer().tostring(); return false; } } public static void main(string arg[]) throws nosuchalgorithmexception, ioexception, invalidkeyspecexception { simpleftpclient f = new simpleftpclient(); f.sethost("ftp.drivehq.com"); f.setuser("sabari06"); f.setpassword("sabari06"); f.setremotefile("c.txt"); boolean connected = f.connect(); try{ keypairgenerator keygen = keypairgenerator.getinstance("rsa"); keygen.initialize(1024); keypair generatedkeypair = keygen.genkeypair(); system.out.println("generated key pair"); f.dumpkeypair(generatedkeypair); f.savekeypair(path, generatedkeypair); keypair loadedkeypair = f.loadkeypair(path, "rsa"); system.out.println("loaded key pair"); f.dumpkeypair(loadedkeypair); }catch (exception e) { e.printstacktrace(); return; } } private void dumpkeypair(keypair keypair) { publickey pub = keypair.getpublic(); system.out.println("public key: " + gethexstring(pub.getencoded())); privatekey priv = keypair.getprivate(); system.out.println("private key: " + gethexstring(priv.getencoded())); } private string gethexstring(byte[] b) { string result = ""; (int = 0; < b.length; i++) { result += integer.tostring((b[i] & 0xff) + 0x100, 16).substring(1); } return result; } public void savekeypair(string path, keypair keypair) throws ioexception { privatekey privatekey = keypair.getprivate(); publickey publickey = keypair.getpublic(); x509encodedkeyspec x509encodedkeyspec = new x509encodedkeyspec( publickey.getencoded()); fileoutputstream fos = new fileoutputstream(path + "\\public.key"); fos.write(x509encodedkeyspec.getencoded()); fos.close(); pkcs8encodedkeyspec pkcs8encodedkeyspec = new pkcs8encodedkeyspec( privatekey.getencoded()); fos = new fileoutputstream(path + "\\private.key"); fos.write(pkcs8encodedkeyspec.getencoded()); fos.close(); } public keypair loadkeypair(string path, string algorithm) throws ioexception, nosuchalgorithmexception, invalidkeyspecexception { file filepublickey = new file(path + "/public.key"); fileinputstream fis = new fileinputstream(path + "/public.key"); byte[] encodedpublickey = new byte[(int) filepublickey.length()]; fis.read(encodedpublickey); fis.close(); file fileprivatekey = new file(path + "/private.key"); fis = new fileinputstream(path + "/private.key"); byte[] encodedprivatekey = new byte[(int) fileprivatekey.length()]; fis.read(encodedprivatekey); fis.close(); keyfactory keyfactory = keyfactory.getinstance(algorithm); x509encodedkeyspec publickeyspec = new x509encodedkeyspec( encodedpublickey); publickey publickey = keyfactory.generatepublic(publickeyspec); pkcs8encodedkeyspec privatekeyspec = new pkcs8encodedkeyspec( encodedprivatekey); privatekey privatekey = keyfactory.generateprivate(privatekeyspec); return new keypair(publickey, privatekey); } }
open command prompt or ide administrator. don't have appropriate writing rights i've judged. right click on command prompt or ide , choose run administrator , try executing program again.
Comments
Post a Comment