解密 AES256 加密字节



我以前从未使用过加密。实际上我对加密一无所知。我有一个使用参数使用 openssl 工具加密的文件:

openssl aes-256-cbc -nosalt -in file in -out fileOUT -p -k KEY

我需要将其解密到内存中,但我不知道如何。任何人都可以为我提供与加密相关的代码吗?

这是我

编写的用于解密用上述参数编码的字符串的类(如果我记得正确的话):

public class CipherUtils {
    public static byte[] getKey(String password, byte[] salt) {
        try {
            byte[] passwordSalt = EncodingUtils.getAsciiBytes(password);
            passwordSalt = concatenateByteArrays(passwordSalt, salt);
            byte[] hash1 = getHashForHash(null, passwordSalt);
            byte[] hash2 = getHashForHash(hash1, passwordSalt);
            byte[] key = concatenateByteArrays(hash1, hash2);
            return key;
        } catch (Exception e) {
            return null;
        }
    }
    public static byte[] getIV(String password, byte[] salt) {
        try {
            byte[] passwordSalt = EncodingUtils.getAsciiBytes(password);
            passwordSalt = concatenateByteArrays(passwordSalt, salt);
            byte[] hash1 = getHashForHash(null, passwordSalt);
            byte[] hash2 = getHashForHash(hash1, passwordSalt);
            byte[] hash3 = getHashForHash(hash2, passwordSalt);
            return hash3;
        } catch (Exception e) {
            return null;
        }
    }
    private static byte[] getHashForHash(byte[] hash, byte[] passwordSalt) {
        try {
            byte[] hashMaterial = concatenateByteArrays(hash, passwordSalt);
            MessageDigest md = MessageDigest.getInstance("MD5");
            return md.digest(hashMaterial);
        } catch (Exception e) {
            return null;
        }
    }
    private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
        if (a == null)
            return b;
        if (b == null)
            return a;
        byte[] result = new byte[a.length + b.length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }
}

在这种情况下,盐是一个空字节数组。它使用apache-commons-compress.jar。

下面是使用示例:

byte[] key = CipherUtils.getKey(password, null);
byte[] IV = CipherUtils.getIV(password, null);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),
        new IvParameterSpec(IV));
cis = new CipherInputStream(is, cipher);

其中is是加密数据的InputStream

这可能会对你有所帮助

public void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        // Here you read the cleartext.
        FileInputStream fis = new FileInputStream("data/cleartext");
        // This stream write the encrypted text. This stream will be wrapped by
        // another stream.
        FileOutputStream fos = new FileOutputStream("data/encrypted");
        // Length is 16 byte
        SecretKeySpec sks = new SecretKeySpec("yourkey".getBytes(), "AES");
        // Create cipher
        Cipher cipher = Cipher.getInstance("AES/CBC");
        cipher.init(Cipher.ENCRYPT_MODE, sks);
        // Wrap the output stream
        CipherOutputStream cos = new CipherOutputStream(fos, cipher);
        // Write bytes
        int b;
        byte[] d = new byte[8];
        while ((b = fis.read(d)) != -1) {
            cos.write(d, 0, b);
        }
        // Flush and close streams.
        cos.flush();
        cos.close();
        fis.close();
    }

解密

public  void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        FileInputStream fis = new FileInputStream("data/encrypted");
        FileOutputStream fos = new FileOutputStream("data/decrypted");
        SecretKeySpec sks = new SecretKeySpec("yourkey".getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC");
        cipher.init(Cipher.DECRYPT_MODE, sks);
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        int b;
        byte[] d = new byte[8];
        while((b = cis.read(d)) != -1) {
            fos.write(d, 0, b);
        }
        fos.flush();
        fos.close();
        cis.close();
    }

最新更新