java - 将硬编码文件解密为 byte[]

标签 java android encryption

嗯,这实际上是一个二人组...

首先我需要

  1. 读取文件内容
  2. 将它们加密成一个byte[]
  3. byte[] 写入文件或其他...

然后#2 或#3 的结果将进入另一个项目。我正在努力保护我们的 PEM/DER key 。

为了解密,我需要

  1. 读取加密文件的内容作为byte[]
  2. 将它们解密成byte[]
  3. 将解密数据写入文件或使用它代替文件

现在,我有了一些基本的加密代码

        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128); // 192 and 256 bits may not be available

    SecretKey secretKey = keyGenerator.generateKey();

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");


    // By initializing the cipher in CBC mode, an "initialization vector" has been randomly
    // generated. This initialization vector will be necessary to decrypt the encrypted data.
    // It is safe to store the initialization vector in plain text for later use. You can obtain
    // it's bytes by calling iv.getIV().
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    IvParameterSpec iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class);
        //      IvParameterSpec iv = new IvParameterSpec(IV); //used for the hardcoded one

        byte[] encryptedData = cipher.doFinal(data);

还有解密

    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    byte[] decryptedData = cipher.doFinal(encryptedData);
    System.out.println("decrypted: " + new String(decryptedData));

问题是:

在这样一种用例场景中,人们很少会加密某些内容并且会分发要在运行时解密的加密 key ,除了密文之外我还需要保存什么?

我知道我需要保存 IV,但是当我解密时不是很好 - 这让我相信我也需要保存 secretKey。

任何人都可以给我任何提示、指示或一般安全提示以获得更好的解决方案吗?如果我需要保存 key 、IV和加密后的数据,我应该把它们存放在哪里?也许对 key 进行硬编码并将 IV 与加密数据一起存储?也许对 IV 和 key 都进行硬编码,然后将加密数据存储在文件中?

这与理论上的安全无关,将其视为您可能对试图窃取您的 key 的人造成的最大麻烦和不便。我们都知道我无法完美地隐藏它们。

我非常需要这个人以 Decrypting an encrypted file and executing in Java 开头的东西

但是,如果有更好的方法将安全数据输入 PemKeyReader,我会洗耳恭听。

最佳答案

共享 key 和加密某些东西是完全不同的两件事。 How to share keys

话虽如此,128 位的 AES 是比 3DES 更强大的加密算法,所以您可以做的是保持 PKI 基础设施到位交换 AES key ,然后使用它们进行加密和解密。

为什么不是RSARSA 需要至少 512 位才能将其视为最强,如果您增加更多位,则会增加加密和解密所需的时间。

SO AES 快速且安全。

使用SecretKeySpec从 byte[] 创建 key

public static void main(String[] args) throws Exception
{
    // Initialise secret key with predefined byte array [] like below. I
    // have used simple string to array method to generate 16 byte array.
    // AES Key must be minimum 16 bytes.
    // Now you can put this byte array some where is .SO file.
    // Generate new Key using this byte []
    // Then you can generate a key using device specific information at
    // first boot up.
    // Use second key to encrypt data and first key to encrypt the second
    // key
    // I Hope it clears all the doubts
    SecretKey key = new SecretKeySpec("ABCDEFGHIJKLMNOP".getBytes(), "AES");
    System.out.println(Arrays.toString(key.getEncoded()));
    // Initialise Cipher with AES Algorithm
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    // Set The Encrypt Mode
    cipher.init(Cipher.ENCRYPT_MODE, key);
    // Encrypt some bytes
    byte[] encrypted = cipher.doFinal("ABCDEFGH".getBytes());
    // Print it to vefiry
    System.out.println(Arrays.toString(encrypted));

    // Get The IV
    byte[] iv = cipher.getIV();
    System.out.println(iv.length);
    // Now why storing you can create structure like [16 IV][Encrypted Data]
    // And while decrypting you can read first [16] bytes IV and then
    // decrypt remaining bytes

    //byte[] iv = new byte[16];
    // System.arraycopy(encrypted, 0, iv, 0, 16)
    //Copy remaining bytes to decrypt


    // set cipher to decrypt mode

    cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(iv));

    // decrypt it
    byte[] decrypted = cipher.doFinal(encrypted);
    System.out.println(new String(decrypted));

}

现在编写一个算法,该算法将从一些随机数据(如设备名称、用户名、随机种子等)生成 byte[]。

您可以通过使用 C 编写该算法并创建 .SO 文件并获取 byte [] 来为算法源代码添加更多保护本地调用

这样做的好处是什么?

  1. 如果您的 so 被黑客入侵,则需要实时环境来运行从中创建 key 。
  2. 即使有人破解了它,损坏也将是有限的,即 1 台设备
  3. 黑客将不得不对每台设备重复相同的操作,这是极不可能做到的。

关于java - 将硬编码文件解密为 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12053299/

相关文章:

c# - 如何从 C# 代码运行 jar 文件

多边形内/多边形上的 JavaFX 文本

java - 使用java解压文件

java - 什么是列出已编译的 Java 类所需的所有导入的可移植方法

java - 从 Android 启动不同的任务

android - YouTube Android API 停止工作

安卓 JDBC 连接器

ios - 在 iOS 3+ 中对称加密字符串的正确且安全的方法

http - HMAC 256 与 HMAC 512 JWT 签名加密

iphone - 如何在 iOS 中加密 mp3 文件