我有以下加密/解密例程,需要将它们移植到我的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;
}
答案 0 :(得分:3)
我建议你查看你正在使用的JDE的API文档,特别是我猜你的net.rim.device.api.crypto
包可能是你最感兴趣的。
net.rim.device.api.crypto.Crypto
也可能是一个很好的类,因为它包含用于加密和解密的静态方法。
答案 1 :(得分:1)
管理让它工作......
byte[] keyData = keyString.getBytes();
AESKey key = new AESKey(keyData);
NoCopyByteArrayOutputStream out = new NoCopyByteArrayOutputStream();
AESEncryptorEngine engine = new AESEncryptorEngine(key);
BlockEncryptor encryptor = new BlockEncryptor(engine, out);
encryptor.write(data, 0, data.length);
int finalLength = out.size();
byte[] cbytes = new byte[finalLength];
System.arraycopy(out.getByteArray(), 0, cbytes, 0, finalLength);
encryptedData = bytesToHex(cbytes);
return encryptedData;