0
我必須使用AES算法在J2ME和android中加密相同的數據。爲Java ME和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
有人可以幫我解決我的代碼?
這甚至沒有編譯。 J2ME代碼中定義了「ivspec」?也有可怕的密碼錯誤,可能會破壞整個事情的安全。 – ntoskrnl
ohh有錯誤... ivspec在上面的j2me函數中定義:private IvParameterSpec ivspec; – Bushra
足夠公平......'ivpsec' *初始化*在哪裏? – ntoskrnl