|
|
 |

| Current Page:
Hpmbcalc --> generate RSA public/private key pairs... |
Generate RSA public/private key pairs using JScript/VBScript
Summary
This article describes how to generate RSA public/private key pairs using
JScript/VBScript and the number theory operation methods dispatched by
Hpmbmath object. To complete this example, you will need
Hpmbcalc Hex Calculator
and a scripting language such as VBScript or JScript. In this example, we will
be using JScript as the scripting language.
More Information
Hpmbcalc is a programmable multiple-precision hex calculator with many
utilities. Behind it, there is an automation object, named Hpmbmath,
which dispatches some multiple-precision arithmetic methods for your scripts
to invoke. To access these methods, you can run your scripts under any Windows
scripting host that supports VBScript or JScript.
Hpmbcalc itself is a scripting host, which provides a user interface for
you to load and run scripts. Within Hpmbcalc, you can perform many
purpose-designed big number arithmetic. This article shows you an example of
using the number theory operation methods to generate RSA public/private key pairs.
The example applies to Hpmbcalc version 3.31 or later.
The following sample Jscript code illustrates how to implement this function:
///////////////////////////////////////////////////////////////////
// RSA Public/Private Key Pair Generation Script
// ---------------------------------------------
//
// This JScript code shows an example of RSA public/private key
// pair generation with parameters that you can specify.
//
// Summary: B encrypts a message m for A, which A decrypts.
//
// 1. Key generation. A should do the following:
//
// (a) Generate two 256-bit primes p, q.
// (b) Compute n = pq and f = (p-1)(q-1).
// (c) Choose e and find d such that ed = 1 (mod f).
//
// A's public key is the pair (n, e), while A's private key is d.
//
// 2. Encryption. B should do the following:
//
// (a) Obtain A's authentic public key (n, e).
// (b) Represent the message as an integer m in the interval [0, n-1].
// (c) Compute c = m^e mod n.
// (d) Send the ciphertext c to A.
//
// 3. Decryption. To recover plaintext m from c, A should do the
// following:
//
// (a) Use the private key d to recover m = c^d mod n.
//
///////////////////////////////////////////////////////////////////
var strHexP, strHexQ, strHexP1, strHexQ1
var strHexN, strHexF, strHexE, strHexD
var strHexC, strHexMB, strHexMA
//
// 1. Key generation
//
// (a) Generate two 256-bit primes p, q.
strHexP = Hpmbmath.MillerRabinSearch(256, 8)
do {
strHexQ = Hpmbmath.MillerRabinSearch(256, 8)
} while (strHexQ== strHexP);
Hpmbmath.Output("1. Key generation")
Hpmbmath.Output("\r\n(a) Generate two 256-bit primes p, q.")
Hpmbmath.Output("\r\nPrime p = \r\n" + strHexP)
Hpmbmath.Output("\r\nPrime q = \r\n" + strHexQ)
//
// (b) Compute n = pq and f = (p-1)(q-1).
strHexP1 = Hpmbmath.Subtract(strHexP, "01")
strHexQ1 = Hpmbmath.Subtract(strHexQ, "01")
strHexN = Hpmbmath.Multiply(strHexP, strHexQ)
strHexF = Hpmbmath.Multiply(strHexP1, strHexQ1)
Hpmbmath.Output("\r\n(b) compute modulus n = pq.")
Hpmbmath.Output("\r\nModulus n = \r\n" + strHexN)
//
// (c) Choose e and find d such that ed = 1 (mod f).
strHexE = Hpmbmath.MillerRabinSearch(64, 8)
strHexD = Hpmbmath.ModInverse(strHexE, strHexF)
Hpmbmath.Output("\r\n(c) Choose e and find d such that ed = 1 (mod f).")
Hpmbmath.Output("\r\nPublic exponent e = \r\n" + strHexE)
Hpmbmath.Output("\r\nPrivate exponent d = \r\n" + strHexD)
//
// 2. Encryption
//
// (a) Encrypt a message m = 5234673
strHexMB = "4f df f1"
//
// (b) Compute c = m^e mod n
strHexC = Hpmbmath.ModPow(strHexMB, strHexE, strHexN)
//
// (c) B sends c to A.
//
// 3. Decryption
//
// (a) To encrypt c, A computes c^d mod n
strHexMA = Hpmbmath.ModPow(strHexC, strHexD, strHexN)
// If all go well, strHexMA and strHexMB should have same value
Hpmbmath.Output("\r\n2. Encrypt/Decrypt Test")
Hpmbmath.Output("\r\nMB = " + strHexMB)
Hpmbmath.Output("MA = " + strHexMA)
Hpmbmath.Output("\r\nMA should be same as MB.")
|
 |