【问题标题】:AES algorithm in blackberry黑莓中的AES算法
【发布时间】:2012-05-24 08:20:13
【问题描述】:

我正在黑莓中制作应用程序。在那个应用程序中,我想使用 AES 算法加密一个字符串。是否可以在黑莓中使用 AES 算法?有任何API吗?提前致谢,

【问题讨论】:

    标签: blackberry encryption java-me aes


    【解决方案1】:

    试试这个 -

    useremail= CryptAes.AESEncryption(username_.getBytes());
    

    CryptAes 类如下 -

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

    【讨论】:

    【解决方案2】:

    查看AESEncryptorEngineAESDecryptorEngine(在 Google 的帮助下)。

    【讨论】:

      【解决方案3】:

      或者,您也可以考虑为 j2me 使用充气城堡,建议 here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-21
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        • 2010-10-28
        • 2010-10-25
        • 1970-01-01
        相关资源
        最近更新 更多