代码之家  ›  专栏  ›  技术社区  ›  Señor Reginold Francis

Android加密

  •  27
  • Señor Reginold Francis  · 技术社区  · 16 年前

    我正在开发一个Android应用程序,我需要在其中一个方面使用加密。我真的对我使用的算法(AES、DES、RSA等)漠不关心。我知道Java有一个密码包,但我一点也不熟悉它。有人能发布一个如何加密/解密功能的例子吗?

    4 回复  |  直到 8 年前
        1
  •  14
  •   Community Mohan Dere    9 年前

    这个 java AES 库中有一个缺陷,允许侦听器在正确的情况下解密发送的数据包。见 Padding Oracle Exploit Tool vs Apache MyFaces .

    有人说,看看这个问题 Java 256bit AES Encryption .

    Bouncy Castle AES示例被盗自: http://www.java2s.com/Code/Java/Security/EncryptionanddecryptionwithAESECBPKCS7Padding.htm

    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    public class MainClass {
      public static void main(String[] args) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());    
        byte[] input = "www.java2s.com".getBytes();
        byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 
                     0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 
                     0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 
                     0x15, 0x16, 0x17 };
    
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
    
        System.out.println(new String(input));
    
        // encryption pass
        cipher.init(Cipher.ENCRYPT_MODE, key);
    
        byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
        int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
        ctLength += cipher.doFinal(cipherText, ctLength);
        System.out.println(new String(cipherText));
        System.out.println(ctLength);
    
        // decryption pass
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
        int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
        ptLength += cipher.doFinal(plainText, ptLength);
        System.out.println(new String(plainText));
        System.out.println(ptLength);
      }
    }
    
        2
  •  1
  •   Community Mohan Dere    9 年前

    看看我的答案 Android database encryption . 它包含2个文件,您可以在任何需要加密数据存储的应用程序中包含这些文件。

        3
  •  1
  •   user868459    12 年前

    我还要检查一下“隐藏”是否符合你的要求。它有一个易于使用的API,它抽象了加密细节: https://github.com/facebook/conceal/

        4
  •  0
  •   Tin Megali    8 年前

    考虑到Android中对数据进行加密和解密的开销,我设计了一个只依赖Android和Java原生库的库,使过程尽可能简单。

    要安装,请使用jcenter Distribution Center。 年级学生:

    compile 'com.tinmegali.android:mcipher:0.4'

    用法

    String ALIAS = "alias"
    MEncryptor encryptor = new MEncryptorBuilder( ALIAS ).build();
    MDecryptor decryptor = new MDecryptorBuilder( ALIAS ).build();
    
    String toEncrypt = "encrypt this string";
    // encrypting
    String encrypted = encryptor.encryptString( toEncrypt, this );
    
    // decrypting
    String decrypted = decryptor.decryptString( encrypted, this );
    

    mcipher与sdk 19+兼容,并自动适应较小和较大的数据块。默认情况下,它使用 AES/GCM/NoPadding 对于SDK 23+,以及 RSA/ECB/PKCS1Padding 对于旧版本。

    MCipher on Github