An Bug of iText and iTextSharp

Somebody sent me a encrypted PDF to ask me help decrypt, I tried pdfcrypt, it got error. and when used pdfcrack, got the following warning messages,

WARNING: O-String != 32 Bytes: 33
WARNING: U-String != 32 Bytes: 33

After studied the source code of iTextSharp, I noticed it is a bug of iTextSharp in SetupGlobalEncryptionKey method of the PdfEncryption class,
misused the this.ownerKey and ownerKey and we can only find this bug when the PDF has strange length of ownerkey/userkey, for example 33, not 32.

this.documentID = documentID;
//this.ownerKey = ownerKey;
Array.Copy(ownerKey, 0, this.ownerKey, 0, 32);
this.permissions = permissions;
// use variable keylength
mkey = new byte[keyLength / 8];

//fixed by ujihara in order to follow PDF refrence
md5.Reset();
md5.BlockUpdate(userPad, 0, userPad.Length);
//md5.BlockUpdate(ownerKey, 0, ownerKey.Length);
md5.BlockUpdate(this.ownerKey, 0, this.ownerKey.Length);

Then I review iText 5.1.1 and find it has the same bug:

this.documentID = documentID;
//this.ownerKey = ownerKey;
System.arraycopy(ownerKey, 0, this.ownerKey, 0, 32);
this.permissions = permissions;
// use variable keylength
mkey = new byte[keyLength / 8];

// fixed by ujihara in order to follow PDF reference
md5.reset();
md5.update(userPad);
//md5.update(ownerKey);
md5.update(this.ownerKey);

Be Sociable, Share!

PdfCrypt got the first donation

Thanks Richard Ramey for his donation, it is the first 10 dollar for the PdfCrypt.
BTW, If you do not want to download PdfCrypt, or you do not have windows system, but still want to remove the PDF Password(either user password or owner password) and stricts(print, copy and so on), please feel free to try my free Remove PDF Password Online solution hosted on Google App Engine(GAE/J), it bases on modified iText 2.1.7.

Be Sociable, Share!

How to run iText on Google App Engine

Important update:

The last version itext-gae hosts at GitHub now, with many changes,

  • update to iText 4.2.0
  • fixs many bugs
  • supports Graphics2D now

Have applied Google App Engine (aka GAE) for a long time, recently I thought maybe I can build some PDF manipulation services on GAE, and I am familiar with iText, so I decide to try iText on GAE.

after some search, I got the following solution,

iText has dependencies on certain Java classes (java.awt.Color, java.nio.MappedByteBuffer etc.) which are ** not ** allowed by the Google
App Engine runtime .
Google App Engine throws an exception e.g “java.lang.NoClassDefFoundError: java.nio.MappedByteBuffer is a restricted class. Please see the Google App
Engine developer’s guide for more details.” is thrown for the unsupported class(es).
- Would it be possible to remove the dependency by iText on these Java classes, so as to enable the library to work within Google AppEngine?
See attached (very rough) Subversion patch file, for how I quickly removed some of the dependencies, so as to get iText to process a PDF form within
Google App Engine.

You can download the patch here,iText patch for GoogleAppEngine support.

And if you are very lazy, you can try directly download the modified version iText-2.1.7 (both jar and source)from here,

notice, iText-2.1.7-gae.jar forgot cmap_info.txt and glyphlist.txt, so if you directly use iText-2.1.7-gae.jar, you need add cmap_info.txt and glyphlist.txt to it first.

I have write two little demos and deployed them on my GAE home page, feel free to have a look here,
http://rubypdf.appspot.com

reference,
http://code.google.com/appengine/docs/java/jrewhitelist.html
iTextをGoogle App Engineから利用できるiText-gaeを公開しました
http://1t3xt.info/examples/browse/index.php

P.S.
Dec 21,2009, Release my first Google App Engine Application,Remove PDF Password Online, for introduce, please visit here.

Be Sociable, Share!