InputStream中的AES加密

	private static final String key = "aesEncryptionKey";
    private static final String initVector = "encryptionIntVec";
    
    /*
     * Getting a 128 bit key and iv for encryption
     */
    
    public static InputStream encriptFile(InputStream inputFile) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
        
        byte[] nonEncryptedByteArray = IOUtils.toByteArray(inputFile);
        
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec secretkey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); //Cipher instance using AES encryption algorithm
        cipher.init(Cipher.ENCRYPT_MODE, secretkey, iv);
        byte[] encryptedByteArray = cipher.doFinal(nonEncryptedByteArray);
        
        /*
         * Used the cipher library to encrypt the stream to a byte array
         */
        InputStream encryptedInputStream = new ByteArrayInputStream(encryptedByteArray);
        
        /*
         * Back to streams, but this time encrypted
         */
        
        return encryptedInputStream;
    }
    
    public static InputStream decriptFile(InputStream inputFile) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        
        byte[] encrytToDecryptByteArray = IOUtils.toByteArray(inputFile);
        
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec secretkey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretkey, iv);
        byte[] decryptedByteArray = cipher.doFinal(encrytToDecryptByteArray);
        
        /*
         * dencrypted the encrypted data
         */
        
        InputStream decryptedInputStream = new ByteArrayInputStream(decryptedByteArray);
        
        return decryptedInputStream;
    }

主要方法如下:

 		File file = new File("test.txt");
        InputStream is = new FileInputStream(file);
        
        InputStream eis = encriptFile(is);
        
        StringWriter writer = new StringWriter();
        IOUtils.copy(eis, writer, "UTF-8");
        String theString = writer.toString();
        
        System.out.print(theString);
要使用Java字节流进行AES加密,可以按照以下步骤进行: 1. 创建一个SecretKey对象,用于设置加密的密钥。可以使用KeyGenerator类来生成密钥。示例代码如下: ```java KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); // 设置密钥长度为128位 SecretKey secretKey = keyGenerator.generateKey(); ``` 2. 创建一个Cipher对象,用于进行加密操作。可以使用Cipher类和密钥来初始化Cipher对象。示例代码如下: ```java Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); ``` 3. 创建一个输入流和输出流,用于读取和写入数据。可以使用FileInputStream和FileOutputStream类来创建输入流和输出流。示例代码如下: ```java FileInputStream inputStream = new FileInputStream("input.txt"); FileOutputStream outputStream = new FileOutputStream("output.txt"); ``` 4. 创建一个缓冲区,用于存储读取的数据。可以使用byte数组作为缓冲区。示例代码如下: ```java byte[] buffer = new byte[1024]; int bytesRead; ``` 5. 使用Cipher对象对数据进行加密。可以使用update方法将数据写入缓冲区,并使用doFinal方法将加密后的数据写入输出流。示例代码如下: ```java while ((bytesRead = inputStream.read(buffer)) != -1) { byte[] encryptedBytes = cipher.update(buffer, 0, bytesRead); outputStream.write(encryptedBytes); } byte[] finalBytes = cipher.doFinal(); outputStream.write(finalBytes); ``` 6. 关闭输入流和输出流。示例代码如下: ```java inputStream.close(); outputStream.close(); ``` 这样就完成了使用Java字节流进行AES加密的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值