我试图在Android上使用iText签署PDF文档。证书已包含在模拟器中并已激活。这是我使用的库:
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.itextpdf:itextg:5.5.9'
testCompile 'junit:junit:4.12'
compile files('libs/itext-licensekey-1.0.4.jar')
compile 'com.madgag.spongycastle:core:1.54.0.0'
compile 'com.madgag.spongycastle:prov:1.54.0.0'
compile 'com.madgag.spongycastle:pkix:1.54.0.0'
compile 'com.madgag.spongycastle:pg:1.54.0.0'
以下是我用来签署现有pdf文档的类:
public class SignPDF {
static {
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
}
public void sign(String src, String dest,
Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider,
MakeSignature.CryptoStandard subfilter, String reason, String location)
throws GeneralSecurityException, IOException, DocumentException {
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason(reason);
appearance.setLocation(location);
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature =
new PrivateKeySignature(pk, digestAlgorithm, provider);
MakeSignature.signDetached(appearance, digest, signature, chain,
null, null, null, 0, subfilter);
}
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static final int REQUEST_EXTERNAL_STORAGE = 1;
public static final String SRC = Environment.getExternalStorageDirectory() + File.separator + "testdsign_1.pdf"; // consider the file to be created earlier
public static final String DEST = Environment.getExternalStorageDirectory() + File.separator + "testdsign_2.pdf";
public static void signat(Activity a)
throws IOException, GeneralSecurityException {
int permission = ActivityCompat.checkSelfPermission(a, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
a,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
String path = Environment.getExternalStorageDirectory() + File.separator + "keystore.p12";
char[] pass = "123456".toCharArray();
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName());
ks.load(new FileInputStream(path), pass);
String alias = (String) ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, pass);
Certificate[] chain = ks.getCertificateChain(alias);
SignPDF app = new SignPDF();
try {
app.sign(SRC, DEST, chain, pk, DigestAlgorithms.SHA256, provider.getName(),
MakeSignature.CryptoStandard.CMS, "Test", "Ghent");
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
最后,错误堆栈
java.lang.NoSuchMethodError: No direct method <init>(I)V in class Lorg/spongycastle/asn1/ASN1Integer; or its super classes (declaration of 'org.spongycastle.asn1.ASN1Integer' appears in /data/data/dsign.example.com.dsigntest/files/instant-run/dex/slice-core-1.54.0.0_016eb8590d95a10a91a6367c6ca7de97121683a4-classes.dex)
at com.itextpdf.text.pdf.security.PdfPKCS7.getEncodedPKCS7(PdfPKCS7.java:826)
at com.itextpdf.text.pdf.security.MakeSignature.signDetached(MakeSignature.java:154)
at dsign.example.com.dsigntest.SignPDF.sign(SignPDF.java:56)
at dsign.example.com.dsigntest.SignPDF.signat(SignPDF.java:91)
at dsign.example.com.dsigntest.MainActivity.onCreate(MainActivity.java:59)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
问题的原因是什么?感谢您提供任何帮助。
答案 0 :(得分:4)
您使用的是BouncyCastle 1.54版。 iTextG 5.5。*需要SpongyCastle版本1.47.0.2(同时iText 5.5。*需要BouncyCastle版本1.49)。
推荐的方法是将iTextG作为依赖项添加到Maven / Gradle / ...配置中,您的构建工具将自动引入正确版本的SpongyCastle,目前为1.47.0.2。无需手动搜索依赖项。依赖性解决方案是Android上解决的问题。
编辑:
有些人显然不使用Maven构建工具(或Gradle),而是在http://search.maven.org上手动搜索jar。如果你这样做,并盲目地搜索“spongycastle”,那么你只能找到错误的版本。
正如人们可以在iTextG pom.xml
中看到的那样:
<dependencies>
<dependency>
<groupId>com.madgag</groupId>
<artifactId>scprov-jdk15on</artifactId>
<version>1.47.0.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.madgag</groupId>
<artifactId>scpkix-jdk15on</artifactId>
<version>1.47.0.2</version>
<optional>true</optional>
</dependency>
...
</dependencies>
我再说一遍,这些是iText 5.5的正确依赖关系。*:
com.madgag
scprov-jdk15on
和scpkix-jdk15on
1.47.0.2
对于iText 5.5,这些依赖关系错误。*:
com.madgag.spongycastle
prov
和pkix
[1.50,)
如果在确定使用正确版本的SpongyCastle之后,您仍有问题,那么您必须提出一个新问题。在您的问题中,请包含以下信息:
mvn dependency:tree -Dverbose