如何使用从密码派生的密钥正确加密和解​​密文件

2024-02-16

我正在尝试找出使用“PBEWithHmacSHA256AndAES_256”标准加密和解密文件的正确过程。

据我了解,看这个示例代码 https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/CryptoSpec.html#PBEEx来自甲骨文。

我发现需要盐,以及迭代计数和哈希标准。

所以我有我的主要方法,传递到加密方法:

  1. 用户定义的密码new String(key).toCharArray()作为字节数组(使用此方法进行其他加密运行)
  2. 安全随机 IVinitVector作为字节数组
  3. 纯文本文件inputFile作为字符串
  4. 要创建的密文文件的名称outputFile作为字符串

我已按照代码示例编写了我认为正确的加密方法。我通过将盐和 IV 附加到密文来存储用于解密的盐和 IV。

private static void encrypt(byte[] key, byte[] initVector, String inputFile, String outputFile) //exceptions for throws... {
    //Initalisation for encryption
    Cipher cipher;

    byte[] salt = new byte[16];

        SecureRandom rand = new SecureRandom();

        // Salt randomly generated with base64
        rand.nextBytes(salt);
        System.out.println("my salt should be" + Base64.getEncoder().encodeToString(salt));
        salt = Base64.getEncoder().encode(salt);

        // Iteration count
        int count = 1000;

        IvParameterSpec iv = new IvParameterSpec(initVector);
        
        // Create PBE parameter set
        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(Base64.getDecoder().decode(salt), count, iv);                
        // Convert pass into SecretKey object
        PBEKeySpec pbeKeySpec = new PBEKeySpec(new String(key).toCharArray());
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_256");
        SecretKey pbeKey;
        try {
            pbeKey = keyFac.generateSecret(pbeKeySpec);
        } catch (InvalidKeySpecException e) {
            System.out.println("Sorry, the password specified cannot be used as a secret key");
            System.out.println("Please check that your password uses valid characters");
            return;
        }

        // Create PBE Cipher
        cipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_256");

        // Initialize PBE Cipher with key and parameters
        cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
    }

    //File error checking and file handling (i.e. generating file paths)...

    System.out.println("Secret key is " + Base64.getEncoder().encodeToString(key));
    System.out.println("IV is " + Base64.getEncoder().encodeToString(initVector));

    //Special file reading and writing with 'Cipher Stream'
    try (InputStream fin = FileEncryptor.class.getResourceAsStream(loadFile.getName());
            OutputStream fout = Files.newOutputStream(saveFile);

            CipherOutputStream cipherOut = new CipherOutputStream(fout, cipher) {
            }) {
        final byte[] bytes = new byte[1024];
        for(int length=fin.read(bytes); length!=-1; length = fin.read(bytes)){

                fout.write(initVector);
                fout.write(salt);

            cipherOut.write(bytes, 0, length);

        }
    } catch (IOException e) {
        System.out.println("Something went wrong with reading and writing these files!");
        System.out.println("Please check you have the latest version of this program");
        System.out.println("Contact your IT admin to make sure you have sufficient privileges");
    }
    System.out.println("SUCCESS! Encryption finished, saved at specified location");
}

