简体   繁体   English

使用 NoPadding 解密 DESede 密码

[英]Decrypting DESede cipher with NoPadding

For a Crypto class we've been given a DESede ciphertext using ECB with no padding => "6t8Z5bKl5ybJL+MiFerNBmiTDS7wlTEUdWNwJJApWmQ==" which is in base64 form.对于 Crypto 类,我们获得了使用 ECB 且没有填充 => "6t8Z5bKl5ybJL+MiFerNBmiTDS7wlTEUdWNwJJApWmQ==" 的 DESede 密文,该密文采用 base64 格式。 We were given clues about the key and so I constructed all possible keys (where all are in an ASCII format).我们得到了有关密钥的线索,因此我构建了所有可能的密钥(所有密钥均为 ASCII 格式)。

            String strToDecrypt="6t8Z5bKl5ybJL+MiFerNBmiTDS7wlTEUdWNwJJApWmQ==";
            byte[] input = strToDecrypt.getBytes();

            //Decrypt
            Cipher b = Cipher.getInstance("DESede/ECB/NoPadding");
            b.init(Cipher.DECRYPT_MODE, keySpec);
            byte output[] = b.doFinal(input);
            String out = new String(output);
            System.out.println(new String(out));

When I run this code using my keys, I get an IllegalBlockSizeException as my input isn't a multiple of 8 bytes.当我使用我的密钥运行此代码时,我得到一个 IllegalBlockSizeException,因为我的输入不是 8 字节的倍数。 I'm confused as to which "bases" to use.我对使用哪些“基础”感到困惑。 As I've said above, the ciphertext is in base64 so When running Cipher.DECRYPT should I be giving keys in a certain "base" or the string I want to decrypt in a certain base.正如我上面所说的,密文是 base64,所以在运行 Cipher.DECRYPT 时,我应该在某个“基”中提供密钥,或者我想在某个基中解密的字符串。

Don't make the key generation harder than it needs to be.不要让密钥生成变得比它需要的更难。 You want to try each possible value for the first byte of the variable section.您想为变量部分的第一个字节尝试每个可能的值。 But what if that value is 0xFB?但是如果那个值是 0xFB 呢? Subsequent values would be 0xFC, 0xFD, 0xFE, and 0xFF.后续值将是 0xFC、0xFD、0xFE 和 0xFF。 But what about the last value?但是最后一个值呢? You can assume that they wrap around, to 0x00.您可以假设它们环绕到 0x00。

If that's the case, something like this should work to find the correct key:如果是这种情况,这样的事情应该可以找到正确的密钥:

static byte[] search(byte[] ciphertext) throws GeneralSecurityException {
  byte[] key = template(); /* Initialize the fixed bytes of "key" */
  Cipher nopadding = ... ; /* Create the correct cipher */
  for (long xy = 0; xy < 256; ++xy) { /* Iterate over possible values */
    for (long wz = 0; wz < 256; ++wz) { /* Is there another range? */
      for (int off = 0; off < 6; ++off) {
        key[8 + off] = (byte) (xy + off); /* Set the first range */
      }
      for (int off = 0; off < 6; ++off) {
        key[16 + off] = (byte) (wz + off); /* Set the second range */
      }
      nopadding.init(/* Initialize the cipher with your key */);
      byte[] plaintext = nopadding.doFinal(ciphertext);
      String str = new String(plaintext, StandardCharsets.US_ASCII);
      /* Return the key if it produced valid text */
      if (str.indexOf('\uFFFD') < 0) return key;
    }
  }
  throw new IllegalArgumentException(); /* No key found */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM