简体   繁体   English

在java中使用AES/CBC/PKCS5Padding加密后的文件大小是多少

[英]What is the file size after encryption using AES/CBC/PKCS5Padding in java

I tried to use encryption and decryption when sending files using socket.我在使用套接字发送文件时尝试使用加密和解密。 I use the AES/CBC/PKCS5Padding algorithm and first write the IV and then the file to the stream.我使用AES/CBC/PKCS5Padding算法并首先写入IV ,然后将文件写入流。

The problem is that the loop does not end when the file is receiving.问题是接收文件时循环没有结束。

I think this is due to the file size and the encrypted file seems to be smaller than the original file, While I give the size of the original file to the receiver.我认为这是由于文件大小和加密文件似乎比原始文件小,而我将原始文件的大小提供给接收者。 If this hypothesis is correct, is there a way to calculate the size of the encrypted file?如果这个假设是正确的,有没有办法计算加密文件的大小?

File sender文件发件人

            SecretKey keySpec = new SecretKeySpec(key, "AES");
            byte[] iv = AES.randomNonce(16);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);

            outputStream = socket.getOutputStream();
            outputStream.write(iv);
            outputStream.flush();
            inputStream = context.getContentResolver().openInputStream(message.getUri());
            cipherOutputStream = new CipherOutputStream(outputStream, cipher);

            long size = message.getSize();
            long written = 0;
            byte[] buffer = new byte[8192];
            int count;
            int percent = 0;
            while (!isStopped && (count = inputStream.read(buffer)) > 0) {
                cipherOutputStream.write(buffer, 0, count);
                written += count;
                int p = (int) (((float) written / (float) size) * 100);
                if (percent != p) {
                    percent = p;
                    if (onProgressListener != null) {
                        onProgressListener.onProgress(percent);
                    }
                }
            }
            cipherOutputStream.flush();
            if (onProgressListener != null) {
                onProgressListener.onEnd(null);
            }

File receiver文件接收器

            inputStream = socket.getInputStream();
            fileOutputStream = new FileOutputStream(file);
            byte[] iv = new byte[16];
            inputStream.read(iv);

            SecretKey keySpec = new SecretKeySpec(key, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);

            cipherInputStream = new CipherInputStream(inputStream, cipher);

            long size = message.getSize();
            long read = size;
            byte[] buffer = new byte[8192];
            int count;
            int percent = 0;
            while (!isStopped && read > 0 && (count = cipherInputStream.read(buffer, 0, (int) Math.min(buffer.length, read))) != -1) {
                fileOutputStream.write(buffer, 0, count);
                read -= count;
                int p = (int) (((float) read / (float) size) * 100);
                if (percent != p) {
                    percent = p;
                    if (onProgressListener != null) {
                        onProgressListener.onProgress(100 - percent);
                    }
                }
            }
            if (onProgressListener != null) {
                onProgressListener.onEnd(Uri.fromFile(file));
            }

I tried to use encryption and decryption when sending files using socket.使用套接字发送文件时,我尝试使用加密和解密。 I use the AES/CBC/PKCS5Padding algorithm and first write the IV and then the file to the stream.我使用AES/CBC/PKCS5Padding算法,首先将IV写入文件,然后将文件写入流。

The problem is that the loop does not end when the file is receiving.问题在于,接收文件时循环不会结束。

I think this is due to the file size and the encrypted file seems to be smaller than the original file, While I give the size of the original file to the receiver.我认为这是由于文件大小,加密的文件似乎比原始文件小,而我将原始文件的大小提供给接收器。 If this hypothesis is correct, is there a way to calculate the size of the encrypted file?如果此假设正确,是否可以计算加密文件的大小?

File sender文件发件人

            SecretKey keySpec = new SecretKeySpec(key, "AES");
            byte[] iv = AES.randomNonce(16);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);

            outputStream = socket.getOutputStream();
            outputStream.write(iv);
            outputStream.flush();
            inputStream = context.getContentResolver().openInputStream(message.getUri());
            cipherOutputStream = new CipherOutputStream(outputStream, cipher);

            long size = message.getSize();
            long written = 0;
            byte[] buffer = new byte[8192];
            int count;
            int percent = 0;
            while (!isStopped && (count = inputStream.read(buffer)) > 0) {
                cipherOutputStream.write(buffer, 0, count);
                written += count;
                int p = (int) (((float) written / (float) size) * 100);
                if (percent != p) {
                    percent = p;
                    if (onProgressListener != null) {
                        onProgressListener.onProgress(percent);
                    }
                }
            }
            cipherOutputStream.flush();
            if (onProgressListener != null) {
                onProgressListener.onEnd(null);
            }