然后我还有我的主要方法,传递到解密方法:

  1. 用户定义的密码String inputKEY作为字符串(也使用此方法进行其他解密运行)

  2. 一个字符串inputIV,已作为 null 传入,因为未用于 PBE。

  3. 密文文件inputFile作为字符串

  4. 要创建的 Revealplaintext 文件的名称outputFile作为字符串

    私有静态无效解密(字符串输入KEY,字符串输入IV,字符串输入文件,字符串输出文件){ 密码 密码 = null;

     //File error checking and file handling (i.e. generating file paths)...
    
     InputStream encryptedData = Files.newInputStream(loadFilePath);
    
    
         PBEKeySpec pbeKeySpec = new PBEKeySpec(inputKEY.toCharArray());
         SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_256");
         SecretKey pbeKey = null;
         try {
             pbeKey = keyFac.generateSecret(pbeKeySpec);
         } catch (InvalidKeySpecException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         byte[] initVect = new byte[16];
         encryptedData.read(initVect);
    
         IvParameterSpec iv = new IvParameterSpec(Base64.getDecoder().decode(initVect);
    
         byte[] salt = new byte[16];
         encryptedData.read(salt);
    
         PBEParameterSpec pbeParamSpec = new PBEParameterSpec(Base64.getDecoder().decode(salt), 1000, iv);  
         cipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_256");
    
         System.out.println("my salt should be" + Base64.getEncoder().encodeToString(Base64.getDecoder().decode(salt)));
    
         cipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); 
    
     try (CipherInputStream decryptStream = new CipherInputStream(encryptedData, cipher);    
             OutputStream decryptedOut = Files.newOutputStream(saveFile)){
         final byte[] bytes = new byte[1024];
         for(int length=decryptStream.read(bytes); length!=-1; length = decryptStream.read(bytes)){
             decryptedOut.write(bytes, 0, length);
         }
     } catch (IOException e) { //This is caught when decryption is run
         System.out.println("Something went wrong with reading and writing these files!");
         System.out.println("Please check you have the latest version of this program");
         System.out.println("Contact your IT admin to make sure you have sufficient privileges");
     }
    
     System.out.println("SUCESS! Decryption finished, saved at specified location");
    

    }

我认为我对 PBE 的理解有些不对劲,因此我实现它的方式可能是错误的。谁能指出什么似乎是错误的?


主要问题是:

  • IV 和 Salt 不得写在for loop.
  • IV 存储在encrypt不是 Base64 编码的,但是是 Base64 解码的decrypt.
  • 16字节salt存储在encrypt(不必要)Base64 编码,即存储 24 个字节。在decrypt但是只加载了 16 个字节。

Also:

  • 编码/解码时,有时没有指定编码,因此使用默认编码。
  • encrypt and decrypt对 key 和 IV 使用不同的参数类型。
  • 代码中存在许多复制/粘贴错误。

注意:与您的代码相比,linked https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/CryptoSpec.html#PBEEx除了密钥之外,代码还确定来自密码和盐的 IV。 在您的代码中,IV 已通过。因此,您必须确保密钥/IV 对只能使用一次。通常每次加密都会生成一个随机 IV。

在以下代码中(基于您的代码,但为了简单起见,没有异常处理)这些问题已得到修复/优化。此外,该代码适用FileInputStream and FileOutputStream而不是你的课程(但这不是必需的):

private static void encrypt(String key, byte[] initVector, String inputFile, String outputFile) throws Exception {

    // Key
    PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_256");
    SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

    // IV
    IvParameterSpec iv = new IvParameterSpec(initVector);

    // Salt
    SecureRandom rand = new SecureRandom();
    byte[] salt = new byte[16];
    rand.nextBytes(salt);

    // ParameterSpec
    int count = 1000; // should be larger, see Michael Fehr's comment
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count, iv);

    // Cipher
    Cipher cipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_256");
    cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

    try (FileInputStream fin = new FileInputStream(inputFile);
         FileOutputStream fout = new FileOutputStream(outputFile);
         CipherOutputStream cipherOut = new CipherOutputStream(fout, cipher)) {
    
        // Write IV, Salt
        fout.write(initVector);
        fout.write(salt);
    
        // Encrypt
        final byte[] bytes = new byte[1024];
        for (int length = fin.read(bytes); length != -1; length = fin.read(bytes)) {
            cipherOut.write(bytes, 0, length);
        }
    } 
}
private static void decrypt(String key, byte[] initVect, String inputFile, String outputFile) throws Exception {

    try (FileInputStream encryptedData = new FileInputStream(inputFile);
         FileOutputStream decryptedOut = new FileOutputStream(outputFile)) {

        // Key
        PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_256");
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

        // Read IV
        if (initVect == null) {
            initVect = encryptedData.readNBytes(16);
        }
        IvParameterSpec iv = new IvParameterSpec(initVect);

        // Read salt
        byte[] salt = encryptedData.readNBytes(16);

        // ParameterSpec
        int count = 1000;
        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count, iv);

        // Cipher
        Cipher cipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_256");
        cipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

        try (CipherInputStream decryptStream = new CipherInputStream(encryptedData, cipher)) {
        
            // Decrypt
            final byte[] bytes = new byte[1024];
            for (int length = decryptStream.read(bytes); length != -1; length = decryptStream.read(bytes)) {
                decryptedOut.write(bytes, 0, length);
            }
        } 
    }
}

