scientifichackers / flutter_pdf_viewer

A native PDF viewer for flutter.
MIT License
107 stars 40 forks source link

Can't decrypt encrypted(xor) file. #32

Open hillol3786 opened 5 years ago

hillol3786 commented 5 years ago
  1. I have encrypted a pdf in python.
  2. I need to decrypt and view it in flutter app and so I used this package.

my python 3 script:

from itertools import cycle

def xor_crypt(data, key):
    return ''.join(chr(a ^ ord(b)) for (a, b) in zip(data, cycle(key)))
with open('math.pdf', 'rb') as encry, open('xor_math.pdf', 'w') as decry:
    decry.write(xor_crypt(encry.read(), 'abcd'))
    encry.close()
    decry.close()

Normal File: math.pdf

Encrypted File: xor_math.pdf Encryption key : 'abcd'

I cloned the example app and pasted the encrypted file(xor_math.pdf) in asset and changed the file name in code as shown below.

image

I got this error

D/PluginScaffold(31704): invoke { channel: com.pycampers.flutter_pdf_viewer, method: launchPdfActivity(), args: {pageSnap=false, enableImmersive=false, src=assets/xor_math.pdf, pageFling=false, xorDecryptKey=abcd, mode=fromAsset, password=null, autoSpacing=false, initialPage=null, pages=null, pdfId=dd68e353b2390617cff7bf7776677545b583b9ce, enableSwipe=true, autoPlay=false, videoPages={8={mode=fromAsset, src=assets/xor_buck_bunny.mp4, xorDecryptKey=abcd}, 9={mode=fromAsset, src=assets/xor_buck_bunny.mp4, xorDecryptKey=abcd}}, forceLandscape=false, nightMode=false, swipeHorizontal=false} }
I/Timeline(31704): Timeline: Activity_launch_request time:264188741 intent:Intent { cmp=com.pycampers.flutterpdfviewerexample/com.pycampers.flutterpdfviewer.PdfActivity (has extras) }
W/ActivityThread(31704): handleWindowVisibility: no activity for token android.os.BinderProxy@8f4b2ab
D/com.shockwave.pdfium.PdfiumCore(31704): Starting PdfiumAndroid 1.9.0
D/FlutterPdfViewer(31704): loading encrypted pdf from assets { flutter_assets/assets/xor_math.pdf }...
I/dfviewerexampl(31704): Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I/flutter (31704): analyticsCallback: { pdfId: dd68e353b2390617cff7bf7776677545b583b9ce, pageIndex: 0, paused: false }
D/jniPdfium(31704): Init FPDF library
D/jniPdfium(31704): Destroy FPDF library
D/FlutterPdfViewer(31704): encountered error: java.io.IOException: cannot create document: File not in PDF format or corrupted.
W/System.err(31704): java.io.IOException: cannot create document: File not in PDF format or corrupted.
W/System.err(31704):    at com.shockwave.pdfium.PdfiumCore.nativeOpenMemDocument(Native Method)
W/System.err(31704):    at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:150)
W/System.err(31704):    at com.github.barteksc.pdfviewer.source.ByteArraySource.createDocument(ByteArraySource.java:35)
W/System.err(31704):    at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:49)
W/System.err(31704):    at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
W/System.err(31704):    at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err(31704):    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err(31704):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err(31704):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err(31704):    at java.lang.Thread.run(Thread.java:764)
D/PluginScaffold(31704): piping exception to flutter (java.io.IOException)
E/flutter (31704): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(java.io.IOException, cannot create document: File not in PDF format or corrupted., java.io.IOException: cannot create document: File not in PDF format or corrupted.
E/flutter (31704):  at com.shockwave.pdfium.PdfiumCore.nativeOpenMemDocument(Native Method)
E/flutter (31704):  at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:150)
E/flutter (31704):  at com.github.barteksc.pdfviewer.source.ByteArraySource.createDocument(ByteArraySource.java:35)
E/flutter (31704):  at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:49)
E/flutter (31704):  at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
E/flutter (31704):  at android.os.AsyncTask$2.call(AsyncTask.java:333)
E/flutter (31704):  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/flutter (31704):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/flutter (31704):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/flutter (31704):  at java.lang.Thread.run(Thread.java:764)
E/flutter (31704): )
E/flutter (31704): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (31704): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter (31704): <asynchronous suspension>
E/flutter (31704): #2      PdfViewer._launchPdfActivity (package:flutter_pdf_viewer/flutter_pdf_viewer.dart:263:19)
E/flutter (31704): <asynchronous suspension>
E/flutter (31704): #3      PdfViewer.loadAsset (package:flutter_pdf_viewer/flutter_pdf_viewer.dart:314:18)
E/flutter (31704): <asynchronous suspension>
E/flutter (31704): #4      PdfLoaderSectionState.onPressed (package:flutter_pdf_viewer_example/main.dart:112:25)
E/flutter (31704): <asynchronous suspension>
E/flutter (31704): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter (31704): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter (31704): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (31704): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter (31704): #9      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:312:7)
E/flutter (31704): #10     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (31704): #11     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter (31704): #12     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (31704): #13     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (31704): #14     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (31704): #15     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (31704): #16     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (31704): #17     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (31704): #18     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (31704): #19     _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter (31704): #20     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
E/flutter (31704): 
I/flutter (31704): analyticsCallback: { pdfId: dd68e353b2390617cff7bf7776677545b583b9ce, pageIndex: 0, paused: true }
I/System.out(31704): atExit!
devxpy commented 5 years ago

Looks like your encryption mechanism is broken. PDF is a binary format, not a text format. But your encryption mechanism outputs a text file.

Use this function instead.

def xor_encrypt(bytes_data: bytes, secret_key: str) -> bytes:
    return bytes((x ^ y) for (x, y) in zip(bytes_data, cycle(secret_key.encode())))