CryptoSys PKI examples VB6 to VB.NET

RSA_RawPrivate

Transforms raw data using an RSA private key.

VB6/VBA

Debug.Print "Testing RSA_RawPrivate ..."
Dim strEPKFile As String
Dim strPubFile As String
Dim strPassword As String
Dim strPublicKey As String
Dim strPrivateKey As String
Dim nRet As Long
Dim strOutputFile As String
Dim abData() As Byte
Dim nDataLen As Long
Dim sHexData As String

strEPKFile = "rsa508.epk"
strPassword = "password"

' Read in the deciphered private key string
strPrivateKey = rsaReadPrivateKey(strEPKFile, strPassword)
If Len(strPrivateKey) = 0 Then
    MsgBox "Unable to retrieve private key"
    Exit Sub
End If
Debug.Print "PriKey length= " & RSA_KeyBits(strPrivateKey) & " bits"

' Create some raw data to be RSA'd
' Ref: 3.2 Signing the CertificationRequestInfo encoding
' 64-octet EB in full:
'00 01 ff ff ff ff ff ff ff ff ff ff ff ff ff ff
'ff ff ff ff ff ff ff ff ff ff ff ff ff 00 30 20
'30 0c 06 08 2a 86 48 86 f7 0d 02 02 05 00 04 10
'dc a9 ec f1 c1 5c 1b d2 66 af f9 c8 79 93 65 cd

sHexData = "0001ffffffffffffffffffffffffffff" & _
    "ffffffffffffffffffffffffff003020" & _
    "300c06082a864886f70d020205000410" & _
    "dca9ecf1c15c1bd266aff9c8799365cd"

abData = cnvBytesFromHexStr(sHexData)
nDataLen = UBound(abData) - LBound(abData) + 1
Debug.Print "Input:  " & cnvHexStrFromBytes(abData)

' Now we have our data in a byte array and
' our private key in string format,
' we are ready to do a "raw" operation
nRet = RSA_RawPrivate(abData(0), nDataLen, strPrivateKey, 0)
Debug.Print "RSA_RawPrivate returns " & nRet
If nRet <> 0 Then
    Debug.Print pkiGetLastError()
Else
    ' Display our results in hex format
    Debug.Print "Output: " & cnvHexStrFromBytes(abData)
End If

' Get the corresponding Public Key, also in a file
strPubFile = "rsa508.pub"
strPublicKey = rsaReadPublicKey(strPubFile)
Debug.Print "PubKey length= " & RSA_KeyBits(strPublicKey) & " bits"

' Do a "raw" encryption with the public key
nRet = RSA_RawPublic(abData(0), nDataLen, strPublicKey, 0)
Debug.Print "RSA_RawPublic returns " & nRet
If nRet <> 0 Then
    Debug.Print pkiGetLastError()
Else
    ' Display our results in hex format
    Debug.Print "Decrypt:" & cnvHexStrFromBytes(abData)
End If

Output

Testing RSA_RawPrivate ...
PriKey length= 508 bits
Input:  0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003020300C06082A864886F70D020205000410DCA9ECF1C15C1BD266AFF9C8799365CD
RSA_RawPrivate returns 0
Output: 06DB36CB18D3475B9C01DB3C789528080279BBAEFF2B7D558ED6615987C851863F8A6C2CFFBC89C3F75A18D96B127C717D54D0D8048DA8A0544626D17A2A8FBE
PubKey length= 508 bits
RSA_RawPublic returns 0
Decrypt:0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003020300C06082A864886F70D020205000410DCA9ECF1C15C1BD266AFF9C8799365CD

VB.NET

Console.WriteLine("Testing RSA_RawPrivate ...")
Dim strEPKFile As String
Dim strPubFile As String
Dim strPassword As String
Dim sbPublicKey As StringBuilder
Dim sbPrivateKey As StringBuilder
Dim abData() As Byte
Dim sHexData As String

strEPKFile = "rsa508.epk"
strPassword = "password"

' Read in the deciphered private key string
sbPrivateKey = Rsa.ReadEncPrivateKey(strEPKFile, strPassword)
If sbPrivateKey.Length = 0 Then
    Console.WriteLine("Unable to retrieve private key")
    Exit Sub
End If
Console.WriteLine("PriKey length= " & Rsa.KeyBits(sbPrivateKey.ToString()) & " bits")

' Create some raw data to be RSA'd
' Ref: 3.2 Signing the CertificationRequestInfo encoding
' 64-octet EB in full:
'00 01 ff ff ff ff ff ff ff ff ff ff ff ff ff ff
'ff ff ff ff ff ff ff ff ff ff ff ff ff 00 30 20
'30 0c 06 08 2a 86 48 86 f7 0d 02 02 05 00 04 10
'dc a9 ec f1 c1 5c 1b d2 66 af f9 c8 79 93 65 cd

sHexData = "0001ffffffffffffffffffffffffffff" & _
    "ffffffffffffffffffffffffff003020" & _
    "300c06082a864886f70d020205000410" & _
    "dca9ecf1c15c1bd266aff9c8799365cd"

abData = Cnv.FromHex(sHexData)
Console.WriteLine("Input:  " & Cnv.ToHex(abData))

' Now we have our data in a byte array and
' our private key in string format,
' we are ready to do a "raw" operation
abData = Rsa.RawPrivate(abData, sbPrivateKey.ToString)
Console.WriteLine("RSA_RawPrivate returns " & abData.Length)
If abData.Length = 0 Then
    Console.WriteLine("ERROR: " & General.LastError())
Else
    ' Display our results in hex format
    Console.WriteLine("Output: " & Cnv.ToHex(abData))
End If

' Get the corresponding Public Key, also in a file
strPubFile = "rsa508.pub"
sbPublicKey = Rsa.ReadPublicKey(strPubFile)
Console.WriteLine("PubKey length= " & Rsa.KeyBits(sbPublicKey.ToString()) & " bits")

' Do a "raw" encryption with the public key
abData = Rsa.RawPublic(abData, sbPublicKey.ToString(), 0)
Console.WriteLine("RSA_RawPublic returns " & abData.Length)
If abData.Length = 0 Then
    Console.WriteLine("ERROR: " & General.LastError())
Else
    ' Display our results in hex format
    Console.WriteLine("Decrypt:" & Cnv.ToHex(abData))
End If
[HOME]   [NEXT: RSA_RawPublic...]

Copyright © 2010-20 D.I. Management Services Pty Ltd. All rights reserved.