CryptoSys PKI Pro Manual

Example: Rsa.DecodeMsgForEncryption  Rsa.EME  Rsa.EncodeMsg  Rsa.EncodeMsgForEncryption 

[VB6 equivalent: RSA_EncodeMsg]

Dim abData(3) As Byte
Dim abBlock() As Byte
Dim abCheck() As Byte
Dim nBlockLen As Integer

' Our message data, 4 bytes long
abData(0) = &HDE
abData(1) = &HAD
abData(2) = &HBE
abData(3) = &HEF
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))

See Also:
Rsa.EncodeDigestForSignature Method
Rsa.EncodeMsgForEncryption Method
Rsa.EncodeMsgForSignature Method
Rsa.EncodeMsgIso9796 Method

[Contents] [Index]

[PREV: Example: Rsa.DecodeDigestForSignature ...]   [Contents]   [Index]   
   [NEXT: Example: Rsa.CheckKey ...]

Copyright © 2004-23 D.I. Management Services Pty Ltd. All rights reserved. Generated 2023-09-18T10:02:53Z.