gpt4 book ai didi

encryption - j2me 中文手机不支持 AESEncoder 类

转载 作者:行者123 更新时间:2023-12-05 01:12:01 25 4
gpt4 key购买 nike

我在 J2ME 应用程序中使用这个加密类。我的 J2ME 应用程序在所有诺基亚设备上运行良好。该应用程序不适用于中国 MIw200 手机。也许该手机不支持这种加密技术?有没有其他的解决方案或者其他的加解密方法?

请帮帮我。非常感谢。

我的代码如下:

import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


public class AESEncoder {

private SecretKeySpec keyspec;
private Cipher cipher;
private String secretkey;

public AESEncoder(String secretkey) {
this.secretkey = secretkey;
keyspec = new SecretKeySpec(secretkey.getBytes(), 0, 16, "AES");

// keyspec=new SecretKeySpec(key, offset, len, secretkey);

try {
cipher = Cipher.getInstance("AES/ECB/NoPadding");
} 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");
}

int encrypted = 0;

byte[] bytenc = null;//new byte[32];
byte[] input = null;

try {
cipher.init(Cipher.ENCRYPT_MODE, keyspec);
// byte empty[]=padString(text).getBytes();
// encrypted = cipher.doFinal(padString(text).getBytes());
// encrypted=cipher.doFinal(padString(text).getBytes(), 0, 0, padString(text).getBytes(), 0);

input = padString(text).getBytes();
bytenc = new byte[input.length];
encrypted = cipher.doFinal(input, 0, input.length, bytenc, 0);

String str = new String(bytenc, 0, encrypted);
// encrypted=cipher.update(padString(text).getBytes(), 0, 0, 0, 0);
// System.out.println("Encrypted is:>>" + str);
// bytenc=hexToBytes(String.valueOf(encrypted));
} catch (Exception e) {
throw new Exception("[encrypt] " + e.getMessage());
}
return bytenc;
}


public String encrypt_hsm(String text) throws Exception {
if (text == null || text.length() == 0) {
throw new Exception("Empty string");
}
String base64=null;
int encrypted = 0;

byte[] bytenc = null;//new byte[32];
byte[] input = null;

try {
cipher.init(Cipher.ENCRYPT_MODE, keyspec);
// byte empty[]=padString(text).getBytes();
// encrypted = cipher.doFinal(padString(text).getBytes());
// encrypted=cipher.doFinal(padString(text).getBytes(), 0, 0, padString(text).getBytes(), 0);

input = padString(text).getBytes();
bytenc = new byte[input.length];
encrypted = cipher.doFinal(input, 0, input.length, bytenc, 0);

String str = new String(bytenc, 0, encrypted);

base64 = Base64.encode(bytenc);

// encrypted=cipher.update(padString(text).getBytes(), 0, 0, 0, 0);
// System.out.println("Encrypted is:>>" + str);
// bytenc=hexToBytes(String.valueOf(encrypted));


} catch (Exception e) {
throw new Exception("[encrypt] " + e.getMessage());
}
return base64;
}

public byte[] decrypt(String code) throws Exception {
if (code == null || code.length() == 0) {
throw new Exception("Empty string");
}
int decrypted = 0;

byte[] bytedec = null;
byte[] input = null;

try {
cipher.init(Cipher.DECRYPT_MODE, keyspec);

// input=hexToBytes(code);
input = Base64ToBytes(code);
bytedec = new byte[input.length];
decrypted = cipher.doFinal(input, 0, input.length, bytedec, 0);

String str = new String(bytedec, 0, decrypted);
// System.out.println("Decrypted is:>>" + str);

} catch (Exception e) {
throw new Exception("[decrypt] " + e.getMessage());
}
return bytedec;
}

public static String bytesToHex(byte[] bsData) {
int nDataLen = bsData.length;
String sHex = "";
for (int nIter = 0; nIter < nDataLen; nIter++) {
int nValue = (bsData[nIter] + 256) % 256;
int nIndex1 = nValue >> 4;
sHex += Integer.toHexString(nIndex1);
int nIndex2 = nValue & 0x0f;
sHex += Integer.toHexString(nIndex2);
}
return sHex;
}

public 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 static String padString(String source) {
char paddingChar = ' ';
int size = 32;
int x = source.length() % size;
int padLength = size - x;

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

// System.out.println("====>Pad String:" + source);
return source;
}

public void startApp() {
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

private byte[] Base64ToBytes(String code) {
code = code.replace('-', '+');
code = code.replace('_', '/');
code = code.replace(',', '=');
System.out.println("Final Base 64:"+code);

byte[] aesString = Base64.decode(code);
// System.out.println("Base64 after decoding:"+new String(aesString));
return aesString;
}
}

最佳答案

请发布堆栈跟踪。否则我们都只是猜测。

AES 是一个 required cipher ,有时介于 v4 和 v7 之间。花一些时间确认此密码在 MIw200 上不可用。

Someone getting "AES not available"在 Mac 上得到了一些建议。

尝试让系统回退到 3DES 或 DES,而不是强制使用 AES。 Here's an example that tests encryption with each algorithm.

尝试使用 BouncyCastle应用程序接口(interface)。 Here is a how-to guide .

关于encryption - j2me 中文手机不支持 AESEncoder 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20466270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com