password protected zip file issue - java

User upload the file, i need to password protect the file and then zip it put in a storage server which is different from the server where my code is running. So i use AESEncrypter to encrypt the file and jcraft.jsch.ChannelSftp to transfer the file to the server.
public ResponseEntity<ResponseWrapper> uploadFile(#RequestParam("uploads") MultipartFile file) throws Exception {
FileOutputStream fos = new FileOutputStream("outputfile.zip");
AESEncrypter aesEncrypter = new AESEncrypterBC();
aze=new AesZipFileEncrypter(fos, aesEncrypter);
aze.add(file.getOriginalFilename(), file.getInputStream(), "test123");
JSch ssh = new JSch();
Session session = ssh.getSession("username", "Servername", 22);
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword("*****");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
sftpChannel.put(file.getInputStream(), "/storedfiles/outputfile.zip");
}
File is getting transferred to the server, but when i download that transferred file and try to open it says "Errors were found opening ".." you cannot extract file.. do you want to fix the problems". Not sure why i am getting this issue, also it creates a file in local server, which line is causing that?
I tried replacing this line
aze=new AesZipFileEncrypter(fos, aesEncrypter);
with
aze=new AesZipFileEncrypter("outputfile.zip", aesEncrypter);
but dint work.

I placed the file in remote server, read that in output stream and then password protected, solved my issue.
public ResponseEntity<ResponseWrapper> uploadFile(#RequestParam("uploads") MultipartFile file) throws Exception {
JSch ssh = new JSch();
Session session = ssh.getSession("username", "Servername", 22);
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword("*****");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
OutputStream os = sftp.put("/storedfiles/outputfile.zip");
AESEncrypter aesEncrypter = new AESEncrypterBC();
aze=new AesZipFileEncrypter(os, aesEncrypter);
aze.add(file.getOriginalFilename(), file.getInputStream(), "test123");
if(aze != null) {
aze.close();
}
}

Related

Getting exception "session is down" when calling the ftp server through jsch

When I am trying to get connected with the ftp server for file uploading, I am getting exception com.jcraft.jsch.JSchException: session is down
Code is in groovy:
String SFTPHOST = "########"
int SFTPPORT = 22
String SFTPUSER = "########"
String SFTPPASS = "########"
String SFTPWORKINGDIR = "/QA/"
ChannelSftp sftp = null
Session session = null
try {
JSch jsch = new JSch()
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT)
session.setPassword(SFTPPASS)
session.setConfig("StrictHostKeyChecking", "no")
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password")
session.connect()
Channel channel = session.openChannel "sftp"
channel.connect()
sftp = channel as ChannelSftp
sftp.cd SFTPWORKINGDIR
File f = new File("Demo.csv")
sftp.put(new FileInputStream(f), f.getName())
//def fileList = sftp.ls("*")
println fileList.size()
} catch (Exception ex) {
ex.printStackTrace()
}
I got the issue...Actually JSch is not an FTP client it's an SSH client (with an included SFTP implementation). And the ftp server which i am connecting with is not a ssh server. That's why jsch is unable to connect with that ftp server. I have used apache commons ftp client and its working fine

Equivalent Scala code for Java code

I am new to scala.Any one having idea what is the equivalent code in scala for the following java code
package sampleFTP
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import com.jcraft.jsch._
object FTPTest {
def main(args: Array[String]) {
println("Hello, world!")
var ftpClient= new FTPClient();
val SFTPPASS = "xxxx";
val SFTPWORKINGDIR = "/xxxx/xxxx";
System.out.println("preparing the host information for sftp.");
val jsch = new JSch();
var session = jsch.getSession("xxxx", "xxxx", 22)
session.setPassword(SFTPPASS);
var config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Host connected.");
var channel = session.openChannel("sftp");
channel.connect();
System.out.println("sftp channel opened and connected.");
var sftpChannel = (ChannelSftp) session.openChannel("sftp");//error in this line
System.out.println("Directory:" + sftpChannel.pwd());
session.disconnect();
}
}
I am getting the following error
value session is not a member of object com.jcraft.jsch.ChannelSftp
I have Successfully implemented the secure FTP connection using jsch.How to download and list file via jsch in scala.
To cast to a different type in Scala use:
session.openChannel("sftp").asInstanceOf[ChannelSftp]

How to send password in command using ssh command

