更改 "Key Owner Name" 的数字签名

Change Digitally signed by "Key Owner Name"

我正在使用 itext5 签署 pdf

MakeSignature.signDetached(appearance, digest, pks, chain, crlList, ocspClient, tsaClient, estimatedSize, subfilter);

生成的文本是:由 JOHN MORGAN 数字签名 + 时间戳

当我编辑 Layer2Text 时

appearance.setLayer2Text("Signed by ");

我只得到:签名(无姓名)。

我需要编辑 "Digitally signed by" 但我想保留密钥所有者的名字

如何获得密钥所有者的姓名?

我尝试使用别名(用于从密钥库获取密钥)但它不是正确的名称(包含特殊字符)

KeyStore keyStore = .....;
PrivateKey pk = (PrivateKey) ks.getKey(alias, null);

谢谢

如果设置第2层文本,则必须设置整个第2层文本,即不仅"Signed by "而且"Signed by JOHN MORGAN + Timestamp"

由于 iText 是开源的,您可以简单地复制和粘贴并操作 iText 代码来生成第 2 层文本。负责的iText代码:

String text;
if (layer2Text == null) {
    StringBuilder buf = new StringBuilder();
    buf.append("Digitally signed by ");
    String name = null;
    X500Name x500name = CertificateInfo.getSubjectFields((X509Certificate)signCertificate);
    if (x500name != null) {
        name = x500name.getField("CN");
        if (name == null)
            name = x500name.getField("E");
    }
    if (name == null)
        name = "";
    buf.append(name).append('\n');
    SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
    buf.append("Date: ").append(sd.format(signDate.getTime()));
    if (reason != null)
        buf.append('\n').append(reasonCaption).append(reason);
    if (location != null)
        buf.append('\n').append(locationCaption).append(location);
    text = buf.toString();
}
else
    text = layer2Text;

(来自PdfSignatureAppearance.getAppearance()

用 getter 和常量替换对受保护成员的引用并从此代码中删除 "Digitally " 会生成可用于您的任务的内容:

StringBuilder buf = new StringBuilder();
buf.append("Signed by ");
String name = null;
X500Name x500name = CertificateInfo.getSubjectFields((X509Certificate)chain[0]);
if (x500name != null) {
    name = x500name.getField("CN");
    if (name == null)
        name = x500name.getField("E");
}
if (name == null)
    name = "";
buf.append(name).append('\n');
SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
buf.append("Date: ").append(sd.format(appearance.getSignDate().getTime()));
if (appearance.getReason() != null)
    buf.append('\n').append("Reason: ").append(appearance.getReason());
if (appearance.getLocation() != null)
    buf.append('\n').append("Location: ").append(appearance.getLocation());
appearance.setLayer2Text(buf.toString());

(来自CreateSignature测试signWithCustomLayer2Text