如何在Blackberry中进行AES 256加密...
我用于加密:但没有获得数据 AES256标准:
private static byte[] encrypt( byte[] keyData, byte[] data ) throws CryptoException, IOException
{
// Create the AES key to use for encrypting the data.
// This will create an AES key using as much of the keyData
// as possible.
AESKey key = new AESKey( keyData );
// Now, we want to encrypt the data.
// First, create the encryptor engine that we use for the actual
// encrypting of the data.
AESEncryptorEngine engine = new AESEncryptorEngine( key );
// Since we cannot guarantee that the data will be of an equal block
// length we want to use a padding engine (PKCS5 in this case).
PKCS5FormatterEngine fengine = new PKCS5FormatterEngine( engine );
// Create a BlockEncryptor to hide the engine details away.
ByteArrayOutputStream output = new ByteArrayOutputStream();
BlockEncryptor encryptor = new BlockEncryptor( fengine, output );
// Now, all we need to do is write our data to the output stream.
// But before doing so, let's calculate a hash on the data as well.
// A digest provides a one way hash function to map a large amount
// of data to a unique 20 byte value (in the case of SHA1).
SHA1Digest digest = new SHA1Digest();
digest.update( data );
byte[] hash = digest.getDigest();
// Now, write out all of the data and the hash to ensure that the
// data was not modified in transit.
encryptor.write( data );
encryptor.write( hash );
encryptor.close();
output.close();
// Now, the encrypted data is sitting in the ByteArrayOutputStream.
// We simply want to retrieve it.
return output.toByteArray();
}
答案 0 :(得分:3)
这给了我“wQVge + rn7HGVs17a82GKTw ==”。享受:
private static byte[] encrypt(byte[] keyData, byte[] data)
throws CryptoException, IOException {
// Create the AES key to use for encrypting the data.
// This will create an AES key using as much of the keyData
// as possible.
AESKey key = new AESKey(keyData);
// Now, we want to encrypt the data.
// First, create the encryptor engine that we use for the actual
// encrypting of the data.
AESEncryptorEngine engine = new AESEncryptorEngine(key);
// Since we cannot guarantee that the data will be of an equal block
// length we want to use a padding engine (PKCS5 in this case).
PKCS5FormatterEngine fengine = new PKCS5FormatterEngine(engine);
// Create a BlockEncryptor to hide the engine details away.
ByteArrayOutputStream output = new ByteArrayOutputStream();
BlockEncryptor encryptor = new BlockEncryptor(fengine, output);
encryptor.write(data);
encryptor.close();
output.close();
// Now, the encrypted data is sitting in the ByteArrayOutputStream.
// We simply want to retrieve it.
return output.toByteArray();
}
.... somewhere else
byte[] keyData = "@mbe0RcM$@mbe0RcM$@mbe0RcM$@mbe0".getBytes();
byte[] data = "S2526".getBytes();
byte[] encryptedInAES;
try {
encryptedInAES = encrypt(keyData, data);
} catch (Exception e) {
Dialog.alert("Failed to AES: " + e);
return;
}
byte[] encodedInBase64 = null;
try {
encodedInBase64 = Base64OutputStream.encode(
encryptedInAES, 0, encryptedInAES.length, false, false
);
} catch (IOException e) {
Dialog.alert("Failed to Base64: " + e);
return;
}
try {
String encodedStr = new String(encodedInBase64, "UTF-8");
Dialog.alert("Result: " + encodedStr);
} catch (UnsupportedEncodingException ignored) {}