| 1 | |
| 2 | == Encryption and Decryption test == |
| 3 | |
| 4 | |
| 5 | * Encryption process on Android |
| 6 | |
| 7 | {{{ |
| 8 | ConfigManager.init("jar://jdigidoc.cfg"); |
| 9 | Log.d("despues de:", "ConfigManager.init"); |
| 10 | |
| 11 | /** signed doc object if used */ |
| 12 | SignedDoc m_sdoc; |
| 13 | m_sdoc = null; |
| 14 | |
| 15 | /** encrypted data object if used */ |
| 16 | EncryptedData m_cdoc; |
| 17 | m_cdoc = null; |
| 18 | |
| 19 | String inFile = null, outFile = null; |
| 20 | String certFile = null; |
| 21 | String recipient = null; |
| 22 | String keyName = null; |
| 23 | String carriedKeyName = null; |
| 24 | String sId = null; |
| 25 | |
| 26 | inFile = str; |
| 27 | |
| 28 | |
| 29 | outFile = "cifrado.cdoc"; |
| 30 | |
| 31 | certFile = "/mnt/sdcard/chespirito.crt"; |
| 32 | |
| 33 | |
| 34 | // agregar el destinatario |
| 35 | try { |
| 36 | if (m_cdoc == null){ |
| 37 | Log.d("m_cdoc == null", "-"); |
| 38 | m_cdoc = new EncryptedData(null, null, null, EncryptedData.DENC_XMLNS_XMLENC, EncryptedData.DENC_ENC_METHOD_AES128); |
| 39 | } |
| 40 | Log.d("Adding recipient", certFile); |
| 41 | X509Certificate recvCert = SignedDoc.readCertificate(new File(certFile)); |
| 42 | if (recvCert != null && recipient == null) |
| 43 | recipient = SignedDoc.getCommonName(recvCert.getSubjectDN().getName()); |
| 44 | Log.d("Recipient", recipient); |
| 45 | if (sId == null){ |
| 46 | int n = m_cdoc.getNumKeys() + 1; |
| 47 | sId = "ID" + n; |
| 48 | |
| 49 | } |
| 50 | |
| 51 | EncryptedKey ekey = new EncryptedKey(sId, recipient, EncryptedData.DENC_ENC_METHOD_RSA1_5, keyName, carriedKeyName, recvCert); |
| 52 | m_cdoc.addEncryptedKey(ekey); |
| 53 | |
| 54 | |
| 55 | }catch(Exception e){ |
| 56 | Log.d("Error adding EncryptedKey: ", e.getMessage()); |
| 57 | Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); |
| 58 | } |
| 59 | |
| 60 | // ejecutar el cifrado |
| 61 | try { |
| 62 | Log.d("Encrypting file:", inFile + " to: " + outFile); |
| 63 | File fIn = new File(inFile); |
| 64 | // create a ddoc intermediate file |
| 65 | m_sdoc = new SignedDoc(SignedDoc.FORMAT_DIGIDOC_XML, SignedDoc.VERSION_1_3); |
| 66 | |
| 67 | Log.d("Encrypting file:", "paso new SignedDoc"); |
| 68 | |
| 69 | DataFile df = m_sdoc.addDataFile(new File(inFile), SignedDoc.xmlns_digidoc13, DataFile.CONTENT_EMBEDDED_BASE64); |
| 70 | |
| 71 | Log.d("Encrypting file:", "paso addDataFile"); |
| 72 | |
| 73 | |
| 74 | byte[] data = SignedDoc.readFile(new File(inFile)); |
| 75 | |
| 76 | Log.d("Encrypting file:", "paso readFile"); |
| 77 | |
| 78 | df.setBase64Body(data); |
| 79 | |
| 80 | Log.d("Encrypting file:", "paso setBase64Body"); |
| 81 | |
| 82 | byte[] inData = m_sdoc.toXML().getBytes("UTF-8"); |
| 83 | |
| 84 | Log.d("Encrypting file:", "paso toXML()"); |
| 85 | |
| 86 | Log.d("Encrypting file", "Content: " + inFile + " size: " + data.length); |
| 87 | Log.d("Encrypting file", "DF: " + new String(inData)); |
| 88 | |
| 89 | m_cdoc.setData(inData); |
| 90 | m_cdoc.setDataStatus(EncryptedData.DENC_DATA_STATUS_UNENCRYPTED_AND_NOT_COMPRESSED); |
| 91 | m_cdoc.addProperty(EncryptedData.ENCPROP_FILENAME, inFile + ".ddoc"); |
| 92 | m_cdoc.setMimeType(EncryptedData.DENC_ENCDATA_TYPE_DDOC); |
| 93 | StringBuffer sb = new StringBuffer(); |
| 94 | sb.append(fIn.getName()); |
| 95 | sb.append("|"); |
| 96 | sb.append(new Long(fIn.length()).toString() + " B|"); |
| 97 | sb.append("application/unknown|"); |
| 98 | sb.append("/" + fIn.getName()); |
| 99 | m_cdoc.addProperty(EncryptedData.ENCPROP_ORIG_FILE, sb.toString()); |
| 100 | //m_cdoc.addProperty(EncryptedData.ENCPROP_ORIG_SIZE, new Long(inData.length).toString()); |
| 101 | |
| 102 | int nCompressOption = 0; |
| 103 | |
| 104 | m_cdoc.encrypt(nCompressOption); |
| 105 | |
| 106 | |
| 107 | // genera el archivo cifrado en /data/data/ve.gob.cenditel/files |
| 108 | FileOutputStream fos = openFileOutput(outFile, Context.MODE_PRIVATE); |
| 109 | |
| 110 | Log.d("Encrypting file", "antes de escribir archivo " + outFile); |
| 111 | |
| 112 | fos.write(m_cdoc.toXML()); |
| 113 | |
| 114 | Log.d("Encrypting file", "despues de escribir archivo " + outFile); |
| 115 | |
| 116 | fos.close(); |
| 117 | |
| 118 | Log.d("Encrypting file", "despues de cerrar archivo " + outFile); |
| 119 | |
| 120 | Toast.makeText(getApplicationContext(), "Cifrado correctamente: " + outFile, Toast.LENGTH_SHORT).show(); |
| 121 | |
| 122 | |
| 123 | } catch(Exception e) { |
| 124 | Log.d("Error encrypting file: ", inFile + " - " + e.getMessage()); |
| 125 | e.printStackTrace(System.err); |
| 126 | Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | }}} |
| 131 | |
| 132 | |
| 133 | |
| 134 | * Decryption process with jdigidocutil (encrypted file through Android) |
| 135 | |
| 136 | {{{ |
| 137 | aaraujo@moe:~/desarrollo/andriod/pruebas/jdigidoc-3.7/jdigidoc$ java -jar jdigidocutil-\$\{version\}.jar -cdoc-in /tmp/cifrado.cdoc -cdoc-decrypt-pkcs12-sk ~/desarrollo/chespirito.p12 chespirito PKCS12 /tmp/salida.pdf |
| 138 | JDigiDoc - 3.6.0.1 |
| 139 | Reading config file: jar://jdigidoc.cfg |
| 140 | Reading encrypted file: /tmp/cifrado.cdoc |
| 141 | Decrypting to: /tmp/salida.pdf |
| 142 | Using recipient: 0 |
| 143 | SAXDigiDocFactory::readSignedDocOfType |
| 144 | Start reading ddoc/bdoc from file: /tmp/salida.pdf |
| 145 | Start Element: SignedDoc lname: uri: |
| 146 | Start Element: DataFile lname: uri: |
| 147 | Start collecting digest |
| 148 | Allocating buf: 122556 Element: DataFile lname: uri: |
| 149 | Attr: ContentType ='EMBEDDED_BASE64' |
| 150 | Attr: Filename ='LSMDFE.pdf' |
| 151 | Attr: Id ='D0' |
| 152 | Attr: MimeType ='http://www.sk.ee/DigiDoc/v1.3.0#' |
| 153 | Attr: Size ='61278' |
| 154 | Attr: xmlns ='http://www.sk.ee/DigiDoc/v1.3.0#' |
| 155 | Canonicalized: '<DataFile xmlns="http://www.sk.ee/DigiDoc/v1.3.0#" ContentType="EMBEDDED_BASE64" Filename="LSMDFE.pdf" Id="D0" MimeType="http://www.sk.ee/DigiDoc/v1.3.0#" Size="61278">' |
| 156 | End Element: DataFile collect: 1 |
| 157 | Digest: D0 - 5hmr2x1Q4PKrjbmkWy7K+jzYL/o= size: 61278 |
| 158 | End Element: SignedDoc collect: 0 |
| 159 | JDigiDoc end, time: 3 sec result: success |
| 160 | }}} |
| 161 | |
| 162 | |
| 163 | |