0

我正在编写一个使用 AES (ECB) 模式加密和解密数据的应用程序。黑莓代码的加密数据通过php代码成功解密。但问题是,当我从 php 获取加密文本时,我无法用黑莓代码解密它。即使我没有得到任何异常。

这是我加密和解密文本的代码。

public 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();


    return output.toByteArray();
}
public static String AESDecryption(byte[] keyData,byte[] cipherText, int dataLength ) throws CryptoException, IOException {

    // Create the input stream based on the ciphertext
    ByteArrayInputStream in = new ByteArrayInputStream( cipherText, 0, dataLength );

    // Now create the block decryptor and pass in a new instance
    // of an AES decryptor engine with the specified block length
    BlockDecryptor cryptoStream = new BlockDecryptor(new AESDecryptorEngine( new AESKey( keyData )), in );

    byte[] T= new byte[dataLength];
    // Read the decrypted text from the AES decryptor stream and
    // return the actual length read

    int length= cryptoStream.read( T );
  String str= new String(T);


  return str;
}

提前致谢..

4

2 回答 2

3

这是接受密钥和加密数据(以字节数组的形式)并以字节数组的形式返回解密数据的方法:

public static byte[] decrypt(byte[] keyData, byte[] ciphertext) 
         throws CryptoException, IOException {

    // First, create the AESKey again.
    AESKey key = new AESKey(keyData);

    // Now, create the decryptor engine.
    AESDecryptorEngine engine = new AESDecryptorEngine(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).
    PKCS5UnformatterEngine uengine = new PKCS5UnformatterEngine(engine);

    // Create the BlockDecryptor to hide the decryption details away.
    ByteArrayInputStream input = new ByteArrayInputStream(ciphertext);
    BlockDecryptor decryptor = new BlockDecryptor(uengine, 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;
}

注意加密/解密不直接对字符串进行操作。它仅适用于字节数组。因此,作为最后的操作,您需要使用String(byte[] bytes)String(byte[] bytes, String enc)构造函数将解密的字节数组转换为字符串。如果您的字符串使用“ISO-8859-1”编码(这是 BlackBerry 的默认编码)以外的任何编码进行编码,则第二个构造函数会很有用。

更新:

事实证明,您没有在服务器端使用 PKCS5 填充并将加密的字节数据转换为 HEX 字符串,那么解决方案代码是:

// this is to convert HEX to bytes
public static byte[] convertHexToBytes(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

// this is the same as initial version, but we don't handle PKCS5 padding here
public static byte[] decrypt(byte[] keyData, byte[] ciphertext)
        throws CryptoException, IOException {

    // 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;
}

// and finally :)
String key =       "1234567890123456";
String encrypted = "48b983c4f1575280d244b74cf689efe5";

byte[] keyBytes = key.getBytes();
byte[] encryptedBytes = convertHexToBytes(encrypted);

// displays "nirav bhandari"
Dialog.inform(new String(decrypt(keyBytes, encryptedBytes)));
于 2012-06-28T11:02:45.200 回答
1

调用解密函数时,如何将密文转换为Byte[]?

于 2012-06-28T12:11:16.947 回答