EDIT- 关于盐和 IV 的读取decrypt:
As GPI他们在评论中指出,FileInputStream.read(byte[] b) https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/io/FileInputStream.html#read(byte%5B%5D)一般读作b.length字节,但这是不保证。更稳健的是确定读取数据的长度并循环调用该方法,直到数据完成。另一种选择是使用InputStream.readNBytes​(int len) https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/io/InputStream.html#readNBytes(int),即保证读书lenbytes(除非遇到流末尾或抛出异常),如Zabuzard已建议。在代码中,现在使用后者,即read被替换为readNBytes​.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用从密码派生的密钥正确加密和解​​密文件 的相关文章

  • 在内存中使用 byte[] 创建 zip 文件。 Zip 文件总是损坏

    我创建的 zip 文件有问题 我正在使用 Java 7 我尝试从字节数组创建一个 zip 文件 其中包含两个或多个 Excel 文件 应用程序始终完成 没有任何异常 所以 我以为一切都好 当我尝试打开 zip 文件后 Windows 7 出
  • 为什么 JTables 使 TableModel 在呈现时不可序列化?

    所以最近我正在开发一个工具 供我们配置某些应用程序 它不需要是什么真正令人敬畏的东西 只是一个具有一些 SQL 脚本生成功能并创建几个 XML 文件的基本工具 在此期间 我使用自己的 AbstractTableModel 实现创建了一系列
  • .properties 中的通配符

    是否存在任何方法 我可以将通配符添加到属性文件中 并且具有所有含义 例如a b c d lalalala 或为所有以结尾的内容设置一个正则表达式a b c anything 普通的 Java 属性文件无法处理这个问题 不 请记住 它实际上是
  • 没有 Spring 的自定义 Prometheus 指标

    我需要为 Web 应用程序提供自定义指标 问题是我不能使用 Spring 但我必须使用 jax rs 端点 要求非常简单 想象一下 您有一个包含键值对的映射 其中键是指标名称 值是一个简单的整数 它是一个计数器 代码会是这样的 public
  • 帮助将图像从 Servlet 获取到 JSP 页面 [重复]

    这个问题在这里已经有答案了 我目前必须生成一个显示字符串文本的图像 我需要在 Servlet 上制作此图像 然后以某种方式将图像传递到 JSP 页面 以便它可以显示它 我试图避免保存图像 而是以某种方式将图像流式传输到 JSP 自从我开始寻
  • jdbc mysql loginTimeout 不起作用

    有人可以解释一下为什么下面的程序在 3 秒后超时 因为我将其设置为在 3 秒后超时 12秒 我特意关闭了mysql服务器来测试mysql服务器无法访问的这种场景 import java sql Connection import java
  • 如何在用户输入数据后重新运行java代码

    嘿 我有一个基本的java 应用程序 显示人们是成年人还是青少年等 我从java开始 在用户输入年龄和字符串后我找不到如何制作它它们被归类为 我希望它重新运行整个过程 以便其他人可以尝试 的节目 我一直在考虑做一个循环 但这对我来说没有用
  • tomcat 中受密码保护的应用程序

    我正在使用 JSP Servlet 开发一个Web应用程序 并且我使用了Tomcat 7 0 33 as a web container 所以我的要求是tomcat中的每个应用程序都会password像受保护的manager applica
  • Mac 应用程序商店 - 尝试让加密发挥作用。 。 。我缺少什么?

    我正在尝试使用 Alan Quartermain 的解决方案 如该问题所链接 Mac App Store 收据验证码 https stackoverflow com questions 4261348 mac app store recei
  • Java 和 Python 可以在同一个应用程序中共存吗?

    我需要一个 Java 实例直接从 Python 实例数据存储中获取数据 我不知道这是否可能 数据存储是否透明 唯一 或者每个实例 如果它们确实可以共存 都有其单独的数据存储 总结一下 Java 应用程序如何从 Python 应用程序的数据存
  • 尝试将 Web 服务部署到 TomEE 时出现“找不到...的 appInfo”

    我有一个非常简单的项目 用于培训目的 它是一个 RESTful Web 服务 我使用 js css 和 html 创建了一个客户端 我正在尝试将该服务部署到 TomEE 这是我尝试部署时遇到的错误 我在这里做错了什么 刚刚遇到这个问题 我曾
  • logcat 中 mSecurityInputMethodService 为 null

    我写了一点android应显示智能手机当前位置 最后已知位置 的应用程序 尽管我复制了示例代码 并尝试了其他几种解决方案 但似乎每次都有相同的错误 我的应用程序由一个按钮组成 按下按钮应该log经度和纬度 但仅对数 mSecurityInp
  • 获取文件的总大小(以字节为单位)[重复]

    这个问题在这里已经有答案了 可能的重复 java 高效获取文件大小 https stackoverflow com questions 116574 java get file size efficiently 我有一个名为 filenam
  • 关键字“table”附近的语法不正确,无法提取结果集

    我使用 SQL Server 创建了一个项目 其中包含以下文件 UserDAO java public class UserDAO private static SessionFactory sessionFactory static se
  • 找不到符号 NOTIFICATION_SERVICE?

    package com test app import android app Notification import android app NotificationManager import android app PendingIn
  • 包 javax.el 不存在

    我正在使用 jre6 eclipse 并导入 javax el 错误 包 javax el 不存在 javac 导入 javax el 过来 这不应该是java的一部分吗 谁能告诉我为什么会这样 谢谢 米 EL 统一表达语言 是 Java
  • 在java中为组合框分配键

    我想添加一个JComboBox在 Swing 中这很简单 但我想为组合中的每个项目分配值 我有以下代码 JComboBox jc1 new JComboBox jc1 addItem a jc1 addItem b jc1 addItem
  • 长轮询会冻结浏览器并阻止其他 ajax 请求

    我正在尝试在我的中实现长轮询Spring MVC Web 应用程序 http static springsource org spring docs 2 0 x reference mvc html但在 4 5 个连续 AJAX 请求后它会
  • CamcorderProfile.videoCodec 返回错误值

    根据docs https developer android com reference android media CamcorderProfile html 您可以使用CamcorderProfile获取设备默认视频编解码格式 然后将其
  • Spring Boot 无法更新 azure cosmos db(MongoDb) 上的分片集合

    我的数据库中存在一个集合 documentDev 其分片键为 dNumber 样本文件 id 12831221wadaee23 dNumber 115 processed false 如果我尝试使用以下命令通过任何查询工具更新此文档 db

