AES 256在黑莓 [英] AES 256 in Blackberry

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

问题描述

我该怎么办AES 256加密的黑莓...

我使用加密:但没有得到数据的 AES256标准

 私有静态的byte []加密(字节[] KEYDATA,字节[]数据)抛出CryptoException,IOException异常
    {
        //创建AES密钥用于加密数据。
        //这将创建一个使用尽可能多的KEYDATA的AES密钥
        //越好。
        AESKey键=新AESKey(KEYDATA);        //现在,我们希望对数据进行加密。
        //首先,创建我们用实际的加密引擎
        //数据的加密。
        AESEncryptorEngine发动机=新AESEncryptorEngine(密钥);        //因为我们不能保证该数据将是一个平等的块
        //长度我们要使用的填充引擎(PKCS5在这种情况下)。
        PKCS5FormatterEngine fengine =新PKCS5FormatterEngine(发动机);        //创建一个BlockEncryptor隐藏引擎的细节了。
        ByteArrayOutputStream输出=新ByteArrayOutputStream();
        BlockEncryptor加密=新BlockEncryptor(fengine,输出);        //现在,我们需要做的是我们的数据写入到输出流。
        //但在此之前,让我们计算对数据的哈希为好。
        //一个摘要提供单向散列函数映射大量
        //数据到一个唯一的20字节的值(在SHA1的情况下)的。
        SHA1Digest消化=新SHA1Digest();
        digest.update(数据);
        字节[] =哈希digest.getDigest();        //现在,写出所有数据和散列的,以确保
        //数据中没有被修改。
        encryptor.write(数据);
        encryptor.write(散);
        encryptor.close();
        output.close();        //现在,加密的数据是坐在ByteArrayOutputStream。
        //我们只是想找回它。
        返回output.toByteArray();
    }


解决方案

这给了我wQVge + rn7HGVs17a82GKTw ==。享受:

 私有静态的byte []加密(字节[] KEYDATA,字节[]数据)
        抛出CryptoException,IOException异常{
    //创建AES密钥用于加密数据。
    //这将创建一个使用尽可能多的KEYDATA的AES密钥
    //越好。
    AESKey键=新AESKey(KEYDATA);    //现在,我们希望对数据进行加密。
    //首先,创建我们用实际的加密引擎
    //数据的加密。
    AESEncryptorEngine发动机=新AESEncryptorEngine(密钥);    //因为我们不能保证该数据将是一个平等的块
    //长度我们要使用的填充引擎(PKCS5在这种情况下)。
    PKCS5FormatterEngine fengine =新PKCS5FormatterEngine(发动机);    //创建一个BlockEncryptor隐藏引擎的细节了。
    ByteArrayOutputStream输出=新ByteArrayOutputStream();
    BlockEncryptor加密=新BlockEncryptor(fengine,输出);    encryptor.write(数据);
    encryptor.close();
    output.close();    //现在,加密的数据是坐在ByteArrayOutputStream。
    //我们只是想找回它。
    返回output.toByteArray();
}.... 别的地方字节[] KEYDATA =@ mbe0RcM $ @ mbe0RcM $ @ mbe0RcM $ @ mbe0.getBytes();
字节[]数据=S2526.getBytes();
字节[] encryptedInAES;
尝试{
    encryptedInAES =加密(KEYDATA,数据);
}赶上(例外五){
    Dialog.alert(无法​​AES:+ E);
    返回;
}字节[] EN codedInBase64 = NULL;
尝试{
    EN codedInBase64 = Base64OutputStream.en code(
        encryptedInAES,0,encryptedInAES.length,假的,假的
    );
}赶上(IOException异常五){
    Dialog.alert(无法​​为Base64:+ E);
    返回;
}尝试{
    字符串连接codedStr =新的String(EN codedInBase64,UTF-8);
    Dialog.alert(结果:+ EN codedStr);
}赶上(UnsupportedEncodingException忽略){}

How can I do AES 256 encryption in Blackberry...

I am using for encryption : but not get data AES256 standard :

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

解决方案

This gave me "wQVge+rn7HGVs17a82GKTw==". Enjoy:

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) {}

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

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