java - 通过 JDK 7 使用 AES 128 时出现问题

标签 java encryption encoding aes

我有一个从 Mainframe_JCL 触发的独立 Java 程序。 Java 程序有一个代码来加密和解密一个字符串。

当我在本地运行程序时。加密后,当我解密时,该值是正确的(我得到了我为加密提供的字符串)。

但是当它通过 JCL 执行它在大型机上运行时......我得到了奇怪的解密值......有点像垃圾。

不确定是什么问题。任何帮助,将不胜感激。

我们正在使用以下 JDK: IBM SDK for z/OS,Java 技术版,版本 7

下面是用于加密和解密的方法:

    public static String encrypt(String text) throws UnsupportedEncodingException {
        byte iv[] = new byte[16];
        byte[] encrypted = null;
        BASE64Encoder enc = new BASE64Encoder();
        try {            
            SecureRandom random = new SecureRandom();
            random.nextBytes(iv);
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, getKeySpec(), ivspec);
            encrypted = cipher.doFinal(text.getBytes());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return URLEncoder.encode(enc.encode(iv)+enc.encode(encrypted), "UTF-8");
    }

    public static String decrypt(String text) {
        byte iv[] = new byte[16];
        String decrypted  = "";
        byte[] splitText = null;
        byte[] textToDecrypt = null;
        BASE64Decoder dec = new BASE64Decoder();
        try { 
            text = URLDecoder.decode(text, "UTF-8");
            text = text.replaceAll(" ", "+");
            splitText = dec.decodeBuffer(text);
            splitText.toString();
            for(int i=0;i<16;i++){
                iv[i]=splitText[i];
            }
            textToDecrypt = new byte[splitText.length - 16];
            for(int i=16;i<splitText.length;i++){
                textToDecrypt[i-16]=splitText[i];
            }
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, getKeySpec(), ivspec);
           decrypted = new String(cipher.doFinal(textToDecrypt));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        return decrypted;
    }

static SecretKeySpec spec = null;
public static SecretKeySpec getKeySpec() throws IOException,
                                            NoSuchAlgorithmException {
    if (spec == null) {
        String keyFile = "aes_key.key";
        spec = null;
        InputStream fis = null;
        fis = Config.class.getClassLoader().getResourceAsStream(keyFile);
        byte[] rawkey = new byte[16];
        fis.read(rawkey);
        fis.close();
        spec = new SecretKeySpec(rawkey, "AES");
}
return spec;
}

最佳答案

您没有提供足够的信息来运行您的代码(所以我不会修复),但我发现有两件事可能会破坏结果:

  1. 在使用 Base64 编码之前,您应该在 16 字节 IV 之前添加前缀;目前您可能正在混合 IV 和密文。
  2. 您应该为toBytesnew String 定义字符编码;目前,您可能需要满足两个不同的平台字符集。

最后,URL 编码 base 64 可能不是很有效,你最好只替换 +/ 字符 by URL safe counterparts .


所以我遇到了一个难题:

package nl.owlstead.stackoverflow;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public final class VeryStaticStringEncryption {

    private static final String KEY_FILE = "aes_key.key";
    private static final SecretKey AES_KEY = retrieveSecretKey(KEY_FILE);

    private VeryStaticStringEncryption() {
        // avoid instantiation
    }

    public static String encrypt(final String text) {
        try {
            final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            final int n = cipher.getBlockSize();

            final SecureRandom random = new SecureRandom();
            final byte[] iv = new byte[n];
            random.nextBytes(iv);
            final IvParameterSpec ivspec = new IvParameterSpec(iv);

            cipher.init(Cipher.ENCRYPT_MODE, AES_KEY, ivspec);

            final byte[] plaintext = text.getBytes(UTF_8);
            byte[] ciphertext = Arrays.copyOf(iv,
                    n + cipher.getOutputSize(plaintext.length));
            final int ciphertextSize = cipher.doFinal(
                    plaintext, 0, plaintext.length,
                    ciphertext, n);

            // output size may be bigger, but not likely
            if (n + ciphertextSize  < ciphertext.length) {
                ciphertext = Arrays.copyOf(ciphertext, n + ciphertextSize);
            }

            return Base64.getUrlEncoder().encodeToString(ciphertext);
        } catch (final GeneralSecurityException e) {
            throw new IllegalStateException("Could not encrypt string", e);
        }
    }

    public static String decrypt(final String text) {
        try {
            // throws IllegalArgumentException if decoding fails
            final byte[] ciphertext = Base64.getUrlDecoder().decode(text);

            final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            final int n = cipher.getBlockSize();

            // CBC specific
            if (ciphertext.length < n + n || ciphertext.length % n != 0) {
                throw new IllegalArgumentException("Ciphertext has incorrect size");
            }

            final IvParameterSpec ivspec = new IvParameterSpec(ciphertext, 0, n);
            cipher.init(Cipher.DECRYPT_MODE, AES_KEY, ivspec);
            final byte[] plaintext = cipher.doFinal(ciphertext, n, ciphertext.length - n);
            return new String(plaintext, UTF_8);
        } catch (final GeneralSecurityException e) {
            throw new IllegalStateException("Could not encrypt string", e);
        }
    }

    public static SecretKey retrieveSecretKey(final String keyResource) {
        try (final InputStream fis = VeryStaticStringEncryption.class.getResourceAsStream(keyResource)) {
            final byte[] rawkey = new byte[16];
            for (int i = 0; i < 16; i++) {
                final int b = fis.read();
                if (b == -1) {
                    throw new IOException("Key is not 16 bytes");
                }
                rawkey[i] = (byte) b;
            }

            if (fis.read() != -1) {
                throw new IOException("Key is not 16 bytes");
            }

            return new SecretKeySpec(rawkey, "AES");
        } catch (final IOException e) {
            // e may contain confidential information
            throw new IllegalStateException("AES key resource not available");
        }
    }

    public static void main(String[] args) {
        String ct = encrypt("owlstead");
        System.out.println(ct);
        String pt = decrypt(ct);
        System.out.println(pt);
    }
}

请注意,我使用了包本地资源,因为我不想弄乱我的 SO 项目。

重要说明:如果可能发生中间人攻击,CBC 加密对于传输模式加密是不安全的。如果需要,重构为 GCM 模式。

关于java - 通过 JDK 7 使用 AES 128 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33214730/

相关文章:

java - CMIS 浏览器网络应用程序

encryption - 我需要指定加密字符串的内容类型吗?

java - java中的UTF-8编码,从网站检索数据

python - UnicodeDecodeError 帮助 : Data needs to be in ascii for seaborn but source code is in utf-8

java - Ajax 调用在 Android 模拟器 Web View 中不起作用

java - Runtime Change Android 素材主题

java - Java 中的泛型继承

php - 在 Cocoa 中加密数据,在 PHP 中解码

python - 用于 AES 256 加密的(纯)Python 库是什么?

python - 使用 Python 写入和读取二进制文件时,Jupyter 中出现编码错误