随机推荐

  • 替换 Eclipse 中所有文件中的字符串[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 如何在当前项目的所有文件中搜索并替换字符串 假设我有字符串 sites default 现在我希望它是 public sites defa
  • PHP glob() 列出不以下划线开头的文件?

    标准的 glob 函数用法如下 dir glob txt foreach dir as filename echo filename size filesize filename n 使用 作为通配符 但是有没有办法否定它以忽略任何以下划线
  • Google 地图 V3 不会删除事件侦听器

    我在使用 Google 地图 API v3 时遇到问题 我试图在缩放更改时删除鼠标悬停侦听器 这是我的代码 document ready function var myOptions var map new google maps Map
  • Android LruCache(Android 3.1)线程安全

    是新的Android类LruCache http developer android com reference android util LruCache html线程安全 java 文档说 这个类是线程安全的 通过在缓存上同步以原子方式
  • 用Qt等待一个SLOT完成执行

    在我的代码中我发出一个信号mySignal我想等待连接的插槽结束mySlot在继续之前执行 emit mySignal Wait for the end of mySlot execution Some code that has to b
  • 使用 CLASSIC ASP 将数据返回到 jsonp 调用

    我已经浏览了 stackoverflow 上的帖子 但似乎找不到我要找的东西 如果我这样做 表格 ajaxSettings dataType jsonp get http MYREMOTESERVER com GetCustNewID as
  • C# 检查网络状态

    如何检查我是否有开放的网络连接并且可以联系 C 中的特定 IP 地址 我在 VB Net 中见过示例 但它们都使用 My 结构 谢谢 如果您只想检查网络是否正常 请使用 bool networkUp System Net NetworkIn
  • 为什么“rails runner -e production”不起作用?

    为了解决我的问题 该问题在 生产中的 will paginate 错误未定义方法 paginate https stackoverflow com questions 19577344 will paginate error in prod
  • 如何使用查找和替换在 Xcode 中用单个新行替换多个新行

    在Xcode项目中 我们编写程序时一般不会关注行间距 那么编码完成后 如何用一行删除所有多余的换行符呢 在文本搜索字段中 可以使用以下方法进行多行 非打印字符 搜索 替换 假设我想替换所有实例 This is a comment with
  • 我的 Cocos2D-iPhone 游戏可以支持 VoiceOver 吗?

    我正在制作一款游戏 玩家通过动作对声音做出反应 由于玩游戏不需要视觉元素 而且许多人都是闭着眼睛玩的 所以不完全兼容 VoiceOver 似乎很遗憾 我目前正在使用 Cocos2D iPhone 和 CocosDenshion 来处理音频
  • php图像显示无需在邮件中下载

    您好 我想使用 php 邮件程序类发送 html 格式的图像 但图像下载后显示在邮件中 但我想显示图像而不下载 邮件程序类中是否有任何选项或者有另一种方法 或者我必须以其他格式发送图像 好吧 可能的答案只有两种 您不想在电子邮件中嵌入实际的
  • 使用 IO 在 C# 中读取十六进制

    我是从 Java 转向 C 的新手 我正在尝试使用十六进制 IO 读取文件 当我读取第一个字节时 我看不到我在十六进制编辑器中看到的内容 我在用着 StreamReader reader new StreamReader fileDirec
  • 如何获得与 Apple 推送通知一起播放的自定义声音?

    我正在尝试在发送 Apple 推送通知时播放自定义声音 显示弹出消息 徽章也会更新 但我总是播放默认的 iPhone 声音 而不是我的自定义声音 这是我发送的 JSON 数据 aps sound sound caf badge 2 aler
  • 将值返回给不同的 Activity

    我有一个应用程序 基本上看起来像附图中的设计 可以看到 有4个Activity 部分Activity有碎片 我想将测试的答案返回到用户的个人资料中 到目前为止 我一直将结果上传到服务器 并让应用程序在用户每次返回时更新用户的个人资料简介活动
  • ng-repeat 过滤器“显示所有”项目(如果未选择过滤器)

    我有一个ng repeat过滤器使用
  • iOS 7 UIWebView 键盘问题

    我必须像这里一样删除这个栏link https stackoverflow com questions 13101642 ios keyboard style in webview但对于 iOS 7 此代码不起作用 我们通过一些 Objec
  • 忽略触发器中的错误

    我有一个存储过程 在插入 更新 删除时的触发器内调用 问题是这个 SP 中有一个不重要的代码块 因此我想忽略此代码块引起的任何错误 我将此代码块插入到 TRY CATCH 块中 但令我惊讶的是我收到了以下错误 当前事务无法提交 并且无法支持
  • 如何在 AWS Step Function 中获取纪元时间

    我们可以使用 State EnteredTime 引用AWS Step函数中的当前时间 但这给出了ISO格式 有没有办法获取纪元秒 毫秒 我想基于此在 DynamoDB 中添加 TTL 值 这可能吗 或者我是否必须仅为时间戳调用 Lambd
  • 观察对象的所有元素(除了一个元素)

    我的代码中有一块手表 scope watch foo function true 这确保了如果对象 foo 中的任何属性发生更改 则将调用此监视 我想对此破例 如果 foo 中除一个属性之外的任何属性发生更改 我想调用此监视 如果该属性发生
  • 如何使用从密码派生的密钥正确加密和解​​密文件

    我正在尝试找出使用 PBEWithHmacSHA256AndAES 256 标准加密和解密文件的正确过程 据我了解 看这个示例代码 https docs oracle com javase 8 docs technotes guides s