|
Post by rabehpg on Nov 27, 2011 9:18:02 GMT -5
Hello all.
My English is not good.
Tired of many in the search for a way to use encryption 3des - des using vbcorlib
Please explain the examples or for that
Good luck Thanks in advance.
|
|
|
Post by Kelly Ethridge on Nov 27, 2011 17:47:39 GMT -5
Hello,
Using the cryptography classes in VBCorLib is very similar to how the same classes in .NET are used. So any example found in .NET can be used as a learning tool. With that said, here is an example of encrypting then decrypting a file with 3DES.
Option Explicit
Public Sub Main() Const Password As String = "abc" Const FileToEncrypt As String = "file.txt" Const FileToDecrypt As String = "file.dat" Const DecryptedFile As String = "file2.txt" EncryptFile FileToEncrypt, Password DecryptFile FileToDecrypt, DecryptedFile, Password End Sub
Private Sub EncryptFile(Filename As String, Password As String) Dim Key() As Byte Key = GenerateKey(Password, 192 \ 8)
Dim Provider As New TripleDESCryptoServiceProvider Provider.Key = Key Provider.IV = NewBytes(0, 0, 0, 0, 0, 0, 0, 0) Dim SourceFile As Stream Set SourceFile = File.OpenFile(Filename, OpenExisting) Dim EncryptedFilename As String EncryptedFilename = Path.ChangeExtension(Filename, "dat") Dim TargetFile As Stream Set TargetFile = File.OpenFile(EncryptedFilename, Create) Dim Encryptor As CryptoStream Set Encryptor = Cor.NewCryptoStream(TargetFile, Provider.CreateEncryptor, WriteMode) Dim Buffer(0 To 4095) As Byte Dim Count As Long Count = SourceFile.ReadBlock(Buffer, 0, 4096) Do While Count > 0 Encryptor.WriteBlock Buffer, 0, Count Count = SourceFile.ReadBlock(Buffer, 0, 4096) Loop Encryptor.CloseStream SourceFile.CloseStream TargetFile.CloseStream End Sub
Private Sub DecryptFile(SourceFilename As String, TargetFilename As String, Password As String) Dim Key() As Byte Key = GenerateKey(Password, 192 \ 8)
Dim Provider As New TripleDESCryptoServiceProvider Provider.Key = Key Provider.IV = NewBytes(0, 0, 0, 0, 0, 0, 0, 0) Dim SourceFile As Stream Set SourceFile = File.OpenFile(SourceFilename, OpenExisting) Dim TargetFile As Stream Set TargetFile = File.OpenFile(TargetFilename, Create) Dim Decryptor As CryptoStream Set Decryptor = Cor.NewCryptoStream(TargetFile, Provider.CreateDecryptor, WriteMode) Dim Buffer(0 To 4095) As Byte Dim Count As Long Count = SourceFile.ReadBlock(Buffer, 0, 4096) Do While Count > 0 Decryptor.WriteBlock Buffer, 0, Count Count = SourceFile.ReadBlock(Buffer, 0, 4096) Loop Decryptor.CloseStream SourceFile.CloseStream TargetFile.CloseStream End Sub
Private Function GenerateKey(Password As String, ByVal Length As Long) As Byte() Dim Gen As Rfc2898DeriveBytes Set Gen = Cor.NewRfc2898DeriveBytes(Password, NewBytes(0, 0, 0, 0, 0, 0, 0, 0)) GenerateKey = Gen.GetBytes(Length) End Function
|
|
|
Post by rabehpg on Nov 28, 2011 9:03:53 GMT -5
thanks man
i want encrypt and decrypt string :
for example
DA65344511C953D8 Encrypt 3DES AF38DA5E9E00AADB39A1BCBCB060DEA3 = E046C69E8CB4A40D
DA65344511C953D8 = data AF38DA5E9E00AADB39A1BCBCB060DEA3= key E046C69E8CB4A40D= data Encrypt Encrypt 3DES = Algo
How is that ؟؟؟
Good luck Thanks in advance.
|
|
|
Post by Kelly Ethridge on Nov 28, 2011 12:51:38 GMT -5
This should get you started:
Option Explicit
Public Sub Main() Const HexKey As String = "0xAF38DA5E9E00AADB39A1BCBCB060DEA3" Const HexData As String = "0xDA65344511C953D8" Const HexEncrypted As String = "0xE046C69E8CB4A40D" Dim Key() As Byte Key = BInt(HexKey).ToArray cArray.Reverse Key
Dim Data() As Byte Data = BInt(HexData).ToArray cArray.Reverse Data
Dim Provider As New TripleDESCryptoServiceProvider Provider.Key = Key Provider.IV = NewBytes(0, 0, 0, 0, 0, 0, 0, 0) Provider.Padding = PaddingMode.None Dim T As ICryptoTransform Set T = Provider.CreateEncryptor Dim Encrypted() As Byte Encrypted = T.TransformFinalBlock(Data, 0, cArray.GetLength(Data)) PrintArray Encrypted End Sub
Private Sub PrintArray(ByRef Data() As Byte) Dim i As Long For i = LBound(Data) To UBound(Data) Debug.Print cString.Format("{0:X2}", Data(i)); Next Debug.Print End Sub
|
|
|
Post by rabehpg on Nov 28, 2011 15:10:18 GMT -5
Thank you. This is exactly what you're looking for.
Thanks again
Good luck
|
|
|
Post by rabehpg on Dec 2, 2011 2:58:20 GMT -5
Hello My brother.
My brother sorry for the inconvenience.
I found a problem in the 3des encrypt & 3des decrypt
When you attempt to encrypt or decrypt data in this form:
data = 0x0065344511C953D8
Problem occurs and the data size becomes only 7 bytes
How can I overcome the problem is 00 the first data or the
encryption key
Thank you in advance
Again sorry for the inconvenience
|
|
|
Post by Kelly Ethridge on Dec 2, 2011 11:12:53 GMT -5
Hello,
The BigInteger's ToArray method will always return the smallest byte array possible. This means that any leading zeros will be excluded in the array. You will have to manually add any leading zeros you need by using ReDim Preserve Data(0 To NewUpperBound).
Option Explicit
Public Sub Main() Dim b As BigInteger Set b = BInt("0x0065344511C953D8") Dim Data() As Byte Data = b.ToArray ReDim Preserve Data(0 To 7) cArray.Reverse Data Dim i As Long For i = 0 To UBound(Data) Debug.Print cString.Format("{0:X2}", Data(i)); Next Debug.Print End Sub
Hope this helps.
|
|
|
Post by rabehpg on Dec 2, 2011 12:30:25 GMT -5
Thank you.
Solved the problem. By:
Re-define the size of the data, as you said.
This is exactly what I was looking for
+
This library I have solved all the problems
Thank you my brother again.
Good luck
|
|