DSA ExampleDSA The Digital Signature Algorithm (DSA) is a United States Federal Government standard or FIPS for digital signatures.
|
Sample code
public void dsa(string message) { Tamir.SharpSsh.jsch.JSch jsch = new Tamir.SharpSsh.jsch.JSch(); string pkey = ""; pkey = message; // Test phrase int keysize = 1024; int type = Tamir.SharpSsh.jsch.KeyPair.DSA; Tamir.SharpSsh.jsch.KeyPair kpair = Tamir.SharpSsh.jsch.KeyPair.genKeyPair(jsch, type, keysize); try { Stream pri = new MemoryStream(); Stream pub = new MemoryStream(); kpair.setPassphrase(pkey); kpair.writePublicKey(pub, pkey); kpair.writePrivateKey(pri); fingerprint = kpair.getFingerPrint(); pri.Position = 0; pub.Position = 0; byte[] buffer = new byte[pri.Length]; pri.Read(buffer, 0, (int)pri.Length); tbPrivate = ByteToAscii(buffer); buffer = new byte[pub.Length]; pub.Read(buffer, 0, (int)pub.Length); tbPublic = ByteToAscii(buffer); pri.Close(); pub.Close(); } catch (Exception ex) { tbPublic = ex.Message; } } public static string ByteToAscii(byte[] buff) { string sbinary = ""; for (int i = 0; i < buff.Length; i++) { sbinary += (char)buff[i]; // hex format } return (sbinary); }
Method
With a discrete logarithm method, we typically have a base generator (\(g\)) and a prime number (\(p\)). We can then perform operations such as:
\(v=g^x. g^y \pmod p = g^{x+y} \pmod p\)
and:
\(v={(g^x)}^y \pmod p = g^{x.y} \pmod p\)
The Digital Signature Algorithm (DSA) is a standard defined in Federal Information Processing Standard (as FIPS 186) for digital signatures, and is based on discrete logarithms. It was outlined by NIST is 1991, and proposed within the Digital Signature Standard (DSS). This was then standardized with FIPS 186 in 1994, and FIPS 186-4 in 2013. Within FIPS 186-5, it is defined that DSA should not be used for the generation of signatures, but can be used for signature verification. Although DSA has a patent (Patent 5,231,668 by David W. Kravitz, who had previously worked for the NSA), NIST published the standard as a royality-free. The ECDSA method is an extension of DSA, but implemented with elliptic curve (EC) methods. As with most public key signing methods, we take a hash of a message, and then apply a private key to create a signature (\(r,s\)). This is done by creating a random value (k) to produce the signature. The signature is then verified using the associated public key. This then verifies the creator of the signature and that the message has not been changed.
Initially, Bob creates two prime numbers (\(p\) and \(q\)) and generates a generator value of \(g\). Next, he generates his secret key (\(x\)) and then computes his public key:
\(Y=g^{x} \pmod p\)
To create a signature for a message (\(M\)), he creates a random value (\(k\)) and then computes two values for the signature:
\(r = g^{k} \pmod{p} \pmod{q}\)
\(s=(k^{-1}.(H(m)+x.r)) \pmod {q}\)
When Alice receives this signature, she takes Bob's public key \((p,q,g,Y)\) and the message can computes:
\(w = s^{-1} \pmod q\)
\(u_1 = H(M).w \pmod q\)
\(u_2 = r.w \pmod q\)
\(v = (g^{u_1} . y^{u_2}) \pmod {p} \pmod {q}\)
She then checks that \(v\) is equal to \(r\). If so, the signature checks out. This works because:
\(v = g^{h.w}.y^{r.w} = g^{h.w}.g^{x.r.w} = g^{h.w+x.r.w} = g^{h/s+x.r/s} = g^{(H+x.r)/(k^{-1}(H+x.r))} = g^k = r\)