2010-09-28 6 views
3

J'écris une application pour Android qui utilise le cryptage à clé symétrique pour protéger les données sensibles. Pour autant que je sache, Android ne supporte directement que "PBEWithMD5AndDES". Quel est le degré de sécurité de cet algorithme? Aussi, j'ai inclus mon code ci-dessous (non-andriod). Mon code crypte-t-il correctement les données?Est-ce une méthode de cryptage sécurisée?

import java.io.UnsupportedEncodingException; 
import java.security.InvalidAlgorithmParameterException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import java.security.SecureRandom; 
import java.security.spec.InvalidKeySpecException; 
import java.security.spec.InvalidParameterSpecException; 
import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.PBEKeySpec; 
import javax.crypto.spec.SecretKeySpec; 

public class CipherTest 
{ 

    private static class EncryptInfo 
    { 

     private final byte[] encryptedData; 
     private final byte[] initVector; 
     private final byte[] salt; 

     public EncryptInfo(byte[] encryptedData, byte[] initVector, byte[] salt) 
     { 
      this.encryptedData = encryptedData.clone(); 
      this.initVector = initVector.clone(); 
      this.salt = salt.clone(); 
     } 

     public byte[] getEncryptedData() 
     { 
      return encryptedData; 
     } 

     public byte[] getInitVector() 
     { 
      return initVector; 
     } 

     public byte[] getSalt() 
     { 
      return salt; 
     } 

    } 

    private static final String keyGenAlgorithm = "PBEWithMD5AndDES"; 
    private static final String keyAlgorithm = "DES"; 
    private static final String cipherTransform = "PBEWithMD5AndDES/CBC/PKCS5Padding"; 

    private static EncryptInfo encrypt(char[] password, byte[] data) 
      throws NoSuchAlgorithmException, InvalidKeySpecException, 
      NoSuchPaddingException, InvalidKeyException, 
      InvalidParameterSpecException, IllegalBlockSizeException, 
      BadPaddingException, UnsupportedEncodingException 
    { 

     byte[] salt = new byte[16]; 
     new SecureRandom().nextBytes(salt); 

     PBEKeySpec keySpec = new PBEKeySpec(password, salt, 1024); 

     SecretKeyFactory secretKeyFactory = SecretKeyFactory 
       .getInstance(keyGenAlgorithm); 
     SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); 
     keySpec.clearPassword(); 
     byte[] key = secretKey.getEncoded(); 
     SecretKeySpec secretKeySpec = new SecretKeySpec(key, keyAlgorithm); 
     Cipher cipher = Cipher.getInstance(cipherTransform); 
     cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); 

     byte[] initVector = cipher.getParameters().getParameterSpec(
       IvParameterSpec.class).getIV(); 

     return new EncryptInfo(cipher.doFinal(data), initVector, salt); 
    } 

    public static byte[] decrypt(byte[] data, char[] password, byte[] salt, 
      byte[] initVector) throws NoSuchAlgorithmException, 
      InvalidKeySpecException, NoSuchPaddingException, 
      InvalidKeyException, InvalidAlgorithmParameterException, 
      IllegalBlockSizeException, BadPaddingException 
    { 
     PBEKeySpec keySpec = new PBEKeySpec(password, salt, 1024); 

     SecretKeyFactory secretKeyFactory = SecretKeyFactory 
       .getInstance(keyGenAlgorithm); 
     SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); 
     keySpec.clearPassword(); 
     byte[] key = secretKey.getEncoded(); 
     SecretKeySpec secretKeySpec = new SecretKeySpec(key, keyAlgorithm); 
     Cipher cipher = Cipher.getInstance(cipherTransform); 
     cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(
       initVector)); 
     return cipher.doFinal(data); 
    } 

    public static void main(String[] args) throws Exception 
    { 
     char[] password = "password".toCharArray(); 

     EncryptInfo info = encrypt(password, "Message".getBytes()); 

     byte[] decyptedText = decrypt(info.getEncryptedData(), password, info 
       .getSalt(), info.getInitVector()); 

     System.out.println(new String(decyptedText)); 

    } 
} 
+1

Citation: "Mon code crypte-t-il correctement les données?" - Désolé mais nous ne testons pas le code pour vous! Vous avez une méthode de cryptage et de décryptage. Essayez d'exécuter des tests simples avec plusieurs chaînes que vous chiffrez/déchiffrez. Si le résultat est égal à l'entrée, tout va bien! – WarrenFaith

Répondre

4

MD5 et DES sont faibles. Si vos données cryptées sont vraiment précieuses, vous devriez chercher une bibliothèque de chiffrement externe pour Android qui offre des algorithmes AES et SHA256/SHA512.

1

Si vous souhaitez crypter des données en utilisant un cryptage à clé symétrique, je recommande:
1) Utilisez AES, car il est certifié par la NSA pour les données classées secrètes.
2) Utilisez une implémentation bien révisée pour ne pas avoir à rechercher la bonne façon de configurer le code. Par exemple, AESCrypt.

Vous pouvez trouver ici AESCrypt: http://www.aescrypt.com/java_aes_crypt.html

Je l'ai vu AESCrypt utilisé dans plusieurs institutions financières. AESCrypt pour Java est une classe unique qui appelle les méthodes JCE. Android, JCE est implémenté par bouncycastle. J'ai vu Bouncycastle utilisé dans plusieurs grandes institutions financières.

Questions connexes