2010-09-20 5 views
5

Je crypte un fichier en Java et envoie le fichier crypté et la clé privée à l'appareil Android. Mais tout en décryptant le fichier dans Android, il donne l'erreur corrompue de bloc de pad. Par ailleurs, même code de déchiffrement fonctionne sur PCbloc de pad corrompu pendant le décryptage (Android)

Voici le cryptage:

public void encrypt(File inf, File outf, File publicKey, int userId, int resourceId) throws ArServerConnectionException { 
    // ENCRYPTION BEGIN 
    try { 
     pkCipher = Cipher.getInstance("RSA"); 
    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    // create AES shared key cipher 
    try { 
     aesCipher = Cipher.getInstance("AES"); 
    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     makeKey(); 
    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
      // File operation 

      try { 
     saveKey(new File(System.getProperty("user.home") + "/" + userId 
       + "/keyfile"), publicKey); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (GeneralSecurityException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    // File operation 

    try { 
     encryptFiles(inf, outf); 
    } catch (InvalidKeyException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    // /ENCRYPTION END 
} 

    public void saveKey(File out, File publicKeyFile) throws IOException, 
     GeneralSecurityException { 
    // read public key to be used to encrypt the AES key 
    byte[] encodedKey = new byte[(int) publicKeyFile.length()]; 
    new FileInputStream(publicKeyFile).read(encodedKey); 

    // create public key 
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey); 
    KeyFactory kf = KeyFactory.getInstance("RSA"); 
    PublicKey pk = kf.generatePublic(publicKeySpec); 

    // write AES key 
    pkCipher.init(Cipher.ENCRYPT_MODE, pk); 
    CipherOutputStream os = new CipherOutputStream(
      new FileOutputStream(out), pkCipher); 
    os.write(aesKey); 
    os.close(); 
} 
    public void makeKey() throws NoSuchAlgorithmException { 
    KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
    kgen.init(AES_Key_Size); 
    SecretKey key = kgen.generateKey(); 
    aesKey = key.getEncoded(); 
    aeskeySpec = new SecretKeySpec(aesKey, "AES"); 
} 

Voici la partie de décryptage:

public class FileDecrypt { 
    public static final int AES_Key_Size = 256; 

    Cipher pkCipher, aesCipher; 
    byte[] aesKey; 
    SecretKeySpec aeskeySpec; 
    private String pubKeyPath=null; 
    private String prvKeyPath=null; 
    private String keyFilePath=null; 
    private String encFilePath=null; 
    private String unencFilePath=null; 


    public String getEncFilePath() { 
     return encFilePath; 
    } 

    public void setEncFilePath(String encFilePath) { 
     this.encFilePath = encFilePath; 
    } 

    public String getUnencFilePath() { 
     return unencFilePath; 
    } 

    public void setUnencFilePath(String unencFilePath) { 
     this.unencFilePath = unencFilePath; 
    } 

    public String getPubKeyPath() { 
     return pubKeyPath; 
    } 

    public void setPubKeyPath(String pubKeyPath) { 
     this.pubKeyPath = pubKeyPath; 
    } 

    public String getPrvKeyPath() { 
     return prvKeyPath; 
    } 

    public void setPrvKeyPath(String prvKeyPath) { 
     this.prvKeyPath = prvKeyPath; 
    } 

    public String getKeyFilePath() { 
     return keyFilePath; 
    } 

    public void setKeyFilePath(String keyFilePath) { 
     this.keyFilePath = keyFilePath; 
    } 
    public void decrypt() { 
     Log.i("DECRYPT","**************************************************DECRYPT&*******************"); 
     Log.i("encFilePath",encFilePath); 
     Log.i("pubKeyPath",pubKeyPath); 
     Log.i("prvKeyPath",prvKeyPath); 
     Log.i("keyFilePath",keyFilePath); 
     Log.i("unencFilePath",unencFilePath); 
     Log.i("DECRYPT","********************************************DECRYPT&*******************"); 
     try { 
      pkCipher = Cipher.getInstance("RSA"); 
     } catch (NoSuchAlgorithmException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (NoSuchPaddingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      aesCipher = Cipher.getInstance("AES"); 
     } catch (NoSuchAlgorithmException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (NoSuchPaddingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     //DECRYPTION BEGIN 

     File pkf=new File(pubKeyPath); 
     byte[] encodedKey = new byte[(int) pkf.length()]; 
     try { 
      new FileInputStream(pkf).read(encodedKey); 
      // create public key 
      X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey); 
      KeyFactory kf = KeyFactory.getInstance("RSA"); 
      PublicKey pk = kf.generatePublic(publicKeySpec); 

      // write AES key 
      pkCipher.init(Cipher.ENCRYPT_MODE, pk); 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (NoSuchAlgorithmException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvalidKeyException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvalidKeySpecException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      loadKey(new File(keyFilePath), new File(prvKeyPath)); 
     } catch (GeneralSecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      decrypt(new File(encFilePath), new File(unencFilePath)); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     //DECRYPTION END 
    } 

    /** 
    * Decrypts an AES key from a file using an RSA private key 
    */ 
    public void loadKey(File in, File privateKeyFile) 
      throws GeneralSecurityException, IOException { 
     // read private key to be used to decrypt the AES key 
     byte[] encodedKey = new byte[(int) privateKeyFile.length()]; 
     new FileInputStream(privateKeyFile).read(encodedKey); 

     // create private key 
     PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey); 
     KeyFactory kf = KeyFactory.getInstance("RSA"); 
     PrivateKey pk = kf.generatePrivate(privateKeySpec); 

     // read AES key 
     pkCipher.init(Cipher.DECRYPT_MODE, pk); 
     aesKey = new byte[AES_Key_Size/8]; 
     CipherInputStream is = new CipherInputStream(new FileInputStream(in), 
       pkCipher); 
     is.read(aesKey); 
     aeskeySpec = new SecretKeySpec(aesKey, "AES"); 
    } 

    /** 
    * Decrypts and then copies the contents of a given file. 
    */ 
    public void decrypt(File in, File out) throws IOException 
     { 
     try { 
      aesCipher.init(Cipher.DECRYPT_MODE, aeskeySpec); 
     } catch (InvalidKeyException e) { 
      Log.i("EXCEPTION","INVALID KEY EXCEPTION"); 
      e.printStackTrace(); 
     } 

     CipherInputStream is = new CipherInputStream(new FileInputStream(in), 
       aesCipher); 
     FileOutputStream os = new FileOutputStream(out); 

     copy(is, os); 

     is.close(); 
     os.close(); 
    } 

    /** 
    * Copies a stream. 
    */ 
    private void copy(InputStream is, OutputStream os) throws IOException { 
     int i; 
     byte[] b = new byte[2048]; 
     while ((i = is.read(b)) != -1) { 
      os.write(b, 0, i); 
     } 
    } 
} 

Répondre

4

Nous étions également confrontés au même problème, il a été résolu uniquement en faisant la partie cryptage aussi sur le côté android (en utilisant android SDK), puis décrypter sur l'appareil.

+0

Yeap, nous avions résolu le problème exactement comme vous l'avez dit. –

0

La raison la plus probable est le code utilisent pour envoyer le fichier à l'appareil est incorrect. Peut-être est-il tronqué, bourré ou corrompu de quelque façon que ce soit pour ne pas le déchiffrer correctement. Essayez de tester avec un fichier statique et voyez si vous pouvez isoler le problème.

+1

Je reçois le fichier de l'appareil et de le tester dans mon PC et il est déchiffré correctement. Par conséquent, il m'a fait penser qu'il n'y a aucun problème à transférer le fichier –

0

Le PC sur lequel vous cryptez le fichier utilise un autre fournisseur de sécurité (principalement SunJCE) et l'android utilise le fournisseur de sécurité "BC" (Bouncy Castle avant 4.2, AndroidOpenssl après 4.2) pour déchiffrer le fichier. D'où le décryptage n'est pas réussi à l'appareil Android. S'il vous plaît utilisez le château gonflable correspondant à votre type de PC à cet endroit Bouncy castle jar download.

Crypter en utilisant le fournisseur "BC" dans le code java du PC.

Cipher cipher = Cipher.getInstance("AES", "BC"); 

Decyption fonctionnera bien à côté Android :)