java连接远程虚拟机读取远程虚拟机文件把文件DESede解密把解密的文件内容返回参要用下面代码方式解密,key是解密的密钥SecretKey secretKey =new SecretKey(ke

java连接远程虚拟机读取远程虚拟机文件把文件DESede解密把解密的文件内容返回参要用下面代码方式解密,key是解密的密钥 SecretKey secretKey =new SecretKey(key.getBytes(),"DESede"); Cipher cipher=Cipher。getInstance("DESede"); cipher.init(2,secretKey); OutputStream os=new CipherOutputStream(out,cipher)' byte[] buffer=new byte[' ']; int len; while(-1 !=(len=in.read(buffer))){ cos.write(buffer,0,len); }

在你提供的代码中,你使用了 CipherOutputStream 来将解密后的内容写入输出流。这是一种流式加密/解密的方式。你需要将输入流(即从远程虚拟机文件读取的加密内容)传递给 CipherOutputStream,它会将解密后的内容写入到输出流中。

以下是如何使用你提供的方式进行 DESede 解密的示例代码:

import com.jcraft.jsch.*;

import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class RemoteFileDecryptor {
    public static void main(String[] args) {
        String remoteHost = "your_remote_host";
        String username = "your_username";
        String password = "your_password";
        String remoteFilePath = "/path/to/remote/file";
        String keyString = "your_key_string"; // 你的密钥字符串

        try {
            JSch jsch = new JSch();

            // 创建 SSH 会话
            Session session = jsch.getSession(username, remoteHost, 22);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            // 创建 SSH 通道
            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            // 从远程文件读取加密内容
            InputStream inputStream = channelSftp.get(remoteFilePath);

            // 创建 SecretKey 对象
            byte[] keyBytes = keyString.getBytes(StandardCharsets.UTF_8);
            SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede");

            // 创建 Cipher 对象
            Cipher cipher = Cipher.getInstance("DESede");
            // 设置为解密模式,并指定密钥
            cipher.init(Cipher.DECRYPT_MODE, secretKey);

            // 创建 CipherInputStream
            CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);

            // 读取解密后的内容
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = cipherInputStream.read(buffer)) != -1) {
                baos.write(buffer, 0, bytesRead);
            }
            String decryptedContent = baos.toString(StandardCharsets.UTF_8);

            // 关闭连接
            cipherInputStream.close();
            channelSftp.disconnect();
            session.disconnect();

            // 输出解密后的内容
            System.out.println("Decrypted content: " + decryptedContent);

        } catch (JSchException | SftpException | IOException | NoSuchAlgorithmException |
                 NoSuchPaddingException | InvalidKeyException e) {
            e.printStackTrace();
        }
    }
}

在上述示例中,我们使用 CipherInputStream 将输入流(即从远程虚拟机文件读取的加密内容)传递给 Cipher 进行解密,然后将解密后的内容读取到 ByteArrayOutputStream 中,并将其转换为字符串。最后,输出解密后的内容。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值