【问题标题】:AES 128bit ECB Encryption Ciphered Text OverflowAES 128 位 ECB 加密密文溢出
【发布时间】:2015-07-06 05:42:47
【问题描述】:

我目前正在尝试使用 BouncyCastle 库制作一个程序,以使用十六进制密钥(长度 16)加密和解密十六进制字符串(长度 16)。它可以工作,但是,加密后,密文的长度是预期的两倍(当前输出长度为 32,预期输出长度为 16)。

电流输出:

 Input: helloworldsingap
Length: 16

   Key: 0000000000000000000000000000004D
Length: 16

Cipher: 47DDFB51938DB86E6CC3EF9F078C78C6FC5E0686775522C07BB46BAB18E0FCFA
Length: 32

Output: helloworldsingap
Length: 16

预期输出:

 Input: helloworldsingap
Length: 16

   Key: 0000000000000000000000000000004D
Length: 16

Cipher: 47DDFB51938DB86E6CC3EF9F078C78C6
Length: 16

Output: helloworldsingap
Length: 16

对于我当前的输出,密文的前半部分是正确的。有谁知道我当前输出的密码末尾的额外 16 个字符是什么,以及如何删除它?

这是我的程序:

package com.optixal.tests;

import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class AES128ECB {

    public static void main(String[] args) throws Exception {

        Security.addProvider(new BouncyCastleProvider());

        // Input
        String hexInput = "68656c6c6f776f726c6473696e676170";
        byte[] byteInput = toByteArray(hexInput);

        // Key
        String hexKey = decToHex(77);
        byte[] byteKey = toByteArray(hexKey);

        System.out.println(" Input: " + new String(byteInput));
        System.out.println("Length: " + byteInput.length + "\n");
        System.out.println("   Key: " + hexKey);
        System.out.println("Length: " + byteKey.length + "\n");

        SecretKeySpec key = new SecretKeySpec(byteKey, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

        // Encrypt
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherText = new byte[cipher.getOutputSize(byteInput.length)];
        int ctLength = cipher.update(byteInput, 0, byteInput.length, cipherText, 0);
        ctLength += cipher.doFinal(cipherText, ctLength);
        System.out.println("Cipher: " + toHexString(cipherText));
        System.out.println("Length: " + ctLength + "\n");

        // Decrypt
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
        int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
        ptLength += cipher.doFinal(plainText, ptLength);
        System.out.println("Output: " + new String(plainText));
        System.out.println("Length: " + ptLength);
    }

    private static final String hexDigits = "0123456789ABCDEF";
    private static final char[] hexDigitsArray = hexDigits.toCharArray();

    public static String decToHex(int dec) {

        final int sizeOfIntInHalfBytes = 32;
        final int numberOfBitsInAHalfByte = 4;
        final int halfByte = 0x0F;

        StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
        hexBuilder.setLength(sizeOfIntInHalfBytes);
        for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i) {
            int j = dec & halfByte;
            hexBuilder.setCharAt(i, hexDigitsArray[j]);
            dec >>= numberOfBitsInAHalfByte;
        }
        return hexBuilder.toString();
    }

    public static String toHexString(byte[] array) {
        return DatatypeConverter.printHexBinary(array);
    }

    public static byte[] toByteArray(String s) {
        return DatatypeConverter.parseHexBinary(s);
    }

}

谢谢!

【问题讨论】:

    标签: java encryption


    【解决方案1】:

    使用AES/ECB/NoPadding 代替AES/ECB/PKCS7Padding 在行

    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
    

    会解决你的问题。

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多