解密加密AES2​​56字节 [英] Decrypt AES256 encrypted bytes

查看:102
本文介绍了解密加密AES2​​56字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有在加密之前的工作。其实我什么都不知道加密。我有一个文件使用OpenSSL的PARAMS加密工具:

I've never worked with encryption before. Actually I know nothing about encryption. I have a file encrypted with openssl tool using params:

OpenSSL的AES-256-CBC -nosalt -in FILEIN退房手续FILEOUT -p -k KEY

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

我需要将其解密到内存中,但我不知道怎么办。任何人都可以向我提供加密相关code?

I need to decrypt it into memory but I don't know how. Can anyone provide me the code related to encryption?

推荐答案

下面是我写解密字符串带上面PARAMS codeD类(如果我remmeber它是正确的):

Here's class I have written to decrypt a string encoded with params above (if I remmeber it correct):

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的公地-COM press.jar。

Salt is an empty bytearray in this case. It uses 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);

其中,的InputStream 加密的数据。

这篇关于解密加密AES2​​56字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