CryptoSys PKI Pro Manual

RSA_RawPrivate

Transforms (that is, encrypts or decrypts) raw data using an RSA private key.

VBA/VB6 Syntax

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

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

C/C++ Syntax

long __stdcall RSA_RawPrivate(unsigned char *lpData, long nDataLen, const char *szPrivateKey, long nOptions);

Parameters

lpData
[in,out] array containing the data to be transformed.
nDataLen
[in] specifying the number of bytes of data.
szPrivateKey
[in] containing the RSA private key string.
nOptions
[in] option flags. Set to zero.

Returns (VBA/C)

If successful, the return value is zero; otherwise it returns a nonzero error code.

VBA Wrapper Syntax

Public Function rsaRawPrivate (lpData() As Byte, szPrivateKey As String, Optional nOptions As Long = 0) As Byte()

.NET Equivalent

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

C++ (STL) Equivalent

static bvec_t dipki::Rsa::RawPrivate (const bvec_t &data, const std::string &keyStr)

Python Equivalent

static Rsa.raw_private(block, prikeystr)

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 (VBA core function)

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.p8e.

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.p8e"
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 = "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

Example (VBA wrapper function)

Dim strPrivateKey As String
Dim pt() As Byte
Dim ct() As Byte
strPrivateKey = rsaReadPrivateKey("rsa508.p8e", "password")
Debug.Assert Len(strPrivateKey) > 0
pt = cnvBytesFromHexStr("0001ffffffffffffffffffffffffffff" & _
    "ffffffffffffffffffffffffff003020" & _
    "300c06082a864886f70d020205000410" & _
    "dca9ecf1c15c1bd266aff9c8799365cd")
ct = rsaRawPrivate(pt, strPrivateKey)
Debug.Print "CT=" & cnvHexStrFromBytes(ct)

Dim strPublicKey As String
Dim dt() As Byte
strPublicKey = rsaReadPublicKey("Rsa508.pub")
Debug.Assert Len(strPublicKey) > 0
dt = rsaRawPublic(ct, strPublicKey)
Debug.Print "DT=" & cnvHexStrFromBytes(dt)

See Also

RSA_RawPublic RSA_EncodeMsg Raw RSA Techniques

[Contents] [Index]

[PREV: RSA_PublicKeyFromPrivate...]   [Contents]   [Index]   
   [NEXT: RSA_RawPublic...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.