gpt4 book ai didi

java - 使用自签名证书生成签名

转载 作者:行者123 更新时间:2023-11-29 06:26:58 28 4
gpt4 key购买 nike

我有以下使用自签名证书生成签名的示例代码

public static String generateSignature(String data) throws Exception {

System.out.println("@@inside generateSignature: " + data);

String signature;

String jksFilepath = "E:\\test.jks";

try {
// Adding Security Provider for PKCS 12
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// Setting password for the e-Token

// logging into token
ks = KeyStore.getInstance("jks");


FileInputStream fileInputStream = new FileInputStream(jksFilepath);

// Loading Keystore
// System.out.println("loading keystore");
ks.load(fileInputStream, JKSPassword);
Enumeration<String> e = ks.aliases();

while (e.hasMoreElements()) {
alias = e.nextElement();
// System.out.println("Alias of the e-Token : "+ alias);

UserCert = (X509Certificate) ks.getCertificate(alias);

UserCertPubKey = (PublicKey) ks.getCertificate(alias).getPublicKey();

// System.out.println("loading Private key");
UserCertPrivKey = (PrivateKey) ks.getKey(alias, JKSPassword);
}

// Method Call to generate Signature
signature = MakeSignature(data);

return signature;

} catch (Exception e) {
e.printStackTrace();
System.out.println("generateSignature" + e.getCause());
throw new Exception();
}

}

private static String MakeSignature(String data) {

System.out.println("@@inside MakeSignature...");

try {
PrivateKey privateKey = (PrivateKey) ks.getKey(alias, JKSPassword);
myPubCert = (X509Certificate) ks.getCertificate(alias);
Store certs = new JcaCertStore(Arrays.asList(myPubCert));

CMSSignedDataGenerator generator = new CMSSignedDataGenerator();

generator.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider("BC").build("SHA256withRSA", privateKey, myPubCert));

generator.addCertificates(certs);

CMSTypedData data1 = new CMSProcessableByteArray(data.getBytes());

CMSSignedData signed = generator.generate(data1, true);

BASE64Encoder encoder = new BASE64Encoder();

String signedContent = encoder.encode((byte[]) signed.getSignedContent().getContent());

String envelopedData = encoder.encode(signed.getEncoded());

return envelopedData;
} catch (Exception e) {
e.printStackTrace();
System.out.println("MakeSignature ==" + e.getCause());
return "";
}
}

还有一些相关的功能,但为了简单起见,我没有添加它。

现在我想用 PHP 做完全相同的事情。

JKS 不适用于 PHP 作为 Java 的 keystore 。

我尝试了具有不同加密方法集的 open_ssl 函数。但是我没有得到与我通过此 Java 代码获得的结果相同的预期结果(“不相同”是关于比特率和生成签名的长度)。

有人可以帮我用 PHP 实现相同的签名生成吗?

最佳答案

我觉得PHP官方文档写的很清楚:http://php.net/manual/en/function.openssl-csr-new.php

示例 #1 创建自签名证书

<?php
$dn = array(
"countryName" => "GB",
"stateOrProvinceName" => "Somerset",
"localityName" => "Glastonbury",
"organizationName" => "The Brain Room Limited",
"organizationalUnitName" => "PHP Documentation Team",
"commonName" => "Wez Furlong",
"emailAddress" => "wez@example.com"
);

// Generate a new private (and public) key pair
$privkey = openssl_pkey_new(array(
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
));

// Generate a certificate signing request
$csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256'));

// Generate a self-signed cert, valid for 365 days
$x509 = openssl_csr_sign($csr, null, $privkey, $days=365, array('digest_alg' => 'sha256'));

// Save your private key, CSR and self-signed cert for later use
openssl_csr_export($csr, $csrout) and var_dump($csrout);
openssl_x509_export($x509, $certout) and var_dump($certout);
openssl_pkey_export($privkey, $pkeyout, "mypassword") and var_dump($pkeyout);

// Show any errors that occurred here
while (($e = openssl_error_string()) !== false) {
echo $e . "\n";
}

然后你可以调用openssl_sign: http://php.net/manual/en/function.openssl-sign.php ,使用生成的私钥进行签名。

如果你想在PHP代码中使用Java(JKS)的key,你应该先导出key,然后使用PHP函数加载key。

关于java - 使用自签名证书生成签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53321299/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com