一部の Java コードを C# に移植しようとしていますが、データの署名に関して問題が発生しています。受け取ったエラーは「内部証明書チェーン エラーが発生しました」です。
コードは私のマシンの Java では正常に実行されますが、C# では実行されません。
以下は、ComputeSignature で例外をスローする C# コードです。
//Import the certificate
var certificate2Collection = new X509Certificate2Collection();
certificate2Collection.Import(certificateFilePath, "**password**", X509KeyStorageFlags.DefaultKeySet);
//Only one cert in pks file
var cert2 = certificate2Collection[0];
//create data to be signed
var time = DateTime.Now.ToString("dd/MM/yyyy") + " 00:00:00";
var modifiedTimestamp = userName + "|" + time;
var dataToSign = Encoding.ASCII.GetBytes(modifiedTimestamp);
var content = new ContentInfo(dataToSign);
var signedMessage = new SignedCms(content);
var cmsSigner = new CmsSigner(cert2);
signedMessage.**ComputeSignature**(cmsSigner);
byte[] signedBytes = signedMessage.Encode();
string signedData = Convert.ToBase64String(signedBytes);
動作するJavaコードは次のとおりです。
final String time = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date());
final String timestamp = time + " 00:00:00";
// Create the data to be signed
final String modifiedTimestamp = userName + "|" + timestamp;
final byte[] ba = modifiedTimestamp.getBytes();
final byte[] signedData;
final KeyStore ks = KeyStore.getInstance(certificateStoreType);
ks.load(certificateStream, certificatePassword);
// Get the certificate from the keystore using the alias
final X509Certificate inCert = (X509Certificate) ks.getCertificate(certificateAlias == null ? userName : certificateAlias);
// Get the private key using the alias and the password
final PrivateKey privateKey = (PrivateKey) ks.getKey(keyAlias == null ? userName : keyAlias, certificatePassword);
// Get the algorithm name from the certificate
final String issuerSigAlg = inCert.getSigAlgName();
// Get the signature for the specified algorithm
final Signature sig = Signature.getInstance(issuerSigAlg);
// Sign the message
sig.initSign(privateKey);
sig.update(ba);
signedData = sig.sign();
よろしくお願いいたします。
編集 これで機能するようになりましたが、コードを大幅に変更する必要がありました。Javaコードを自動変換して、何が生成されるかを確認しました。結果は以下のとおりですが、これは使用されている暗号アルゴリズムについて特定の仮定をしています.:-
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int) fs.Length);
var certificate2 = new X509Certificate2(buffer, certificatePassword);
string timestamp = DateTime.Now.ToShortDateString() + " 00:00:00";
string modifiedTs = userName + "|" + timestamp;
var cryptoKey = new RSACryptoServiceProvider();
var formatter = new RSAPKCS1SignatureFormatter(cryptoKey);
formatter.SetKey(certificate2.PrivateKey);
formatter.SetHashAlgorithm("SHA1");
var bytes = Encoding.UTF8.GetBytes(modifiedTs);
var rgbBytes = new SHA1CryptoServiceProvider().ComputeHash(bytes);
string signature = Convert.ToBase64String(formatter.CreateSignature(rgbBytes));