Basic Encrypt

Maglor

Member
Joined
Apr 29, 2007
Messages
8
Programming Experience
Beginner
I got this basic function that encrypts some text, but I don't know how to decrypt :p Can anyone help me?

VB.NET:
    Function Encrypt(ByVal Text As String, ByVal Key1 As Long, ByVal key2 As Long) As String
        Dim a As Long, tmpStr As String
        For a = 1 To Len(Text)
            tmpStr = tmpStr & Trim$(Str$(key2 * (Key1 + Asc(Mid(Text, a, 1))))) & " "
        Next a
        Encrypt = Trim$(tmpStr)
    End Function
 
I've used this a few times :

http://www.icsharpcode.com/OpenSource/SharpZipLib/Default.aspx

Basically, I'm just using a compression format that happens to support a password (and encryption) to encrypt stuff.

It creates a zip file, but you can use a MemoryStream it to save a byte array of any content. I'm using it to save encrypted images into a database at the moment. It can retrieve from a distant SQL Server database and decrypt a 100 KB image within something like 0.1 sec!

And the security is much better than Maglor's code. Actually it's as safe as you can make the password :D !

The license may be a problem for commercial applications. Even if there is a clause to allow commercial use, the GPL says you can only use it in open source applications! Conclusion : I have no idea what's the license of this thing... :(

You may find another compressed file format that supports encryption and work with streams (if you want to avoid using the disk to exchange data from the dll...) if you can't use this one...
 
Hi Stonkie

Thanks a lot for that. I was expecting a nice simple answer, but the strength of this particular type of encryption sounds too good an opportunity to pass up - it just may be over my head - I'm still a bit of a noob at all this VB stuff.

I'm just of to work now, but I'll have a look at this when I get home tonight. See if I can find my way through it.

Thanks for your input.

DominicB
 
.Net Framework also has lots of cryptography support in System.Security.Cryptography Namespace. CodeProject article .NET Encryption Simplified has a nice overview.
 
Hi JohnH

It is indeed a nice introduction to the world of encryption / decryption with some nice secure algorithms in there. I haven't got around to downloading the source files just yet, but I will do and take a look at them.

Thanks again to both of you.

DominicB
 
The decrypt function would just be the reverse:

VB.NET:
Function Decrypt(ByVal Text As String, ByVal key1 As Long, ByVal key2 As Long) As String
    Dim outText As String
    Dim nT() As String
    Dim i As Integer
    nT = Split(Text, " ")
    For i = 0 To UBound(nT)
        outText = outText & Chr((nT(i) / key2) - key1)
    Next i
    Decrypt = outText
End Function

However, if you were looking to decrypt without the keys.. that would be more difficult.
 
Hi gullible77

Thanks for coming back over this.

However, if you were looking to decrypt without the keys.. that would be more difficult.
No, I was looking to decrypt with both keys. Following the earlier posts, as an exercise I managed to do it but only as a sub - I could not get it working correctly as a function.

Thanks again.

DominicB.
 
Back
Top