java代码自动输入密码_Java输入流密码

我正在做一个简单的项目,在这个项目中,客户机用JCE(DES)加密一个文件,并用套接字将其发送到服务器。服务器接收并解密它,直到服务器必须解密文件的部分,它才能正常工作。实际上,cipherinputstream应该返回解密的纯文本结果为空,但是我在函数中使用的fileinputstream是正常的,所以我不知道可能是什么问题。

客户:

import jdk.internal.util.xml.impl.Input;

import javax.crypto.*;

import javax.crypto.spec.DESKeySpec;

import java.io.*;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.spec.InvalidKeySpecException;

import java.util.Scanner;

import java.net.*;

import java.nio.file.Files;

import java.nio.file.Path;

public class Main {

static private int porta = 81;

static private String hostname = "localhost";

public static void main(String[] args) {

OutputStream outputStream = null;

Socket socket = null;

boolean bIsConnected = false;

System.out.println("Inserisci il percorso del file che vuoi criptare");

Scanner in = new Scanner(System.in);

String path = in.nextLine(); //percorso da usare

System.out.println("Inserisci la chiave di criptazione");

String key = in.nextLine(); //Chiede la chiave di decriptazione (8 caratteri)

File plaintext = new File(path);

File encrypted = new File("Criptato.txt"); //salva il file nella cartella locale del progetto

try {

Encrypt(key, plaintext, encrypted);

System.out.println("Crittazione completata");

} catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | IOException e) {

e.printStackTrace();

}

while (!bIsConnected) { //crittato il file aspetta che l'utente si colleghi al portale del server per mandare il file

try {

socket = new Socket(hostname, porta);

bIsConnected = true;

} catch (SocketException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

byte[] bytes = new byte[16 * 1024];

try {

InputStream inp = new FileInputStream(encrypted);//converte il file in uno stream

OutputStream outp = socket.getOutputStream();

int count;

while ((count = inp.read(bytes)) > 0) {

outp.write(bytes, 0, count); //scrive sulla socket del server il contenuto del file byte per byte

}

} //si collega all'output stream della socket

catch (IOException e) {

}

}

private static void write(InputStream in, OutputStream out) throws IOException {

byte[] buffer = new byte[64];

int numOfBytesRead;

while ((numOfBytesRead = in.read(buffer)) != -1) {

out.write(buffer, 0, numOfBytesRead);

}

out.close();

in.close();

}

public static void Encrypt(String key, File in, File out)

throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IOException {

FileInputStream fis = new FileInputStream(in);

FileOutputStream fos = new FileOutputStream(out);

DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());

SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");

SecretKey secretKey = skf.generateSecret(desKeySpec);

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, secretKey, SecureRandom.getInstance("SHA1PRNG"));

CipherInputStream cis = new CipherInputStream(fis, cipher);

write(cis, fos);

}

}

服务器:

import javax.crypto.*;

import javax.crypto.spec.DESKeySpec;

import java.io.*;

import java.nio.file.Paths;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.spec.InvalidKeySpecException;

import java.util.Scanner;

import java.net.*;

import java.nio.file.Files;

public class Main {

static private int portNumber = 81;

public static void main(String[] args) {

File criptato = new File("CriptatoServer.txt");

File decriptato = new File("Decrittato.txt");

System.out.println("Server running");

// Listening in entrata

ServerSocket socketServer;

try {

socketServer = new ServerSocket(portNumber);

} catch (IOException e) {

System.out.println("Impossibile ascoltare sulla porta.");

e.printStackTrace();

return;

}

while (true) {

Socket socket = null;

try {

socket = socketServer.accept();

} catch (IOException e) {

System.out.println("Impossibile accettare client.");

e.printStackTrace();

return;

}

System.out.println("Ricevuto client socket!");

InputStream in = null;

OutputStream out = null;

try {

in = socket.getInputStream(); //prende ciò che arriva dal client

} catch (IOException ex) {

System.out.println("Can't get socket input stream. ");

}

try {

out = new FileOutputStream(criptato); //scrive nel file criptato (sta copiando dal client)

} catch (FileNotFoundException ex) {

System.out.println("File not found. ");

}

byte[] bytes = new byte[16 * 1024];

int count;

try {

while ((count = in.read(bytes)) > 0) {

out.write(bytes, 0, count);

}

}catch(IOException e){System.out.println("AHIAIAI");}

System.out.println("Inserisci la chiave per decriptare il file e leggere il messaggio");

Scanner ins = new Scanner(System.in);

String key = ins.nextLine(); //chiave

try {

Decrypt(key, criptato, decriptato);

System.out.println("Decrittazione completata");

} catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | IOException e) {

e.printStackTrace();

}

}

}

private static void write(InputStream in, OutputStream out) throws IOException {

byte[] buffer = new byte[64];

int numOfBytesRead;

while ((numOfBytesRead = in.read(buffer)) != -1) {

out.write(buffer, 0, numOfBytesRead);

}

out.close();

in.close();

}

public static void Decrypt(String key, File in, File out)

throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IOException {

FileInputStream fis = new FileInputStream(in); //seems ok

FileOutputStream fos = new FileOutputStream(out);

DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());

SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");

SecretKey secretKey = skf.generateSecret(desKeySpec);

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

cipher.init(Cipher.DECRYPT_MODE, secretKey, SecureRandom.getInstance("SHA1PRNG"));

CipherInputStream cis = new CipherInputStream(fis, cipher); //returns null

write(cis, fos);

}

public static String getFileContent(

FileInputStream fis,

String encoding ) throws IOException

{

try( BufferedReader br =

new BufferedReader( new InputStreamReader(fis, encoding )))

{

StringBuilder sb = new StringBuilder();

String line;

while(( line = br.readLine()) != null ) {

sb.append( line );

sb.append( '\n' );

}

return sb.toString();

}

}

}

我知道有点乱,但我还在努力。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java自动登录是指在网站或应用程序中使用Java语言编写代码,实现用户账户的自动登录功能。以下是一个简单的Java自动登录案例: 假设有一个网站,网站在登录页面上有用户名和密码输入框,并有一个登录按钮,当用户输入正确的用户名和密码后点击登录按钮,网站会验证用户的身份,并跳转到用户的个人主页。现在我们使用Java语言编写代码,实现自动登录功能。 首先,我们需要导入需要的Java类库,如`java.net.URL`、`java.net.HttpURLConnection`等。然后创建一个`URL`对象,传入网站登录页面的URL地址。 接下来,我们创建一个`HttpURLConnection`对象,并使用`openConnection()`方法建立与URL的连接。 然后,我们设置连接的一些属性,如请求方法为POST、设置请求头部(Content-Type为application/x-www-form-urlencoded等)等。 接着,我们构建一个字符串,将用户名和密码以POST请求的格式(如username=value&password=value)放入请求体中,并将请求体写入到连接的输出中。 然后,我们调用`getInputStream()`方法获取连接的输入,并读取服务器的响应。如果响应中包含了登录成功的标志(如用户个人主页的URL),则表示登录成功。 最后,我们关闭连接。 以上就是一个简单的Java自动登录案例。需要注意的是,实际中还可能需要处理一些异常、添加验证码处理、保存登录状态等,具体实现方法会因网站的不同而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值