Changeset f32914c in dispositivos_moviles for TibisayMovil/src/ve/gob/cenditel/tibisaymovil/PKCS12ToDecryptActivity.java
- Timestamp:
- Nov 1, 2013, 9:47:31 AM (11 years ago)
- Branches:
- master
- Children:
- c14b8d2
- Parents:
- 31cf7cf
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TibisayMovil/src/ve/gob/cenditel/tibisaymovil/PKCS12ToDecryptActivity.java
r647bb1f rf32914c 13 13 import java.util.Collections; 14 14 import java.util.Date; 15 import java.util.Vector; 16 17 import com.lowagie.text.pdf.PdfReader; 15 18 16 19 import ee.sk.digidoc.DataFile; … … 22 25 import ee.sk.xmlenc.EncryptedData; 23 26 import ee.sk.xmlenc.factory.EncryptedDataParser; 24 25 27 import ve.gob.cenditel.tibisaymovil.R; 26 28 import android.app.Activity; 27 29 import android.app.AlertDialog; 28 30 import android.app.Dialog; 31 import android.app.ProgressDialog; 29 32 import android.content.Context; 30 33 import android.content.DialogInterface; … … 32 35 import android.graphics.drawable.Drawable; 33 36 import android.net.Uri; 37 import android.os.AsyncTask; 34 38 import android.os.Bundle; 35 39 import android.os.Environment; 40 import android.os.Looper; 36 41 import android.util.Log; 37 42 import android.view.LayoutInflater; … … 655 660 AlertDialog dialogDetails = null; 656 661 657 AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);662 final AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this); 658 663 dialogbuilder.setTitle(R.string.title_pkcs12_password); 659 664 dialogbuilder.setView(dialogview); … … 669 674 public void onClick(View v) { 670 675 671 Toast.makeText(PKCS12ToDecryptActivity.this," Password : "+ password.getText().toString(), Toast.LENGTH_LONG).show();676 672 677 673 678 // asignar la contrasena 674 679 pkcs12Password = password.getText().toString(); 680 if (!pkcs12Password.isEmpty()){ 681 Toast.makeText(PKCS12ToDecryptActivity.this,"PKCS12 Password : "+ password.getText().toString(), Toast.LENGTH_LONG).show(); 682 683 password.setText(""); 684 685 // ejecutar el proceso de descifrado. 686 decryptFile(fileToDecrypt, pkcs12File, pkcs12Password); 687 688 689 } 675 690 676 691 // ejecutar el proceso de descifrado. 677 decryptFile(fileToDecrypt, pkcs12File, pkcs12Password);692 //decryptFile(fileToDecrypt, pkcs12File, pkcs12Password); 678 693 679 694 // borrar el contenido de pkcs12Password … … 691 706 692 707 /** 693 * Crea dialogo para obtener la contrasena del archivo P12708 * Descifra el archivo pasado como argumento con el archivo P12 694 709 * 695 * @return Dialog710 * @return void 696 711 */ 697 private void decryptFile( String fileToDecrypt, String pkcs12File,String pkcs12Password) {712 private void decryptFile(final String fileToDecrypt, final String pkcs12File, final String pkcs12Password) { 698 713 699 714 Toast.makeText(getApplicationContext(), "Decrypting file: " + fileToDecrypt, Toast.LENGTH_SHORT).show(); 700 715 701 716 // ----------------------------- 717 AsyncTask<Void, Void, ArrayList<String>> task = new AsyncTask<Void, Void, ArrayList<String>>() { 718 719 private ProgressDialog pd; 720 721 @Override 722 protected void onPreExecute() { 723 724 pd = new ProgressDialog(PKCS12ToDecryptActivity.this); 725 pd.setTitle("Descifrando archivo"); 726 pd.setMessage(getString(R.string.por_favor_epsere)); 727 pd.setCancelable(false); 728 pd.setIndeterminate(true); 729 pd.show(); 730 731 } 732 733 @Override 734 protected ArrayList<String> doInBackground(Void... arg0) { 735 736 /* 737 try { 738 Thread.sleep(3000); 739 } catch(InterruptedException ex) { 740 Thread.currentThread().interrupt(); 741 } 742 */ 743 744 // para solventar error: 745 // Can't create handler inside thread that has not called Looper.prepare() 746 Looper.prepare(); 747 748 // arreglo para almacenar resultado de la operacion 749 ArrayList<String> resultArray = new ArrayList<String>(); 750 resultArray.clear(); 751 752 ConfigManager.init("jar://jdigidoc.cfg"); 753 // signed doc object if used 754 SignedDoc m_sdoc; 755 m_sdoc = null; 756 757 // encrypted data object if used 758 EncryptedData m_cdoc; 759 m_cdoc = null; 760 761 String outFile = null, keystoreFile = null, keystorePasswd = null, keystoreType="PKCS12"; 762 763 keystoreFile = pkcs12File; 764 765 keystorePasswd = pkcs12Password; 766 767 Log.d("Reading encrypted file: ", fileToDecrypt); 768 769 try { 770 EncryptedDataParser dencFac = ConfigManager.instance().getEncryptedDataParser(); 771 m_cdoc = dencFac.readEncryptedData(fileToDecrypt); 772 } catch(Exception ex) { 773 System.err.println("ERROR: reading encrypted file: " + fileToDecrypt + " - " + ex); 774 Log.d("ERROR: reading encrypted file: ", ex.getMessage()); 775 ex.printStackTrace(System.err); 776 Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show(); 777 778 //showDialog("Error:", ex.getMessage(), false); 779 //return null; 780 781 resultArray.add("false"); 782 resultArray.add("Error"); 783 resultArray.add(ex.getMessage()); 784 return resultArray; 785 } 786 787 int nKey = -1; 788 789 try{ 790 791 Pkcs12SignatureFactory p12fac = new Pkcs12SignatureFactory(); 792 p12fac.init(); 793 System.out.println("p12fac.init " ); 794 795 p12fac.load(keystoreFile, keystoreType, keystorePasswd); 796 System.out.println("p12fac.load()"); 797 798 X509Certificate cert = p12fac.getAuthCertificate(0, keystorePasswd); 799 System.out.println("p12fac.getAuthCertificate: " + nKey); 800 801 nKey = m_cdoc.getRecvIndex(cert); 802 System.out.println("Using recipient: " + nKey); 803 if (nKey == -1) { 804 throw(new ArrayIndexOutOfBoundsException("")); 805 } 806 807 808 } // fin del try interno 809 catch(DigiDocException ex){ 810 System.err.println("ERROR: finding cdoc recipient: " + ex); 811 812 Log.d("Error finding cdoc recipient: ", ex.getMessage()); 813 ex.printStackTrace(System.err); 814 Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show(); 815 816 817 818 if (ex.getCode() == DigiDocException.ERR_TOKEN_LOGIN) { 819 820 // el pasword del PKCS12 no es correcto 821 //showDialog(getResources().getString(R.string.msg_encryption_error), "password incorrecto", false); 822 //return null; 823 824 resultArray.add("false"); 825 resultArray.add(getResources().getString(R.string.msg_encryption_error)); 826 resultArray.add("contraseña incorrecta."); 827 return resultArray; 828 829 830 }else{ 831 //showDialog(getResources().getString(R.string.msg_encryption_error), ex.getMessage(), false); 832 //return null; 833 834 resultArray.add("false"); 835 resultArray.add(getResources().getString(R.string.msg_encryption_error)); 836 resultArray.add(ex.getMessage()); 837 return resultArray; 838 } 839 840 } catch(ArrayIndexOutOfBoundsException ex) { 841 //showDialog(getResources().getString(R.string.msg_encryption_error), 842 // getResources().getString(R.string.error_decrypting_file_index_out_of_bounds), false); 843 //return null; 844 845 resultArray.add("false"); 846 resultArray.add(getResources().getString(R.string.msg_encryption_error)); 847 resultArray.add(getResources().getString(R.string.error_decrypting_file_index_out_of_bounds)); 848 return resultArray; 849 850 } 851 852 System.err.println("**** antes de ejecutar operacion m_cdoc.decryptPkcs12(nKey, keystoreFile, keystorePasswd, keystoreType)"); 853 // ejecutar la operacion de descifrado: 854 try { 855 856 m_cdoc.decryptPkcs12(nKey, keystoreFile, keystorePasswd, keystoreType); 857 858 String [] absolutePathOriginalFile = fileToDecrypt.split(".cdoc"); 859 //String fileName = split[0]; 860 outFile = absolutePathOriginalFile[0]; 861 862 String [] path = absolutePathOriginalFile[0].split("/"); 863 864 String originalFileName = path[path.length-1]; 865 866 FileOutputStream fos = new FileOutputStream( outFile ); 867 868 Log.d("Decrypting file", "antes de escribir archivo " + outFile); 869 870 fos.write(m_cdoc.getData()); 871 872 Log.d("Decrypting file", "despues de escribir archivo " + outFile); 873 874 fos.close(); 875 876 Log.d("Decrypting file", "despues de cerrar archivo " + outFile); 877 878 DigiDocFactory digFac = ConfigManager.instance().getDigiDocFactory(); 879 Log.d("Decrypting file", "despues ConfigManager.instance().getDigiDocFactory()"); 880 881 File file = new File(outFile); 882 InputStream selectedFile = null; 883 selectedFile = new BufferedInputStream(new FileInputStream(file)); 884 885 m_sdoc = digFac.readSignedDocFromStreamOfType(selectedFile, false); 886 Log.d("----", "leyo el ddoc"); 887 Toast.makeText(getApplicationContext(), "leyó el ddoc", Toast.LENGTH_SHORT).show(); 888 889 selectedFile.close(); 890 891 // ******* 892 Log.d("Decrypting file", "antes de m_sdoc.getDataFile(0)"); 893 DataFile df = m_sdoc.getDataFile(0); 894 Log.d("Decrypting file", "despues de m_sdoc.getDataFile(0)"); 895 896 String decryptedDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + 897 getResources().getString(R.string.app_name) + "/" + 898 getResources().getString(R.string.decrypted_dir_files) + "/"; 899 900 decryptedFile = decryptedDir+originalFileName; 901 902 fos = new FileOutputStream(decryptedFile); 903 InputStream is = df.getBodyAsStream(); 904 905 if(is == null) { 906 System.err.println("DataFile has no data!"); 907 //showDialog(getResources().getString(R.string.msg_encryption_error), 908 // "DataFile has no data!", false); 909 //return null; 910 911 912 resultArray.add("false"); 913 resultArray.add(getResources().getString(R.string.msg_encryption_error)); 914 resultArray.add("DataFile has no data!"); 915 return resultArray; 916 917 } 918 byte[] data = new byte[4096]; 919 int n = 0, m = 0; 920 while((n = is.read(data)) > 0) { 921 fos.write(data, 0, n); 922 m += n; 923 } 924 fos.close(); 925 is.close(); 926 927 // borrar el archivo ddoc 928 File ddocFile = new File(outFile); 929 ddocFile.delete(); 930 931 932 //Toast.makeText(getApplicationContext(), "Descifrado correctamente: " + decryptedFile, Toast.LENGTH_SHORT).show(); 933 //showDialog("Información:", "Archivo descifrado exitosamente.", true); 934 935 resultArray.add("true"); 936 resultArray.add("Información:"); 937 resultArray.add("Archivo descifrado exitosamente."); 938 939 940 941 } catch (DigiDocException e) { 942 943 //showDialog(getResources().getString(R.string.msg_encryption_error), 944 // e.getMessage(), false); 945 //return null; 946 947 resultArray.add("false"); 948 resultArray.add(getResources().getString(R.string.msg_encryption_error)); 949 resultArray.add(e.getMessage()); 950 return resultArray; 951 952 } 953 954 catch (FileNotFoundException e) { 955 //showDialog(getResources().getString(R.string.msg_encryption_error), 956 // e.getMessage(), false); 957 //return null; 958 959 resultArray.add("false"); 960 resultArray.add(getResources().getString(R.string.msg_encryption_error)); 961 resultArray.add(e.getMessage()); 962 return resultArray; 963 964 } catch (IOException e) { 965 //showDialog(getResources().getString(R.string.msg_encryption_error), 966 // e.getMessage(), false); 967 //return null; 968 969 resultArray.add("false"); 970 resultArray.add(getResources().getString(R.string.msg_encryption_error)); 971 resultArray.add(e.getMessage()); 972 return resultArray; 973 974 } 975 976 return resultArray; 977 } 978 @Override 979 protected void onPostExecute(ArrayList<String> result) { 980 981 if (result.get(0).equals("false")){ 982 showDialog(result.get(1), result.get(2), false); 983 }else{ 984 showDialog(result.get(1), result.get(2), true); 985 } 986 987 988 pd.dismiss(); 989 990 } 991 992 }; 993 994 task.execute((Void[])null); 995 996 997 // ----------------------------- 998 /* 702 999 ConfigManager.init("jar://jdigidoc.cfg"); 703 / ** signed doc object if used */1000 // signed doc object if used 704 1001 SignedDoc m_sdoc; 705 1002 m_sdoc = null; 706 1003 707 / ** encrypted data object if used */1004 // encrypted data object if used 708 1005 EncryptedData m_cdoc; 709 1006 m_cdoc = null; … … 875 1172 return; 876 1173 } 877 1174 */ 878 1175 879 1176
Note: See TracChangeset
for help on using the changeset viewer.