首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AES算法在黑莓手机中的应用

AES算法在黑莓手机中的应用
EN

Stack Overflow用户
提问于 2012-05-24 16:20:13
回答 3查看 851关注 0票数 0

我正在用黑莓做一个应用程序。在该应用程序中,我希望使用AES算法加密字符串。在Blackberry中可以使用AES算法吗?有什么API可以解决这个问题吗?提前谢谢你,

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-24 16:23:15

试试这个-

代码语言:javascript
复制
useremail= CryptAes.AESEncryption(username_.getBytes());

CryptAes类如下所示-

代码语言:javascript
复制
public class CryptAes {
// First create the AES key based on the bytes in secretKey using  keyLength bits as the length
static AESKey keydec = new AESKey("A3$1E*81234567891111111111111111".getBytes() );
static AESKey keyenc = new AESKey("A3$1E*81234567891111111111111111".getBytes() );
static AESKey keyenc128 = new AESKey("A3Q1EF8123456789".getBytes());
static AESKey keydec128 = new AESKey("A3Q1EF8123456789".getBytes());

private static byte[] iv = { 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c,
    0x0d, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c, 0x0d };

public static byte[] plainText= new byte[10000];

public static String AESEncryption(byte[] plainText) throws CryptoException, IOException {
      AESEncryptorEngine engine = new AESEncryptorEngine( keyenc128 );
      CBCEncryptorEngine cengine=new CBCEncryptorEngine(engine, new InitializationVector(iv));
      PKCS5FormatterEngine fengine = new PKCS5FormatterEngine( engine );
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      BlockEncryptor encryptor = new BlockEncryptor( fengine, output );

      encryptor.write(plainText);
      encryptor.close();
      byte[] encryptedData = output.toByteArray(); output.close();
      String st=new String(encryptedData);

      byte[] base64 = Base64OutputStream.encode(encryptedData, 0, encryptedData.length, false, false);

        //Base64Coder.encodeString(Byte.toString(plainText));
        String str = new String(base64);

   return str;
}


// sampleAESDecryption
public static String AESDecryption(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( keydec128 ), 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);

  int i=str.indexOf("</msg>");
  str=str.substring(0,i+6);
  return str;
}
}
票数 3
EN

Stack Overflow用户

发布于 2012-05-24 16:22:40

看看AESEncryptorEngineAESDecryptorEngine (在谷歌的帮助下)。

票数 2
EN

Stack Overflow用户

发布于 2012-05-24 22:25:11

或者,您可以考虑像here建议的那样,为j2me使用bouncy castle。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10733667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档