Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-输入字节在300处有不正确的结束字节_Java_Encryption - Fatal编程技术网

Java-输入字节在300处有不正确的结束字节

Java-输入字节在300处有不正确的结束字节,java,encryption,Java,Encryption,我已经编写了一个基于JAVA的实用程序,将我的数据存储在excel加密(AES)中,并将其上载到FTP服务器和反向机制,以便下载数据并解密回纯文本 现在,加密端工作得很好,但是当我解密时。它抛出以下异常: Error while decrypting: java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 300 我试图改变编码方案,但没有任何效果 我的代码 public void

我已经编写了一个基于JAVA的实用程序,将我的数据存储在excel加密(AES)中,并将其上载到FTP服务器和反向机制,以便下载数据并解密回纯文本

现在,加密端工作得很好,但是当我解密时。它抛出以下异常:

Error while decrypting: java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 300
我试图改变编码方案,但没有任何效果

我的代码


public void downloadFileFromFTP () {


        int SFTPPORT = 22;
        String SFTPHOST = "";
        String SFTPUSER = "";
        String SFTPPASS = "";
        String SFTPWORKINGDIR = "";

        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;

        try {
            JSch jsch = new JSch();

            session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);

            session.setPassword(SFTPPASS);

            java.util.Properties config = new java.util.Properties();

            config.put("StrictHostKeyChecking", "no");

            session.setConfig(config);

            session.connect();

            channel = session.openChannel("sftp");

            channel.connect();

            channelSftp = (ChannelSftp) channel;

            channelSftp.cd(SFTPWORKINGDIR);

            byte[] buffer = new byte[1024];

            BufferedInputStream bis = new BufferedInputStream(channelSftp.get("test.csv"));

            File newFile = new File("D:\\FMS_Encryption\\dec2.csv");

            OutputStream os = new FileOutputStream(newFile);

            BufferedOutputStream bos = new BufferedOutputStream(os);

            int readCount;

            while ((readCount = bis.read(buffer)) > 0) {

                String encryptedRow = new String(buffer, StandardCharsets.UTF_8);

                String decryptedRow = decrypt( encryptedRow.trim(), "FMS" );

                bos.write(decryptedRow.getBytes(), 0, readCount);
            }

            bis.close();

            bos.close();
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }

    }

加密/解密代码


public static String encrypt(String strToEncrypt, String secret) 
    {
        try
        {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
        } 
        catch (Exception e) 
        {
            System.out.println("Error while encrypting: " + e.toString());
        }
        return null;
    }
    public static String decrypt(String strToDecrypt, String secret) 
    {
        try
        {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
        } 
        catch (Exception e) 
        {
            System.out.println("Error while decrypting: " + e.toString());
        }
        return null;
    }

现在在下载函数中,当我更改行
字符串decryptedRow=decrypt(encryptedRow,“FMS”)
to
String decryptedRow=decrypt(encryptedRow.trim(),“FMS”)它抛出:

java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at java.io.BufferedOutputStream.write(Unknown Source)
    at IbftFileEncryption.downloadFileFromFTP(IbftFileEncryption.java:426)
    at IbftFileEncryption.main(IbftFileEncryption.java:521)

您正试图以小块方式处理加密数据,但加密/解密方法需要一个完整的加密块

您正在使用AES密码使用
PKCS5Padding
填充,因此加密数据将被填充,以在加密过程中最后调用
doFinal(…)
时与AES的块大小对齐。然后,在解密过程中调用对
doFinal(…)
的最后一次调用时,将删除此填充。如果尝试在不存在此填充的情况下解密数据,则解密将失败

在您的代码中,您和up调用
doFinal(…)
的次数太多

同样,您也在块上使用base64解码,而不知道是否有完整的块


如果在解密之前将完整文件下载到一个字符串中,它可能起作用。< /P>代码中的所有空行是什么?<代码> BOS。写(DetrixDoW.GETByStEh(),0,RealCube);

为什么您认为解密文本中的字符数等于Base64编码字节中的字符数?更改为
bos.write(decryptedRow.getBytes())
我会避免使用普通的
getBytes()
,但更喜欢
getBytes(“utf-8”)
或您正在使用的任何字符编码。Crypto对每件事都非常挑剔,直到最后一点都是正确的。