I am using Jsch library to connect with my server. After connecting i am passing command which require password to proceed further hence i am passing my password in command only but nothing happens.
Code:
JSch jsch = new JSch();
jsch.removeAllIdentity();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PubkeyAuthentication", "no");
System.out.println("Establishing Connection...");
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");`
Channel shellChannel = session.openChannel("shell");
shellChannel.connect();
((ChannelShell) shellChannel).setPty(true);
shellChannel.setInputStream(System.in);
shellChannel.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(
shellChannel.getOutputStream());
shellChannel.connect();
shellStream
.println("cd /usr/local/apache2/; ls; cd ../www; ls; git fetch origin; <mypasssword>");
shellStream.flush();
System.out.println("SFTP Channel created.");`
When i run this code git ask password to proceed further.
Note: i cannot disable password for git fetch origin.
I tried your code to access my linux box - it does log in successfully, but then fails to send any commands. I'm not sure if that's the problem you are having - but I will add my solution here, just in case.
Moved the shellStream.println(); command to its own function:
public static void sendCommand(String c) {
shellStream.print(c + "\n");
shellStream.flush();
}
Had to make shellChannel and shellStream global variables in the process.
Changed shellStream.println(); to shellStream.print("\n");, as the aforementioned refused to work.
After this line of your code:
shellStream = new PrintStream(shellChannel.getOutputStream());
Added my command sequence:
Thread.sleep(1000); // wait for it to connect
sendCommand("sudo su"); // the command I tried
Thread.sleep(1000); // not sure how long you need to wait
sendCommand("mypassword");
Thread.sleep(1000);
// etc.
By the way, you are calling shellChannel.connect(); twice in your code - I removed the last one.
Here's the final working version of your code:
import java.io.IOException;
import java.io.PrintStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class MyShell {
static String user = "daniel";
static String host = "localhost";
static int port = 22;
static String password = "mypass";
static Session session;
static Channel shellChannel;
static PrintStream shellStream;
public static void main(String[] args) throws JSchException, IOException,
InterruptedException {
JSch jsch = new JSch();
jsch.removeAllIdentity();
session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PubkeyAuthentication", "no");
System.out.println("Establishing Connection...");
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");
shellChannel = session.openChannel("shell");
shellChannel.connect();
((ChannelShell) shellChannel).setPty(true);
shellChannel.setInputStream(System.in);
shellChannel.setOutputStream(System.out);
shellStream = new PrintStream(shellChannel.getOutputStream());
Thread.sleep(1000);
sendCommand("sudo su");
Thread.sleep(1000);
sendCommand("mypass");
Thread.sleep(1000);
sendCommand("ls");
}
public static void sendCommand(String c) {
shellStream.print(c + "\n");
shellStream.flush();
}
}
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
Keyboard-interactive here mean the password must be tipped with a keyboard. SSH is quite peaky about that, even if there's ways to pass a password through the command line.
The best way would be to use a pubkey auth, but if that's not a option, try to login using only password
session.setConfig("PreferredAuthentications", "password");
Also you may just send the password using
session.setPassword("password");

JSchException: Auth fail and fingerprint

I'm trying to connect to my SFTP server from a Java script.
I'm using JSch lib for my purpose. Username, password and hostname are correct but I obtain an: Auth fail error.
I've also tried to add the following lines before session.connect(), but the problem still remains.
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
What do I have to put inside knownhosts.txt? The fingerprint of my server key?
public static void upload(ArrayList<File> a) {
try{
JSch jsch = new JSch();
jsch.setKnownHosts("knownhosts.txt");
Session session = jsch.getSession("username", "hostname", 22);
session.setPassword("mypassword");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp) channel;
channelSftp.cd("/var/www/");
for(File object: a){
channelSftp.put(new FileInputStream(object), object.getName(), channelSftp.OVERWRITE);
}
channelSftp.exit();
session.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Do you have some advices? Thanks in advance!
Does your network/SMTP server support IP6? If your client has IP6 support, later versions of Java default to IP6, but many SMTP servers are configured on IP4. See this article here for Sending email using JSP for directions on configuring your JVM to force IP4. This needs to be set on the JVM as it is instantiated.

Can we use JSch for SSH key-based communication?

I am using JSch for sftp communication, now i want to use facilitate the key-based authentication, key is loaded on client and server machine once by my network team and all later communication would be only user based for which we have loaded the key.
sftp -oPort=10022 jmark#192.18.0.246
as tjill#192.18.0.135
like this command work fine and connect to the sftp, how i can achieve this functionality programmatically.
if it is not possible using JSch, please suggest some other library. I came across Apache SSHD.
It is possible. Have a look at JSch.addIdentity(...)
This allows you to use key either as byte array or to read it from file.
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class UserAuthPubKey {
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
String user = "tjill";
String host = "192.18.0.246";
int port = 10022;
String privateKey = ".ssh/id_rsa";
jsch.addIdentity(privateKey);
System.out.println("identity added ");
Session session = jsch.getSession(user, host, port);
System.out.println("session created.");
// disabling StrictHostKeyChecking may help to make connection but makes it insecure
// see http://stackoverflow.com/questions/30178936/jsch-sftp-security-with-session-setconfigstricthostkeychecking-no
//
// java.util.Properties config = new java.util.Properties();
// config.put("StrictHostKeyChecking", "no");
// session.setConfig(config);
session.connect();
System.out.println("session connected.....");
Channel channel = session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
System.out.println("shell channel connected....");
ChannelSftp c = (ChannelSftp) channel;
String fileName = "test.txt";
c.put(fileName, "./in/");
c.exit();
System.out.println("done");
} catch (Exception e) {
System.err.println(e);
}
}
}

Categories