File receiver文件接收器

            inputStream = socket.getInputStream();
            fileOutputStream = new FileOutputStream(file);
            byte[] iv = new byte[16];
            inputStream.read(iv);

            SecretKey keySpec = new SecretKeySpec(key, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);

            cipherInputStream = new CipherInputStream(inputStream, cipher);

            long size = message.getSize();
            long read = size;
            byte[] buffer = new byte[8192];
            int count;
            int percent = 0;
            while (!isStopped && read > 0 && (count = cipherInputStream.read(buffer, 0, (int) Math.min(buffer.length, read))) != -1) {
                fileOutputStream.write(buffer, 0, count);
                read -= count;
                int p = (int) (((float) read / (float) size) * 100);
                if (percent != p) {
                    percent = p;
                    if (onProgressListener != null) {
                        onProgressListener.onProgress(100 - percent);
                    }
                }
            }
            if (onProgressListener != null) {
                onProgressListener.onEnd(Uri.fromFile(file));
            }

I tried to use encryption and decryption when sending files using socket.使用套接字发送文件时,我尝试使用加密和解密。 I use the AES/CBC/PKCS5Padding algorithm and first write the IV and then the file to the stream.我使用AES/CBC/PKCS5Padding算法,首先将IV写入文件,然后将文件写入流。

The problem is that the loop does not end when the file is receiving.问题在于,接收文件时循环不会结束。

I think this is due to the file size and the encrypted file seems to be smaller than the original file, While I give the size of the original file to the receiver.我认为这是由于文件大小,加密的文件似乎比原始文件小,而我将原始文件的大小提供给接收器。 If this hypothesis is correct, is there a way to calculate the size of the encrypted file?如果此假设正确,是否可以计算加密文件的大小?

File sender文件发件人

            SecretKey keySpec = new SecretKeySpec(key, "AES");
            byte[] iv = AES.randomNonce(16);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);

            outputStream = socket.getOutputStream();
            outputStream.write(iv);
            outputStream.flush();
            inputStream = context.getContentResolver().openInputStream(message.getUri());
            cipherOutputStream = new CipherOutputStream(outputStream, cipher);

            long size = message.getSize();
            long written = 0;
            byte[] buffer = new byte[8192];
            int count;
            int percent = 0;
            while (!isStopped && (count = inputStream.read(buffer)) > 0) {
                cipherOutputStream.write(buffer, 0, count);
                written += count;
                int p = (int) (((float) written / (float) size) * 100);
                if (percent != p) {
                    percent = p;
                    if (onProgressListener != null) {
                        onProgressListener.onProgress(percent);
                    }
                }
            }
            cipherOutputStream.flush();
            if (onProgressListener != null) {
                onProgressListener.onEnd(null);
            }

File receiver文件接收器

            inputStream = socket.getInputStream();
            fileOutputStream = new FileOutputStream(file);
            byte[] iv = new byte[16];
            inputStream.read(iv);

            SecretKey keySpec = new SecretKeySpec(key, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);

            cipherInputStream = new CipherInputStream(inputStream, cipher);

            long size = message.getSize();
            long read = size;
            byte[] buffer = new byte[8192];
            int count;
            int percent = 0;
            while (!isStopped && read > 0 && (count = cipherInputStream.read(buffer, 0, (int) Math.min(buffer.length, read))) != -1) {
                fileOutputStream.write(buffer, 0, count);
                read -= count;
                int p = (int) (((float) read / (float) size) * 100);
                if (percent != p) {
                    percent = p;
                    if (onProgressListener != null) {
                        onProgressListener.onProgress(100 - percent);
                    }
                }
            }
            if (onProgressListener != null) {
                onProgressListener.onEnd(Uri.fromFile(file));
            }

Check for this exception.检查此异常。 java.io.IOException: javax.crypto.BadPaddingException java.io.IOException: javax.crypto.BadPaddingException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PHP中复制Java的AES / CBC / PKCS5Padding加密 - Replicating Java's AES/CBC/PKCS5Padding encryption in PHP Android AES / CBC / PKCS5Padding加密 - Android AES/CBC/PKCS5Padding encryption AES/CBC/PKCS5Padding 与 AES/CBC/PKCS7Padding 与 256 密钥大小性能 java - AES/CBC/PKCS5Padding vs AES/CBC/PKCS7Padding with 256 key size performance java 客户端Java加密和服务器解密,使用PBKDF2WithHmacSHA1和AES / CBC / PKCS5Padding - Java encryption by client and decryption by server, using PBKDF2WithHmacSHA1 and AES/CBC/PKCS5Padding 什么是php中的AES / CBC / PKCS5Padding java等效项? - What is AES/CBC/PKCS5Padding java equivalent in php? AES CBC PKCS5将Java填充为Ruby - AES CBC PKCS5Padding Java to Ruby 使用AES / CBC / PKCS5Padding正确解密文件-BadPaddingException - Properly decrypting a file using AES/CBC/PKCS5Padding - BadPaddingException Java AES / CBC / PKCS5Padding流加密性能与无加密相比 - Java AES/CBC/PKCS5Padding stream encryption performance compared to no encryption AES/CBC/PKCS5Padding 加密与固定 IV(或没有) - AES/CBC/PKCS5Padding encryption with fixed IV (or without one) AES / CBC / PKCS5在Android和Java中都完成了填充加密。 加密的文字不匹配 - AES/CBC/PKCS5Padding encryption done both in android and java. Encrypted text does not match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM