-
-
(note that sample code
is in VB)
| Q: Add
wodCrypt to my project? |
1. Install the
product using wodCrypt.EXE setup package.
2. In VB IDE, open your project (or create new)
3a. Go to menu
'Project', then select 'Components', find 'WeOnlyDo! COM Crypt
ActiveX Control', click on it and select 'OK'
3b. Find wodCrypt's small icon on toolbox, drag it and drop it
to your form
... or, if you
prefer to use COM object (slightly faster) ...
3a. Go to menu
'Project', then select 'References', find 'WeOnlyDo! COM Crypt
Component', click on it and select 'OK'
3b. Open your code, find (General)(Declarations) and add this
line:
Dim WithEvents Crypt1 As wodCryptCom
3c. In Form_Load sub add this line as well
Set Crypt1
= New wodCryptCom
You can use
code like this:
' declare
blob object that will keep our files
Dim
infile As FileBlob
Dim outfile As FileBlob
' open
original file
Set infile = New FileBlob
infile.FileName = "c:\test.txt"
'
create output file
Set outfile = New FileBlob
outfile.FileName = "c:\test.enc"
'
select cipher
Crypt1.Type = AES
' set
password
Crypt1.SecretKey = "my secret password"
' and,
finally, encrypt it
Crypt1.Encrypt infile, outfile
'
destroy infile and outfile objects
' so file handles are released
Set infile = Nothing
Set outfile = Nothing
Decrypt is very
similar to encrypt. In above (Encrypt) snippet, replace
Crypt1.Encrypt infile, outfile
with
Crypt1.Decrypt outfile, infile
You
might want to change filenames, so original test.txt you started
with doesn't get overwritten!
This is
basically the same as encrypting files, except you might want to
use MemBlob instead of FileBlob.
Also, since encrypted text is
containing binary data, you might want to
use BASE64 encoding of resulting text. So, you can try something
like this:
' declare
blob object for our text
Dim
intext As New MemBlob
Dim outtext As New MemBlob
' feed
original file
intext.Text = "This is the text I want to protect"
'
select cipher
Crypt1.Type = AES
' set
password
Crypt1.SecretKey = "my secret password"
' and,
finally, encrypt it
Crypt1.Encrypt intext, outtext
' show
me what we got
Debug.Print outtext.ToBase64
Kf///95IIxi+Z4RK4c3NzXYqR0YyWl//8gMpZcUtjgySDVtkjLVQXJD1JJUeomi2kUgh31b4GF0N1Wg4hMIW3A==
Same sample as above, just
replace Encrypt with Decrypt, and swap intext and outtext.
| Q: Create
my own private key to sign a document? |
You can use our
wodKeys component to create, load and save private (and public)
keys.
Creating new private key (that is used to Sign data) is as
easy as this:
Dim key As
New Keys
key.Generate RSAkey
key.Save "c:\mykey.rsa", "some password"
Once key is
generated, you should NEVER send it to anyone - it is your only
and you should keep it in the safe place!!
If someone wants to
verify your signature, you should use key.PublicKey property to
get your public key and send it to that person.
Public keys are
derived from private ones, and can be used by 3rd party to
verify your signatures -
but they can never be used to sign
other documents, pretending to be you!
Your document
can be either stored in some file (in which case you would use
FileBlob as in above Encrypt snippet),
or can be in some
variable (in which case you might want to use MemBlob and
populate it through MemBlob.Text property).
You must know that
making Signatures is very time consuming process! You should
*never* try to sign real documents.
Rather, you should create
MD5 or SHA1 digest values of your documents, and then sign
those.
It is much faster to sign 16-20 bytes, than few (hundred)
kilobytes.
Prepare your
private key for this! You can look above to see how to generate
it.
So, try
something like this:
' declare
blob object that will keep our files
Dim
infile As New FileBlob
Dim hash As MemBlob
Dim outtext As New MemBlob
' feed
file that should be signed
Set
infile = New FileBlob
infile.FileName = "c:\test.txt"
'
initialize output blob to store hash (15-20 chars) value
Set
hash = New MemBlob
'
initialize output blob to store signature
Set
outtext = New MemBlob
'
select cipher for making hash
Crypt1.Type = MD5
'
create hash
Crypt1.Digest infile, hash
'
select cipher for making signature
Crypt1.Type = RSA
' load
your private key
Dim key As New Keys
key.Load "c:\mykey.rsa", "some password"
' feed
your private key to wodCrypt
Crypt1.SecretKey = key
' and,
finally, sign it
Crypt1.Sign hash, outtext
' look
what we got
Debug.Print outtext.ToBase64
DDLqG8lFsJGcasy1ZZYP1SD6yY5PsODAjL+b0N3ZHuImDPf6pSaer06uTE/rnzTyAn
MJD4C+/KvJE5H2AfXuLdKRMijBj4nfiCFpLTovgUhfEUniQd8hflaFgMeveGH1SVtC
TBOnIKLUZ/+XZw4F3oFq6I816unPLKQegxtIHNg=
| Q: Verify
signature on the document |
To verify
signature, you need the copy of the document, signature data
supplied by the person who signed it,
and his public key. Make
sure public key really belongs to the person, otherwise you
might verify forged signature
with a forged public key - and get
successful result!
If you're sure
you have real public key, you can try to use code like this:
( I assume here publickey variable has valid public key!)
' declare
blob objects
Dim
infile As New FileBlob
'original file
Dim
hash As MemBlob
'temporary
hash
Dim
signature As MemBlob
'signature
received from the other party
' feed
original file
Set infile = New FileBlob
infile.FileName = "c:\test.xml"
'
initialize output blob to store hash (15-20 chars) value
Set
hash = New MemBlob
'
select cipher for making hash
Crypt1.Type = MD5
'
create hash
Crypt1.Digest infile, hash
'
select cipher for making signature
Crypt1.Type = RSA
' feed
signature
Set
signature = New MemBlob
signature.FromBase64 ("DDLq........6unPLKQegxtIHNg=")
' set
owner's public key
Crypt1.SecretKey = publickey
' now
verify the signature
If
(Crypt1.Verify(hash, signature) = True) Then
MsgBox "Signature is valid!"
Else
MsgBox "Signature is forged!!"
End If
|