我有以下加密/解密例程,需要將它們移植到我的BlackBerry項目。你能讓我開始嗎?Port Java-SE加密代碼到BlackBerry
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
public String EncryptData(String data, String skey) throws Exception {
String encryptedData = "";
try{
byte [] bData = data.getBytes();
String alg = "AES/ECB/NoPadding";
SecretKey key = new SecretKeySpec(skey.getBytes(), alg.replaceFirst("/.*", ""));
Cipher cipher = Cipher.getInstance(alg);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encoded = cipher.doFinal(bData);
encryptedData = bytesToHex(encoded);
}
catch(Exception e){
throw e;
}
return encryptedData;
}
public String DecryptData(String hexString, String skey) throws Exception {
String decryptedData = "";
try{
byte [] bData = convToBinary(hexString);
String alg = "AES/ECB/NoPadding";
SecretKey key = new SecretKeySpec(skey.getBytes(), alg.replaceFirst("/.*", ""));
Cipher cipher = Cipher.getInstance(alg);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decoded = cipher.doFinal(bData);
decryptedData = new String(decoded);
}
catch(Exception e){
throw e;
}
return decryptedData;
}
究竟是什麼問題了嗎? – malaverdiere 2010-07-07 10:45:41
看起來你已經開始了。 – 2010-07-07 11:22:36
上面列出的方法在我的Java應用程序中完美工作。但在黑莓手機中不能編譯。我試過導入相關的加密namspaces(net.rim.device.api.crypto。*)。但沒有一個與我上面的實現相匹配。我需要解密存在於文件中的數據,並且必須使用相同的算法 – JDeVil 2010-07-07 12:45:58