In this case we generate an RSA key pair. With this we have two prime numbers (\(p\) and \(q\)), and compute the modulus (\(N=pq\)). We then pick an encryption key value (\(e=0x010001\)) and then compute \(d=e^{-1} \pmod \phi\)), and where \(\phi=(p-1)(q-1)\). To encrypt a message (\(M\)), we create a cipher \(c=M^e \pmod N\), and then decrypt with \(M=c^d \pmod N\). In this case we will prove the computations with the generation of a message value and an RSA key pair, and then encrypt and decrypt a string.
RSA Encryption and Decryption with PowerShell |
Method
In this case we will generate an RSA key pair and encode the key pair into the CBOR representation. With this we have two prime numbers (\(p\) and \(q\)), and compute the modulus (\(N\)):
\(N=pq\)
We then pick an encryption key value (\(e=0x010001\)) and then compute:
\(d=e^{-1} \pmod \phi\))
and where:
\(\phi=(p-1)(q-1)\)
The public key is then \((e,N)\) and the private key is \((d,N)\). To encrypt a message (\(M\)), we create a cipher:
\(c=M^e \pmod N\)
and then decrypt with:
\(M=c^d \pmod N\)
The key pair thus contains \(e, N, d, p\) and \(q\) for a key pair of \((e,N)\) and \((d,N)\).
The coding is:
$plaintext=$Args[0] $size=$Args[1] $plainBytes = [System.Text.Encoding]::UTF8.GetBytes($plaintext) $tt=[System.Security.Cryptography.RSACryptoServiceProvider]::new($size) $cipher=$tt.Encrypt($plainBytes,[System.Security.Cryptography.RSAEncryptionPadding]::Pkcs1) "Message: "+$plaintext "`nCipher: "+[System.Convert]::ToHexString($cipher) $plain=$tt.Decrypt($cipher,[System.Security.Cryptography.RSAEncryptionPadding]::Pkcs1) "`nDecryption: "+[System.Text.Encoding]::ASCII.GetString($plain) "`n== RSA Parameters ==" $a=$tt.ExportParameters($true) "E: "+[System.Convert]::ToHexString($a.Exponent) "Modulus: "+[System.Convert]::ToHexString($a.Modulus) "D: "+[System.Convert]::ToHexString($a.D) "P: "+[System.Convert]::ToHexString($a.P) "Q: "+[System.Convert]::ToHexString($a.Q) "`n=== As integer values === " "E: "+ [System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.Exponent),'AllowHexSpecifier') "Modulus: "+ [System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.Modulus),'AllowHexSpecifier') "D: "+ [System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.D),'AllowHexSpecifier') "P: "+ [System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.P),'AllowHexSpecifier') "Q: "+ [System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.Modulus),'AllowHexSpecifier')
A sample run for a 1,024-bit RSA key pair:
Message: Hello Cipher: 7E7525895666395B67F409D4F86E9017C531CD959ED3A52430E9C7AEF5CBF6F4994CCF7F96B5FC71E9ECFA4A56BAF3C0587909D1B3958EA819AC3BA88D5573E1 Decryption: Hello == RSA Parameters == E: 010001 Modulus: DABAD378081BDC6BF2C0FF824DE42B257AEFC455E79C99F8816EAC1D2F78105AB35F2C02A5920F36185B322C680DBA8C035DFD07590812C2CE81385BEE77E8BD D: 9C6F723F2ECD7E6056466277B76DB0CE2596C63D0A619E5D7574C9F435B71139FB03D965511E4E0739D4E7DA45942DA51E805A31702F317DAE5E7BBEED249EA1 P: EC12420976496354E3AEDF42759019B47B1E1E5D2669A75434D7DD7CD9BB4C67 Q: ED31CCC79F61AB31B09BE2CD0943746609A3BFCEB66515ADC820EEB356C82B3B === As integer values === E: 65537 Modulus: 11455808605236937175386804659757462125428705732604286344736933508009733905272324168871042998931341260809211816887575639386246576767449366465141635057969341 D: 8193183407001628382207460030397359731801795857281049018882609020666680617176775635518226358353290279705250363254534728246245038483378202978694153397116577 P: 106778091283172327250389290683697272275117089486610360279204681544802804386919 Q: 11455808605236937175386804659757462125428705732604286344736933508009733905272324168871042998931341260809211816887575639386246576767449366465141635057969341