2017-10-24 5 views
1

以下は、私のアンドロイドプロジェクトのAESに関するいくつかの調査の後にまとめたアルゴリズムです。私が知りたいのは、安全であり、改善できるのでしょうか?理由は私が求めているのは、これを行う方法が非常に多く、ちょっとした助けが必要だからです。AESアルゴリズムは安全ですか?

ご協力いただきありがとうございます。

private static final int pswdIterations = 1000; 
private static final int keySize = 256; 
private static final int saltlength = keySize/8; 

private static final String ENCODING = "UTF-8"; 
private static final String PBK = "PBKDF2WithHmacSHA1"; 
private static final String AES = "AES"; 
private static final String CIPHER = "AES/CBC/PKCS5Padding"; 

public String encrypt(String plainText) throws Exception { 
    //get text from password field 
    final String pass = password.getText().toString(); 
    //get salt from generateSalt() method (see below) 
    String salt = generateSalt(); 
    //convert salt to bytes 
    byte[] saltBytes = salt.getBytes(ENCODING); 

    // Derive the key from 
    SecretKeyFactory factory = SecretKeyFactory.getInstance(PBK); 
    PBEKeySpec spec = new PBEKeySpec(
      pass.toCharArray(), 
      saltBytes, 
      pswdIterations, 
      keySize 
    ); 

    //encode key 
    SecretKey secretKey = factory.generateSecret(spec); 
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), AES); 

    //encrypt the message 
    Cipher cipher = Cipher.getInstance(CIPHER); 
    cipher.init(Cipher.ENCRYPT_MODE, secret); 
    AlgorithmParameters params = cipher.getParameters(); 
    byte[] ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV(); 
    byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes(ENCODING)); 

    //encode text and output final encrypted text 
    String encodedText = Base64.encodeToString(encryptedTextBytes, Base64.DEFAULT); 
    String encodedIV = Base64.encodeToString(ivBytes, Base64.DEFAULT); 
    String encodedSalt = Base64.encodeToString(saltBytes, Base64.DEFAULT); 
    return encodedSalt + encodedText + encodedIV; 
} 

public static String generateSalt() { 
    SecureRandom random = new SecureRandom(); 
    byte bytes[] = new byte[saltlength]; 
    random.nextBytes(bytes); 
    return new String(bytes); 
} 
+4

https://codereview.stackexchange.com/questions/tagged/android – petey

+0

またはhttps://crypto.stackexchange.com/またはhttps://security.stackexchange.com/にこの質問を移動することを検討してください。主にセキュリティに関するものです。 – m69

答えて

1

本質的に、このスキームは安全に見えますが、認証は含まれません。

改良:

  1. 組み合わせた結果で反復回数を含めます。
  2. バージョンインジケータを含めます。バージョンインジケータは、1バイトと同じくらい簡単です。
  3. 通常、バイト配列が結合され、全体で1つのBase64エンコーディングが使用されます。

注:
は一緒に様々なアイテムをパッケージング例えばRNCryptor-Specを見てみましょうRNCryptor

を使用することを検討してください。

関連する問題