Visual Basic Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

VB Examples

Bounced Mail
Character Encoding
Digital Certificates
Digital Signatures
Email
FTP
HTML-to-XML
HTTP
IMAP
Encryption
MHT / HTML Email
RSA Encryption
S/MIME
Socket
Spider
String
Tar
Unicode
Upload
XML
XMP
Zip Compression

More Examples...
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor


VB Strings
VB Byte Array

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

 

RSA Encryption -- Same Key Different Results

Download Chilkat RSA ActiveX

The RSA encryption algorithm produces different results for each call, even when encrypting the same data with the same key. Decryption however, will produce the correct results. This example demonstrates.

Dim rsa As New ChilkatRsa

Dim success As Long
success = rsa.UnlockComponent("Anything for 30-day trial")
If (success <> 1) Then
    MsgBox "RSA component unlock failed"
    Exit Sub
End If

'  RSA public key in XML format:
Dim publicKey As String
publicKey = "<RSAKeyValue><Modulus>xxyv1RDPU0MvfFIIa98HppXdcuI7zSu8uIqyGAy/VoxPvxZFX0acajznvjVRHipHbpcO6ryo2LwXUPf89qOqLb3Qd1lfD2ZnH+TQ6MZXNxfFRxTpTUd+tTR4EBYpd2t6kzq8ZRJYLdlviaMQQqUEwR54k7Op5HJYVKUcHIkP1xE=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"

Dim privateKey As String
privateKey = "<RSAKeyValue><Modulus>xxyv1RDPU0MvfFIIa98HppXdcuI7zSu8uIqyGAy/VoxPvxZFX0acajznvjVRHipHbpcO6ryo2LwXUPf89qOqLb3Qd1lfD2ZnH+TQ6MZXNxfFRxTpTUd+tTR4EBYpd2t6kzq8ZRJYLdlviaMQQqUEwR54k7Op5HJYVKUcHIkP1xE=</Modulus><Exponent>AQAB</Exponent><P>4cpW9fvG99Jsz8/AO7PDHTl+pPRAglksrR2kClLV2g9DEeFe/bvmCxLUgMCJ+0eGQ1zA6aA7McKr13zTQ7jKpQ==</P><Q>4cCS/kFlq/P1ExF37Fkh4pCodOEGutepLEG7Q/KljT3ZGlAY+2l8fGu4f+hrkUuGoFl7NOMaJflULoPIgQaq/Q==</Q><DP>lkjcSsvzqh3YKRXJiLNkyf3rypV8noYGU4+oEOsDxilkZfFRDafUPUiiQrRk4ui/d/SzvozU+ZDuWfaOk8PatQ==</DP><DQ>SYCD25i7W8Mwdibn3uIecEAdOQDTSh5RjIFSUYs9b8FFYJXXrHPp/jCsf6jS7RmkGa1Iui1/JAIL8KEjtS7QmQ==</DQ><InverseQ>EDAJa3TpNdPQ3GIdBpnTgFTQY5A60DcszsUW/iCYoXQdPVJ9BLBxVTe9jiLzGuNuzLkVBwQlCy0Bf84hACRV9A==</InverseQ><D>cMFdDYKkddlRNczaugOmOH8b1egpx2liSPs6GYZ2gFObAXJiPK8m+r6c2ckls7hrlUP0DZhi4cG6Tn7xANb0Ek17P7QquVhQYOmFy/YHzm+IJbcwwq7pJHhZBhtcjyXqfUZ+BADGE//GQbrSVwVltpOj5KcxG88NAprLn2MMxfE=</D></RSAKeyValue>"

rsa.ImportPublicKey publicKey

'  Encrypt a string and return the encrypted data base64-encoded:
rsa.EncodingMode = "base64"

Dim plainText As String
plainText = "RSA gives different results with each call, weird but OK"

Dim usePrivateKey As Long
usePrivateKey = 0
Dim encryptedStr1 As String
encryptedStr1 = rsa.EncryptStringENC(plainText,usePrivateKey)
Text1.Text = Text1.Text & encryptedStr1 & vbCrLf
Text1.Refresh

'  Do it again. The results are different...
Dim encryptedStr2 As String
encryptedStr2 = rsa.EncryptStringENC(plainText,usePrivateKey)
Text1.Text = Text1.Text & encryptedStr2 & vbCrLf
Text1.Refresh

'  Now decrypt both strings, and the results are correct
'  in both cases:
Dim rsa2 As New ChilkatRsa
rsa2.ImportPrivateKey privateKey
rsa2.EncodingMode = "base64"
usePrivateKey = 1

Dim decryptedStr1 As String
decryptedStr1 = rsa2.DecryptStringENC(encryptedStr1,usePrivateKey)
Text1.Text = Text1.Text & decryptedStr1 & vbCrLf
Text1.Refresh

Dim decryptedStr2 As String
decryptedStr2 = rsa2.DecryptStringENC(encryptedStr2,usePrivateKey)
Text1.Text = Text1.Text & decryptedStr2 & vbCrLf
Text1.Refresh


Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.