2009/08/02

PKI Research #4 - Digital Signature

這篇預定要來研究數位簽章。

數位簽章的方式跟加解密剛好是反過來的,加解密使用公鑰加密,使用私鑰解密;數位簽章卻是使用私鑰簽章,使用公鑰驗證。

加解密的對象是明文檔案內容,而數位簽章的對象則是明文檔案內容經過雜湊後的值。常用的有 MD5 跟 SHA-1,聽說這兩個都有被碰撞破解的個案,所以使用 SHA256 (SHA-2 家族)。

.NET Fraamework 的 System.Security.Cryptography 命名空間實作不少相關雜湊,我直接寫成以下方法:
public static byte[] useSHA256(byte[] data){
SHA256 sha = new SHA256Managed();
return sha.ComputeHash(data);
}

流程圖一張:
http://en.wikipedia.org/wiki/File:Digital_Signature_diagram.svg

先從簽章實作。第一件事就是將文章進行 Hash,我使用上面寫好的 SHA256 方法來製造。然後要將此 Hash 以私鑰加密,因為 X509Certificate2 不能直接支援 RSA Private Key PEM 檔 (範例中使用 DER 格式方便 RFID Tag 儲存),所以我使用 Bouncy Castle C# 來進行私鑰的讀取,並用來加密 Hash 產生數位簽章 (稱為 Hash')。

另一邊進行驗證。驗證也需要將文章進行 Hash,以便跟數位簽章比對。一樣使用 SHA256 產生 Hash。然後將數位簽章使用 .crt 憑證內存的公鑰解密 (得到 Hash'),結果拿來跟 Hash 比對。如果完全正確 (Hash' = Hash) 表示文章沒有經過竄改,而且也能確定簽章者正是本人。但是 X509Certificate2 不能直接拿公鑰進行解密 (Decrypt),而是提供另一個驗證 (VerifyHash) 方法供呼叫,我也搞不懂為什麼不行。

public static byte[] signature(byte[] data){
byte[] hash = useSHA256(data);
try{
var seq = (Asn1Sequence) Asn1Object.FromStream(File.OpenRead(@"B:\OpenSSL\1-der.key"));
var rsa = new RsaPrivateKeyStructure(seq);
var privSpec = new RsaPrivateCrtKeyParameters(
rsa.Modulus, rsa.PublicExponent, rsa.PrivateExponent,
rsa.Prime1, rsa.Prime2, rsa.Exponent1, rsa.Exponent2,
rsa.Coefficient
);

var encryptEngine = new Pkcs1Encoding(new RsaEngine());
encryptEngine.Init(true, privSpec);
return encryptEngine.ProcessBlock(hash, 0, hash.Length); // Digital Signature
}catch(Exception e){
throw new IOException("problem creating private key: " + e.ToString());
}
}


public static bool verify(byte[] message, byte[] sig){
byte[] hash = useSHA256(message);

var crt = new X509Certificate2(@"B:\OpenSSL\1.crt"); // Read X509 Certificate file
var rsa = (RSACryptoServiceProvider) crt.PublicKey.Key; // Get public key

return rsa.VerifyHash(hash, "SHA256", sig);
}

1 則留言:

  1. This post is a true learning experience for me. I read so many facts which are important as well as unique about this scheme. I will keep visiting your blog to learn more about this technique. Keep sharing.
    digital signature

    回覆刪除