注册

同一个数据Java AES128_ECB加密结果和达梦AES128_ECB加密结果不一样

2023/10/18 975 24 已解决

为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】: DM8
【操作系统】:WIN10
【CPU】:
【问题描述】*:Java AES128_ECB加密结果和达梦AES128_ECB加密结果不一样,Java应该怎么写

    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
    private static final int KEY_SIZE = 16;

    public static void main(String[] args) throws Exception {
        String input = "13607984987";
        String key = "1234567891234567";

        String encrypted = encrypt(input, key);
        System.out.println("加密后的字符串:" + encrypted);
    }

    public static String encrypt(String input, String key) throws Exception {
        if (key == null || key.length() != KEY_SIZE) {
            throw new IllegalArgumentException("密钥必须是" + KEY_SIZE + "位");
        }

        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        byte[] encryptedBytes = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
        return bytesToHex(encryptedBytes);
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }
}

1697621414206.png
1697621465998.png

回答 0
暂无回答
扫一扫
联系客服