RSA Digital Signatures in 12 Lines of Python
I have been setting myself 12 line challenges for RSA encryption, so here’s one which signs a message in RSA in just 12 lines of Python code. With digital signing, we take our private key, and encrypt our message to give a signature. Our public key is then used to verify the signature.
With this we use the decryption key value to encrypt for a signature, and the public key to prove the signature. In this case, we use s as the signing exponent (which would be defined as d for decryption), and v for the verification exponent (which would be defined as e for encryption). Alice initially has two secret prime numbers (p and q), and which she produces a product (the modulus):
N=pq
Next, she selects a verification exponent (v), and which does not share a factor with:
PHI=(p-1)(q-1)
In most cases she will select:
v=65,537
Next she computes the signature exponent (s) with:
sv=1(mod(p−1)(q−1))
Next she takes a message (D) and sign with:
S=D^s (mod N)
Bob then checks the signature with:
M=S^v(modN)