我是黑莓开发新手,必须使用 AES/ECB/NoPadding 完成加密和解密任务。我使用了以下代码,来自互联网。
加密方式:
public static byte[] encrypt( byte[] keyData,String message )
throws Exception
{
byte[] data = message.getBytes("UTF-8");
// Create the AES key to use for encrypting the data.
// This will create an AES key using as much of the keyData
// as possible.
if ((data.length % 16) != 0 ) {
StringBuffer buffer = new StringBuffer(message);
int moduleOut = data.length % 16;
int padding = 16 - moduleOut;
for(int i = 0 ; i < padding; i++){
buffer.append(" ");
}
data = buffer.toString().getBytes("UTF-8");
}
AESKey key = new AESKey( keyData);
NoCopyByteArrayOutputStream out = new NoCopyByteArrayOutputStream(data.length);
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.toByteArray(), 0, cbytes, 0, finalLength);
// encryptor.close();
// out.close();
return cbytes;
}
解密方法:
public static byte[] decrypt(byte[] keyData, byte[] base64EncodedData)
throws CryptoException, IOException
{
// String base64EncodedData=new String(base64EncodedData);
byte[] cipherText =Base64ToBytes(new String(base64EncodedData));
// First, create the AESKey again.
AESKey key = new AESKey(keyData);
// Now, create the decryptor engine.
AESDecryptorEngine engine = new AESDecryptorEngine(key);
// Create the BlockDecryptor to hide the decryption details away.
ByteArrayInputStream input = new ByteArrayInputStream(cipherText);
BlockDecryptor decryptor = new BlockDecryptor(engine, input);
// Now, read in the data.
byte[] temp = new byte[100];
DataBuffer buffer = new DataBuffer();
for (;;)
{
int bytesRead = decryptor.read(temp);
buffer.write(temp, 0, bytesRead);
if (bytesRead < 100)
{
// We ran out of data.
break;
}
}
byte[] plaintext = buffer.getArray();
return plaintext;
}
Base64 到 Bytes 的转换方法:
private static byte[] Base64ToBytes(String code) {
byte[] aesString = null;
try
{
aesString = Base64InputStream.decode(code);
}
catch (IOException ioe)
{
}
return aesString;
}
现在的问题是,当我使用上述方法加密我的字符串时,我在字符串的末尾得到了填充的 unicode,这在服务器端是不能容忍的。
我知道这是由于 PKCS5FormatterEngine 并且在使用此类时在字符串末尾附加字符是正常的。
但是,如果我想用 AES/ECB 方法加密和解密字符串,以及用 NoPadding 加密和解密字符串怎么办。我知道 ECB 不是一种安全模式,除了服务器使用 PHP 并准备就绪,在 Android 和 J2ME 中运行良好。
请指导。如何绕过 PKCS5FormatterEngine 或在没有任何填充的情况下进行加密。
更新:
我尝试在 Blackberry 中使用 Cipher 类,就像在 Android 和 J2ME 中使用的一样,但它在 net_rim_api.jar 中似乎不可用,即使我尝试下载 bouncy castle jar 文件和依赖类 NoSuchAlogrithmException 所以 java.security jar (org.osgi.foundation-1.0. 0.jar),编译但当我尝试运行它停止说找到重复的类。我为 java.security 保留的 jar 中几乎没有重复的类有问题。
如果您对此有解决方案,请告诉我。
更新答案: 我已经用完整的加密和解密代码更新了我的代码,并检查了答案以获得更好的理解。