[Mono-list] How to set parameters for RSACryptoServiceProvider

Goran Milenkovic GMilenkovic@mindport.com
Mon, 3 Nov 2003 14:56:13 +0100


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C3A212.3E834BA8
Content-Type: text/plain

The following VB.NET example should generate the new key pair, store
generated values localy, then encrypt the test string using public key and
decrypt it using private key. However, it works only if all parameters
(P,Q,DP,DQ,InverseQ & D) are given for the decryption, but not if only D is
given. That means that for simple decryption private key is not enough, but
also all of the other parameters ?? 

If you change boolean value OnlyD to True, assuming that only D should be
enough for decryption, then it works. Otherwise, an exception occurs ("Bad
Key"). Is there any solution for this ?

 

'-----------------------------

Dim OnlyD As Boolean = False

Dim TestResult1() As Byte

Dim TestResult2() As Byte

'---Generate the key pair and export all of it's parameters into the
RSAParams1

Dim RSAParams1 As RSAParameters = New RSAParameters

Dim RSAObj1 As RSACryptoServiceProvider = New RSACryptoServiceProvider

RSAParams1 = RSAObj1.ExportParameters(True)

'---Get all parameters into local variables

Dim myModulus As Byte() = RSAParams1.Modulus

Dim myExponent As Byte() = RSAParams1.Exponent

Dim myPrivateKey As Byte() = RSAParams1.D

Dim myP As Byte() = RSAParams1.P

Dim myQ As Byte() = RSAParams1.Q

Dim myDP As Byte() = RSAParams1.DP

Dim myDQ As Byte() = RSAParams1.DQ

Dim myIQ As Byte() = RSAParams1.InverseQ

'---Create new RSACryproProvider

Dim RSAParams2 As RSAParameters = New RSAParameters

Dim RSAObj2 As RSACryptoServiceProvider = New RSACryptoServiceProvider

'---Set the public key

RSAParams2.Modulus = myModulus

RSAParams2.Exponent = myExponent

RSAObj2.ImportParameters(RSAParams2)

'---Encrypt with public key

TestResult1 = RSAObj2.Encrypt(Encoding.ASCII.GetBytes("TEST"), False)

MsgBox("Encrypted")

'---Now set the private key

RSAParams2.D = myPrivateKey

If (Not OnlyD) Then

    RSAParams2.P = myP

    RSAParams2.Q = myQ

    RSAParams2.DP = myDP

    RSAParams2.DQ = myDQ

    RSAParams2.InverseQ = myIQ

End If

RSAObj2.ImportParameters(RSAParams2)

'---Decrypt with private key

TestResult2 = RSAObj2.Decrypt(TestResult1, False)

MsgBox("Decrypted. Original string is: " &
Encoding.ASCII.GetString(TestResult2))

'-----------------------------

 

Many Thanks,

Goran

gmilenkovic@irdetoaccess.com <mailto:gmilenkovic@irdetoaccess.com> 

 
This e-mail and any attachments are CONFIDENTIAL and intended solely for the
use of the individual(s) to whom it is addressed. It can contain proprietary
confidential information and/or be subject to legal privilege and/or subject
to a non-disclosure Agreement. Unauthorized use, disclosure or copying is
strictly prohibited. If you are not the/an addressee and are in possession
of this e-mail, please notify us immediately.

 

 

------_=_NextPart_001_01C3A212.3E834BA8
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=US-ASCII">
<TITLE>Message</TITLE>

<META content="MSHTML 6.00.3790.59" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
style="mso-spacerun: yes"><SPAN class=910164013-03112003>The following 
VB.NET&nbsp;example should generate the new key pair, store generated values 
localy, then encrypt the test string using public&nbsp;key and decrypt it using 
private key. However, it works only if all parameters (P,Q,DP,DQ,InverseQ &amp; 
D) are given for the decryption, but not if only D is given. That means that for 
simple decryption private key is not enough, but also all of the other 
parameters ?? </SPAN></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
style="mso-spacerun: yes"><SPAN class=910164013-03112003>If you change boolean 
value OnlyD to True, assuming that only D should be enough for decryption, then 
it works. Otherwise, an exception occurs ("Bad Key"). Is there any solution for 
this ?</SPAN></SPAN></SPAN><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
style="mso-spacerun: yes"><SPAN 
class=910164013-03112003></SPAN></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
style="mso-spacerun: yes"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"></SPAN></SPAN></SPAN>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
style="mso-spacerun: yes"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN><SPAN 
class=910164013-03112003>--------------------------</SPAN></SPAN></P></SPAN></SPAN>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim OnlyD As Boolean = 
False<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" 
/><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim TestResult1() As 
Byte<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim TestResult2() As 
Byte<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN>Generate the key pair and export all of it's 
parameters into the RSAParams1<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim RSAParams1 As 
RSAParameters = New RSAParameters<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim RSAObj1 As 
RSACryptoServiceProvider = New RSACryptoServiceProvider<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">RSAParams1 = 
RSAObj1.ExportParameters(True)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN>Get all parameters into local 
variables<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim myModulus As Byte() = 
RSAParams1.Modulus<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim myExponent As Byte() = 
RSAParams1.Exponent<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim myPrivateKey As Byte() = 
RSAParams1.D<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim myP As Byte() = 
RSAParams1.P<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim myQ As Byte() = 
RSAParams1.Q<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim myDP As Byte() = 
RSAParams1.DP<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim myDQ As Byte() = 
RSAParams1.DQ<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim myIQ As Byte() = 
RSAParams1.InverseQ<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN>Create new 
RSACryproProvider<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim RSAParams2 As 
RSAParameters = New RSAParameters<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Dim RSAObj2 As 
RSACryptoServiceProvider = New RSACryptoServiceProvider<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN>Set the public key<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">RSAParams2.Modulus = 
myModulus<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">RSAParams2.Exponent = 
myExponent<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">RSAObj2.ImportParameters(RSAParams2)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN>Encrypt with public key<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">TestResult1 = 
RSAObj2.Encrypt(Encoding.ASCII.GetBytes("TEST"), False)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">MsgBox("Encrypted")<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN>Now set the private key<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">RSAParams2.D = 
myPrivateKey<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">If (Not OnlyD) 
Then<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
class=910164013-03112003>&nbsp;&nbsp;&nbsp; </SPAN>RSAParams2.P = 
myP<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
class=910164013-03112003>&nbsp;&nbsp;&nbsp; </SPAN>RSAParams2.Q = 
myQ<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
class=910164013-03112003>&nbsp;&nbsp;&nbsp; </SPAN>RSAParams2.DP = 
myDP<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
class=910164013-03112003>&nbsp;&nbsp;&nbsp; </SPAN>RSAParams2.DQ = 
myDQ<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
class=910164013-03112003>&nbsp;&nbsp;&nbsp; </SPAN>RSAParams2.InverseQ = 
myIQ<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">End If<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">RSAObj2.ImportParameters(RSAParams2)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN>Decrypt with private 
key<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">TestResult2 = 
RSAObj2.Decrypt(TestResult1, False)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">MsgBox("Decrypted. Original 
string is: " &amp; Encoding.ASCII.GetString(TestResult2))</SPAN></P><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">'<SPAN 
class=910164013-03112003>---</SPAN><SPAN 
class=910164013-03112003>--------------------------</SPAN></SPAN></P></SPAN>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><SPAN 
class=910164013-03112003><FONT face=Arial></FONT></SPAN></SPAN><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"></SPAN>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p><SPAN 
class=910164013-03112003>Many Thanks,</SPAN></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p><SPAN 
class=910164013-03112003>Goran</SPAN></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p><SPAN 
class=910164013-03112003><A 
href="mailto:gmilenkovic@irdetoaccess.com">gmilenkovic@irdetoaccess.com</A></SPAN></o:p></SPAN></P></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV align=left><SPAN 
style="FONT-SIZE: 7pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Verdana">
<P class=MsoNormal 
style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; tab-stops: 62.35pt 85.05pt 153.1pt 175.75pt 243.8pt 266.5pt 334.55pt 357.2pt 425.25pt"><SPAN 
style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Verdana; mso-bidi-font-family: Verdana">This 
e-mail and any attachments are CONFIDENTIAL and intended solely for the use of 
the individual(s) to whom it</SPAN><SPAN 
style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: Arial"> i</SPAN><SPAN 
style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: Verdana; mso-bidi-font-family: Verdana">s 
addressed. It can contain proprietary confidential information and/or be subject 
to legal privilege and/or subject to a non-disclosure Agreement. Unauthorized 
use, disclosure or copying is strictly prohibited. If you are not the/an 
addressee and are in possession of this e-mail, please notify us 
immediately.<o:p></o:p></SPAN></P>
<P class=MsoNormal 
style="MARGIN: 0in 0in 0pt; mso-pagination: none; mso-layout-grid-align: none"><FONT 
face=Arial color=#000000 size=2></FONT></SPAN><SPAN 
style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Verdana"><FONT 
face=Verdana color=#ff0000 size=1><FONT face=Arial color=#000000 
size=2></FONT>&nbsp;</P></FONT></SPAN></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BODY></HTML>

------_=_NextPart_001_01C3A212.3E834BA8--