java - AES加密j2me

标签 java android java-me aes

我正在尝试在 j2me 中进行 AES 加密。我对 android 使用了几乎相同的代码,并且在那里工作正常。以下是代码块。我得到 null 作为输出

package cartoon;

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

public class MCrypt {

private String iv = "0123456789abcdef";// iv 
private IvParameterSpec ivspec;
private SecretKeySpec keyspec;
private Cipher cipher;
private String SecretKey = "fedcba9876543210";// secretKey 

public MCrypt() {
    ivspec = new IvParameterSpec(iv.getBytes(), 0, iv.getBytes().length);

    keyspec = new SecretKeySpec(SecretKey.getBytes(), 0, iv.getBytes().length, "AES");

}

String Decrypt(String text) throws Exception {
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
    byte[] results = null;
    int results1 = cipher.doFinal(Base64.decode(text), 0, Base64.decode(text).length, results, 0);
    System.out.println("String resultssssssssssssss " + results1);
    return new String(results, "UTF-8");
}

String Encrypt(String text)
        throws Exception {
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    System.out.println("String input : " + text);

    cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
    byte[] results = null;
    int results1 = cipher.doFinal(text.getBytes(), 0, text.getBytes().length, results, 0);
    return Base64.encode(results);
}
}

打印结果

MCrypt mcrypt = new MCrypt();
    try {
        encrypted = mcrypt.Encrypt("id=450");

        decrypted = new String(mcrypt.Decrypt(encrypted));
        System.out.println("Encrypted Text : " + encrypted);
        System.out.println("Decrypted Text : " + decrypted);

    } catch (Exception e) {
        e.printStackTrace();
    }

我哪里错了?

最佳答案

尝试以下操作

你应该首先适本地初始化字节数组

String Decrypt(String text) throws Exception {
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

    // here
    byte[] results = cipher.doFinal(Base64.decode(text));

    int results1 = cipher.doFinal(Base64.decode(text), 0, Base64.decode(text).length, results, 0);
    System.out.println("String resultssssssssssssss " + results1);
    return new String(results, "UTF-8");
}

String Encrypt(String text)
    throws Exception {
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    System.out.println("String input : " + text);

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

    // and here
    byte[] results = cipher.doFinal(text.getBytes());

    int results1 = cipher.doFinal(text.getBytes(), 0, text.getBytes().length, results, 0);
    return Base64.encode(results);
}

关于java - AES加密j2me,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13263210/

相关文章:

java - 解析使用对象: Doesn't display even though record exists创建表

java - 在流式传输 URL 时获取 Android VideoView 收到的数据包的访问权限

java - Google 手机 map "My Location"功能如何运作?

java - 使用点击监听器绕过堆叠 View

检查给定范围之间的数字是否为回文的 Java 程序

java - HashMap,获取时可能是原子的,并且保证键已经完全放入

Android 麦克风频率响应音调检测低于 100Hz

android - 将应用程序移动到 SD 卡

android - MathFP 库的使用

java - 在 J2ME 中实现最高性能的 float 据类型