CryptoSys PKI Toolkit Manual

RSA_RawPrivate

RSA_RawPrivate transforms (i.e. encrypts or decrypts) raw data using an RSA private key.

VB6/VBA Syntax

Public Declare Function RSA_RawPrivate Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long

nRet = RSA_RawPrivate(abData(0), nDataLen, strPrivateKey, nOptions)

Parameters

abData
[in/out] Byte array containing the data to be transformed.
nDataLen
[in] Long specifying the number of bytes of data.
strPrivateKey
[in] String containing the RSA private key string.
nOptions
[in] Long option flags. Set to zero.

C/C++ Syntax

long _stdcall RSA_RawPrivate(unsigned char *abData, long nDataLen, const char *szPrivateKey64, long nOptions);

Returns (VB6/C)

Long: If successful, the return value is zero; otherwise it returns a non-zero error code.

.NET Equivalent

Rsa.RawPrivate Method (Byte[], String)
Rsa.RawPrivate Method (Byte[], String, Int32)

Remarks

The data must be the same length as the RSA key modulus (use RSA_KeyBytes to find out this). The output is written over the input. The RSA private key must be provided in the internal key string format.

Example

This is adapted from "Some Examples of the PKCS Standards: An RSA Laboratories Technical Note", Burton S. Kaliski Jr., 1993 [PKCS-EX]. It carries out the signing of the encryption block from section 3.2 using the 508-bit private key and then verifies the signature using the corresponding public key. The keys are stored in files rsa508.pub and rsa508.epk.

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 = "C:\Test\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 Function
End If
Debug.Print strPrivateKey

' 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 = "C:\Test\rsa508.pub"
strPublicKey = rsaReadPublicKey(strPubFile)
Debug.Print strPublicKey

' 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

See Also

RSA_RawPublic RSA_EncodeMsg Raw RSA Techniques

[Contents] [Index]

[HOME]   [NEXT: RSA_RawPublic...]

Copyright © 2004-9 D.I. Management Services Pty Ltd. All rights reserved.