Creates an EME or EMSA encoded message block according to PKCS#1.
VB6/VBA
Debug.Print "Testing RSA_EncodeMsg ..." Dim abData(3) As Byte Dim abBlock() As Byte Dim abCheck() As Byte Dim nDataLen As Long Dim nBlockLen As Long Dim nLen As Long Dim nRet As Long ' Our message data, 4 bytes long abData(0) = &HDE abData(1) = &HAD abData(2) = &HBE abData(3) = &HEF nDataLen = 4 Debug.Print "DATA =" & cnvHexStrFromBytes(abData) ' Set up output block with correct size nBlockLen = 64 ReDim abBlock(nBlockLen - 1) ' Encode ready for encryption with default algorithm nRet = RSA_EncodeMsg(abBlock(0), nBlockLen, abData(0), nDataLen, PKI_EME_PKCSV1_5) If (nRet < 0) Then MsgBox "Encoding Error" Exit Sub End If Debug.Print "BLOCK =" & cnvHexStrFromBytes(abBlock) ' Now encrypt this block using RSA_RawPublic ' ... ' ... and send to recipient ... ' ... ' who decrypts using RSA_RawPrivate to get the encoded block ' Recover the message from the encoded block ' How long is it? nLen = RSA_DecodeMsg(0, 0, abBlock(0), nBlockLen, PKI_EME_PKCSV1_5) If (nLen < 0) Then MsgBox "Decryption Error" Exit Sub End If ReDim abCheck(nLen - 1) nLen = RSA_DecodeMsg(abCheck(0), nLen, abBlock(0), nBlockLen, PKI_EME_PKCSV1_5) Debug.Print "DECODED=" & cnvHexStrFromBytes(abCheck) ' Alternative using more-secure OAEP algorithm nRet = RSA_EncodeMsg(abBlock(0), nBlockLen, abData(0), nDataLen, PKI_EME_OAEP) If (nRet < 0) Then MsgBox "Encoding Error" Exit Sub End If Debug.Print "BLOCK =" & cnvHexStrFromBytes(abBlock) ' ... nLen = RSA_DecodeMsg(0, 0, abBlock(0), nBlockLen, PKI_EME_OAEP) If (nLen < 0) Then MsgBox "Decryption Error" Exit Sub End If ReDim abCheck(nLen - 1) nLen = RSA_DecodeMsg(abCheck(0), nLen, abBlock(0), nBlockLen, PKI_EME_OAEP) Debug.Print "DECODED=" & cnvHexStrFromBytes(abCheck)
Output
Testing RSA_EncodeMsg ... DATA =DEADBEEF BLOCK =00021CDECFEAA21E887DD75F58A07DBFB2DDF4804A2B80EF6872CB52057E42A212D0E029D672C5697454D6C6BE88131F3830C6504FFA90EE8022AA00DEADBEEF DECODED=DEADBEEF BLOCK =0015A50DBD4030696DF1E76A58D38B93298F885760AC06015CBA3F25394E310FB2F80ECA71C8948377AC8C84184CFC4060BDA3DF4F030BED8817CEABD0256D4A DECODED=DEADBEEF
VB.NET
Console.WriteLine("Testing RSA_EncodeMsg ...")
Dim abData(3) As Byte
Dim abBlock() As Byte
Dim abCheck() As Byte
Dim nDataLen As Integer
Dim nBlockLen As Integer
' Our message data, 4 bytes long
abData(0) = &HDE
abData(1) = &HAD
abData(2) = &HBE
abData(3) = &HEF
nDataLen = 4
Console.WriteLine("DATA =" & Cnv.ToHex(abData))
' Set up output block with correct size
nBlockLen = 64
' Encode ready for encryption with default algorithm
abBlock = Rsa.EncodeMsgForEncryption(nBlockLen, abData, Rsa.EME.PKCSv1_5)
If (abBlock.Length = 0) Then
Console.WriteLine("Encoding Error")
Exit Sub
End If
Console.WriteLine("BLOCK =" & Cnv.ToHex(abBlock))
' Now encrypt this block using RSA_RawPublic
' ...
' ... and send to recipient ...
' ...
' who decrypts using RSA_RawPrivate to get the encoded block
' Recover the message from the encoded block
' How long is it?
abCheck = Rsa.DecodeMsgForEncryption(abBlock, Rsa.EME.PKCSv1_5)
If (abCheck.Length = 0) Then
Console.WriteLine("Decryption Error")
Exit Sub
End If
Console.WriteLine("DECODED=" & Cnv.ToHex(abCheck))
' Alternative using more-secure OAEP algorithm
abBlock = Rsa.EncodeMsgForEncryption(nBlockLen, abData, Rsa.EME.OAEP)
If (abBlock.Length = 0) Then
Console.WriteLine("Encoding Error")
Exit Sub
End If
Console.WriteLine("BLOCK =" & Cnv.ToHex(abBlock))
' ...
abCheck = Rsa.DecodeMsgForEncryption(abBlock, Rsa.EME.OAEP)
If (abCheck.Length = 0) Then
Console.WriteLine("Decryption Error")
Exit Sub
End If
Console.WriteLine("DECODED=" & Cnv.ToHex(abCheck))
[Contents]