java - 在 Java ME 和 Android 中对相同数据产生相同的加密结果?

标签 java android encryption java-me

我必须使用 AES 算法在 J2ME 和 android 中加密相同的数据。

但是加密结果不一样。我想产生与加密数据相同的输出结果。

J2ME代码:

    public  String Encrypt(String text, String key)
        throws Exception {

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        byte[] b= key.getBytes("UTF-8");
        int len= b.length;
        if (len > keyBytes.length) len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes,0,keyBytes.length, "AES");

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

            byte[] outputBytes = new byte[100];
            byte[] inputBytes;
            inputBytes=text.getBytes("UTF-8");
        int results = cipher.doFinal(inputBytes,0,inputBytes.length,outputBytes,0);

            String str = new String(outputBytes, 0, results);

        String strMobile_No = Base64.encode(str.getBytes());
            String strresult=strMobile_No.toString();
          textField.setString(strMobile_No);
          return strresult;

        }

Android 代码:

    private  String Encrypt(String text, String key)
        throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        byte[] b= key.getBytes("UTF-8");
        int len= b.length;
        if (len > keyBytes.length) len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);

        byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
        Log.v("GET Result from  final:",results.toString());
        strMobile_No = Base64.encodeToString(results, 1);

        return strMobile_No;

        }

J2ME 生成:85IV+rkwyE/oO6z7uvwKbw==

Android 生成:XYMqEaliHBykRXGqV4LawA

有人可以帮我修复我的代码吗?

最佳答案

使用此代码进行加密和解密。出于测试目的,您可以使用“1234567812345678”作为您的 SecretKey。

public MCrypt(String SecretKey) {
    ivspec = new IvParameterSpec(iv.getBytes());
    keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
}

public byte[] encrypt(String text) throws Exception {
    if (text == null || text.length() == 0)
        throw new Exception("Empty string");
    byte[] encrypted = null;
    try {
        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        encrypted = cipher.doFinal(padString(text).getBytes());
    } catch (Exception e) {
        throw new Exception("[encrypt] " + e.getMessage());
    }
    return encrypted;
}

private byte[] decrypt(String code) throws Exception {
    if (code == null || code.length() == 0)
        throw new Exception("Empty string");
    byte[] decrypted = null;
    try {
        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
        decrypted = cipher.doFinal(hexToBytes(code));
    } catch (Exception e) {
        throw new Exception("[decrypt] " + e.getMessage());
    }
    return decrypted;
}

public static String bytesToHex(byte[] data) {
    if (data == null) {
        return null;
    }

    int len = data.length;
    String str = "";
    for (int i = 0; i < len; i++) {
        if ((data[i] & 0xFF) < 16)
            str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
        else
            str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
    }
    return str;
}

private static byte[] hexToBytes(String str) {
    if (str == null) {
        return null;
    } else if (str.length() < 2) {
        return null;
    } else {
        int len = str.length() / 2;
        byte[] buffer = new byte[len];
        for (int i = 0; i < len; i++) {
            buffer[i] = (byte) Integer.parseInt(
                    str.substring(i * 2, i * 2 + 2), 16);
        }
        return buffer;
    }
}

private String padString(String source) {

    char paddingChar = ' ';
    int size = 16;
    int x = source.length() % size;
    int padLength = size - x;

    for (int i = 0; i < padLength; i++) {
        source += paddingChar;
    }
    return source;
}

我希望您知道 key 应该是 128/256 位。

关于java - 在 Java ME 和 Android 中对相同数据产生相同的加密结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17854366/

相关文章:

java - 如何在java中创建直方图

java - 关于java多线程的一个问题

android SeekBar - 删除焦点丢失

java - 检查在 "EditText"中输入的数字是否在数组中 - Android Studio Java

c - Apache 日志 RSA 加密

oracle - 为 Oracle 12 配置 SSL 连接

java - GAE 项目类路径上的 Xerces JAR 导致 "SAXParserFactoryImpl not found"

java - Java EE 应用程序之间的 Web 服务通信

android - 如何将图像添加到 jetpack compose/kotlin 中的按钮

linux - 将 Linux 用户的当前密码与在 Perl 中输入的密码进行比较