使用部分密钥进行java AES256解密
这是家庭作业。我们的教授在CBC模式下用AES-256加密了一条消息(密钥为256bit)。然后他给了我们他使用的IV(randomized
在我的代码中),加密的消息和密钥的后半部分(keySuffix
),密钥也是十六进制的。我们必须使用蛮力解密该消息。该消息是几个长度为8的块,由1和0组成,中间有空格(cryptogram
)。我所做的是去掉空格,将密码转换为byteArray
(您可以在代码中看到我是如何做到这一点的)。在键上我只做getBytes()
,在IV上我使用DatatypeConverter.parseHexBinary()
,否则它会抛出一个关于错误IV长度的错误。当我收到一条消息时,我使用new String(myByteArray)
将其作为字符串返回。然后我检查消息是否包含所有正常字符(而不是一些“bush”)
关键是我没有得到任何结果。我不知道这里可能出了什么问题,我的猜测是,有些转换并没有按预期的方式进行。我一直在用unicodes等做一些尝试,但是蛮力需要很长时间才能完成,这样的测试很麻烦。有没有人能为我指出正确的方向,比如如何进行转换。为键的另一侧生成所有可能的字符串可以正常工作
顺便说一句,PKCS5Padding给出了一个错误
public class Main {
private String randomized = "21232d0960a7b522d3e25141e54ecee6";
private String keySuffix = "1dad418a";
private String cryptogram = "00110001 01111000 01111101 01111100 01100001 11011110 10010010 01011011";
private byte[] cryptogramBytes;
private String pattern = "[a-zA-Z1-9\\s]*";
private IvParameterSpec ivSpec = null;
private Cipher cipher = null;
public static void main(String... args){
char[] elements = { 'a', 'b', 'c', 'd', 'e', 'f', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
char[] buff = new char[8];
Main main = new Main ();
byte[] convertedRandomized = DatatypeConverter.parseHexBinary(main.randomized);
main.ivSpec = new IvParameterSpec(convertedRandomized);
main.cryptogram = main.cryptogram.replaceAll("\\s", "");
BigInteger bigint = new BigInteger(main.cryptogram, 2);
main.cryptogramBytes = bigint.toByteArray();
main.cipher = Cipher.getInstance("AES/CBC/NoPadding");
main.permGen(elements, 0, 8, buff);
}
public void permGen(char[] s, int i, int k, char[] buff){
if (i < k) {
for (int j = 0; j < s.length; j++) {
buff[i] = s[j];
permGen(s, i + 1, k, buff);
}
} else {
String result = decrypt(String.valueOf(buff) + keySuffix);
if (result.matches(pattern))
System.out.println("Key is: " + String.valueOf(buff) + keySuffix);
}
}
public String decrypt(String key){
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
return new String(cipher.doFinal(cryptogramBytes));
}
}
共 (0) 个答案