无法解密的PHP code的AES的密文黑莓 [英] Unable to decrypt AES's ciphertext of php code in blackberry

查看:96
本文介绍了无法解密的PHP code的AES的密文黑莓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用该加密和使用AE​​S(ECB)模式解密数据。
黑莓$ C $的加密数据c由PHP code成功解密。
但问题是,当我从PHP获取加密的文字我不是能与黑莓code..even I M没有得到任何异常解密。

i m writing one application which encrypt and decrypt data using AES (ECB) mode. Encrypted data of blackberry code successfully decrypt by php code. but problem is that when i get encrypted text from php i m not able to decrypt it with blackberry code..even i m not getting any exception.

这是我的code来加密和解密文本。

here is my code to encrypt and decrypt text.

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

在此先感谢..

thanks in advance..

推荐答案

下面是接收密钥和加密的数据(在字节数组的形式)的方法,并在字节数组的形式返回解密后的数据:

Here is the method that accepts key and encrypted data (in the form of byte arrays) and returns the decrypted data in the form of byte array:

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

请注意加密/解密不会用绳子直接操作。它只有一个字节数组。因此,作为一个最后的行动,你需要使用字符串(字节[]字节)字符串(字节[]字节来解密的字节数组转换为字符串,字符串ENC)构造函数。如果你的字符串是连接codeD比ISO-8859-1编码任何其他(这是黑莓手机默认的)第二个构造可能是有用的。

Note encryption/decryption does not operate with strings directly. It only work with byte arrays. So as a final action you need to convert decrypted byte array to string using String(byte[] bytes) or String(byte[] bytes, String enc) constructor. The second constructor can be useful if your string were encoded with any other than "ISO-8859-1" encoding (which is a default one for BlackBerry).

更新:

事实证明你没有使用填充PKCS5在服务器端,你加密的字节数据转换为十六进制字符串,那么解决方案code是:

As it turns out you don't use PKCS5 padding on server side AND you convert encrypted byte data into a HEX string, then the solution code is:

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

这篇关于无法解密的PHP code的AES的密文黑莓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