0

鉴于我知道 aes-128-cbc 用于生成密文,没有使用加盐并且 IV 都是零,我的目标是编写一个程序来找出用于加密纯文本的密钥(我还得到了一个可能使用的键的字典)。

所以我实现了以下

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class myClassName123 {

/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    try {
        BufferedReader in = new BufferedReader(new FileReader("/Users/username/Documents/f12/words.txt"));
        String str;
        while ((str = in.readLine()) != null && str.getBytes().length<16) { //clue given in spec

                System.out.println(decrypt(getBytesFromFile(new File("/Users/username/Documents/f21/some.aes-128-cbc")),str));


        }
        in.close();
    } catch (IOException e) {
    }
}

/**
* This method decrypts the input byte [] using AES Key byte [] 
* 
* @param byte []
* @param byte [] 
* @return byte []
* @throws Exception
*/
public static byte[] decrypt(byte[] text, String key) throws Exception {
    Cipher cipher;
    byte[] bytes = null;


    Provider provider = new BouncyCastleProvider();
    MessageDigest digester = MessageDigest.getInstance("SHA-256", provider);
    digester.update(key.getBytes("UTF-8"));
    //byte[] key = digester.digest();
    SecretKeySpec spec = new SecretKeySpec(digester.digest(), "AES");

    //SecretKeySpec spec = new SecretKeySpec(toByteArray(key.toCharArray()), "AES");
     byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);
    try {
            // Instantiate the cipher
            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, spec, ivspec);

             bytes = cipher.doFinal(text);

             String value = new String(bytes, "UTF-8");

                System.out.println("DEBUG HERE: "+value);

    }catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        //  throw new Exception(e);
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    //  throw new Exception(e);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        //throw new Exception(e);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
        //throw new Exception(e);
    } catch (BadPaddingException e) {
        e.printStackTrace();
        //throw new Exception(e);
    }
    return bytes;
}

// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);

    // Get the size of the file
    long length = file.length();

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}   
}

但是,当我运行它时,我得到以下信息:

在这里调试:(M��1�����-��e�}�p����+�� [B@4ed1e89e

我想我没有正确转换为字符串,但我目前坚持这一点。非常感谢任何帮助,非常感谢。

4

1 回答 1

0

如果问题是字符没有正确显示,可能是因为解密的结果包含不可打印的字符。

即使原始文本是纯文本,这种行为也是有意义的,因为您正在尝试使用不同的键。如果你选错了,你很可能会得到这样的结果。

您还可以将字符串显示为十六进制(http://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java),这可能使其更具可读性。

希望能帮助到你

于 2012-09-28T13:00:42.790 回答