简体   繁体   English

Android加密

[英]Android encryption

I am working on an android application, and I need to use encryption for one aspect of it. 我正在开发一个Android应用程序,我需要在它的一个方面使用加密。 I am really indifferent to which algorithm I use (AES, DES, RSA, etc...). 我对使用哪种算法(AES,DES,RSA等)无动于衷。 I am aware that Java has a crypto package, but I am not familiar with it at all. 我知道Java有一个加密包,但我根本不熟悉它。 Can someone post an example on how to do an encrypt/decrypt function? 有人可以发布一个关于如何进行加密/解密功能的例子吗?

The java AES library has a flaw in it that allows, under the right circumstances, a listener to decrypt the packets sent. java AES库有一个缺陷,它允许在适当的情况下,一个监听器解密发送的数据包。 See Padding Oracle Exploit Tool vs Apache MyFaces . 请参阅填充Oracle漏洞利用工具与Apache MyFaces

That being said check out this SO question Java 256bit AES Encryption . 那就是说看看这个问题Java 256bit AES加密

Bouncy Castle AES EXAMPLE stolen from: http://www.java2s.com/Code/Java/Security/EncryptionanddecryptionwithAESECBPKCS7Padding.htm 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);
  }
}

Look at my answer here Android database encryption . 看看我在这里Android数据库加密的答案。 It contains 2 files that you can include in any of your applications that require data storage to be encrypted. 它包含2个文件,您可以将这些文件包含在需要加密数据存储的任何应用程序中。

I would also check out Conceal to see if it fits your bill. 我还会查看隐藏,看它是否适合你的账单。 It has a easy to use API which abstracts the cryptographic details: https://github.com/facebook/conceal/ 它有一个易于使用的API,它抽象了加密细节: https//github.com/facebook/conceal/

Considering the overhead to encrypt and decrypt data in Android, I devised a library that relies only in Android and Java native libraries to make the process as simple as possible. 考虑到在Android中加密和解密数据的开销,我设计了一个仅依赖于Android和Java本机库的库,以使过程尽可能简单。

To install, use the jcenter distribuition center. 要安装,请使用jcenter分发中心。 On gradle: 在gradle上:

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

Usage 用法

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 is compatible from SDK 19+, and it automatically adapts itself to smaller and large chunks of data. MCipher与SDK 19+兼容,它自动适应较小和较大的数据块。 By default, it uses AES/GCM/NoPadding for SDKs 23+, and RSA/ECB/PKCS1Padding for older versions. 默认情况下,它为SDK 23+使用AES/GCM/NoPadding ,对旧版本使用RSA/ECB/PKCS1Padding

MCipher on Github MCipher在Github上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM