Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data compression and encryption source code 1

Status
Not open for further replies.

dslaby

Programmer
May 18, 1999
7
0
0
US
I need to compress and encrypt a replicated data set using email. I have written the code that attaches the data file to the email message, but need source code for compressing and encrypting the file, and of course, uncompressing and decrypting the file after recieved. Anybody has any source code or locations where I can get some? Likewise, I am willing to share my code for attaching and sending email using smtp from VB. Thanks.
 
Do you especially need to encrupt the data? There is a windows API call that will compress it.<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Here's a link that should provide everything you need:<br>
<A HREF=" TARGET="_new">These guys have been in business for years and their data compression libraries will probably provide better results than any of our meager attempts to re-invent the wheel.<br>
But, Mike, what's the name of that compression API? Dslaby might be a do-it-yourselfer (or unwilling to use a third party solution). If you provide the API, I'll provide a short (but hard to beat) encryption routine.<br>
 
If you post the API call, and alt255 posts some code, I'll be happy to post my final result for anybody's review. Thanks.
 
These are the function definitions for *reading* a compressed file. Hadn't really looked at them before (just made a mental note that there was compression stuff) and I don't see calls to create lz files, just to read them. Hmmm... I'll keep on looking.<br>
<br>
Mike<br>
<br>
'**********************************<br>
'** Function Declarations: <br>
<br>
#if WIN32 Then<br>
Private Declare Function LZStart& Lib &quot;lz32.dll&quot; ()<br>
Private Declare Function LZSeek& Lib &quot;lz32.dll&quot; (ByVal hfFile As Long, ByVal lOffset As Long, ByVal nOrigin As Long)<br>
Private Declare Function LZRead& Lib &quot;lz32.dll&quot; (ByVal hfFile As Long, ByVal lpvBuf As String, ByVal cbread As Long)<br>
Private Declare Function LZOpenFile& Lib &quot;lz32.dll&quot; Alias &quot;LZOpenFileA&quot; (ByVal lpszFile As String, lpOf As OFSTRUCT, ByVal style As Long)<br>
Private Declare Function LZInit& Lib &quot;lz32.dll&quot; (ByVal hfSrc As Long)<br>
Private Declare Sub LZDone Lib &quot;lz32.dll&quot; ()<br>
Private Declare Function LZCopy& Lib &quot;lz32.dll&quot; (ByVal hfSource As Long, ByVal hfDest As Long)<br>
Private Declare Sub LZClose Lib &quot;lz32.dll&quot; (ByVal hfFile As Long)<br>
Private Declare Function GetExpandedName& Lib &quot;lz32.dll&quot; Alias &quot;GetExpandedNameA&quot; (ByVal lpszSource As String, ByVal lpszBuffer As String)<br>
Private Declare Function CopyLZFile& Lib &quot;lz32.dll&quot; (ByVal n1 As Long, ByVal n2 As Long)<br>
#endif 'WIN32<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
dslaby -<br>
<br>
For your encryption, check out Bruce Schneier's web site at <A HREF=" TARGET="_new"> He's huge in the Crypto field (has a regular column in Dr. Dobb's Journal). Be careful where you distribute your application. 40-bit encryption is the current legal limit to export from the US. Some countries (like France) do not permit civilians to use any encryption software whatsoever. Violate the US laws, and you will get locked up under the same statutes as if you had exported a nuclear bomb or poison gas.<br>
<br>
Your safest bet would be to license a library/DLL from someone who has already jumped through the legal hurdles (like RSA or Counterpane or someone else) and can advise you on international use of their library.<br>
<br>
<br>
You need to get a copy of &quot;The Data Compression Book 2nd Edition&quot; by Mark Nelson. I've got the 1st edition, and it is very good. Only drawback is that it was written for C programmers.<br>
<br>
You might want to use the Micosoft LZW DLL like Mike Lacey suggested, but I'm not sure if you would have to pay royalties to Unisys (they hold the patent on the LZW compression algorithm -- see <A HREF=" TARGET="_new"><br>
Chip H.<br>
 
Here's a simple XOR encryption algorithm that I use now and then. This code has been tested:<br>
<br>
Public Function Encrypt(UnEncrypted As String, ByVal Key As String) As String<br>
'Simple XOR Incryption With Key<br>
'This function takes two paramaters. One is the Encrypted or Non-Encrypted string,<br>
'the other is the Key to encrypt or un-encrpyt with. It returns the encrypted or<br>
'un-encrypted string result. EncryptedString = Encrypt(StringToEncrypt, EncryptionKey).<br>
'Then call the function again to decrypt the string<br>
'RealString = Encrypt(EncryptedSting, EncryptionKey).<br>
'<br>
'You must use the same key to decrypt the string as you do to encrypt it.<br>
'<br>
'Sample usage:<br>
'<br>
' strString = Encrypt(&quot;January 1, 2000&quot;, &quot;ABC&quot;)<br>
' Debug.Print Encrypt(strString, &quot;ABC&quot;)<br>
'<br>
<br>
Dim lngOffset As Long<br>
<br>
Do Until Len(Encrypt) = Len(UnEncrypted)<br>
For lngOffset = 1 To Len(Key)<br>
If Len(Encrypt) = Len(UnEncrypted) Then Exit For<br>
Encrypt = Encrypt & Chr(Asc(Mid(UnEncrypted, Len(Encrypt) + 1, 1)) Xor Asc(Mid(Key, lngOffset, 1)))<br>
Next lngOffset<br>
Loop<br>
End Function<br>
<br>
I also found some code that does RLE (Run Length Encoding) in VB. I can't vouch for this code as I have not used it or tested it. Use this code at your own risk. Having said that, here's the code:<br>
<br>
Option Explicit<br>
<br>
Public Sub Main()<br>
Dim TempArr() As Byte<br>
Dim TempStr As String<br>
TempStr = &quot;TTTTHHHHIIIISSSS a Test&quot;<br>
RLECompress TempStr, TempArr<br>
TempStr = TempArr<br>
TempStr = RLEUncompress(TempArr)<br>
End Sub<br>
<br>
Public Sub RLECompress(StrToCompress As String, ByteArr() As Byte)<br>
Dim TempArr() As Byte<br>
Dim I As Integer<br>
Dim ByteCnt As Byte<br>
Dim PrevAdded As Boolean<br>
TempArr = StrConv(StrToCompress, vbFromUnicode)<br>
ReDim ByteArr(0)<br>
ByteArr(0) = TempArr(0)<br>
For I = 1 To UBound(TempArr)<br>
If TempArr(I) = TempArr(I - 1) Then<br>
ByteCnt = I<br>
Do Until (TempArr(ByteCnt) &lt;&gt; TempArr(I - 1)) Or (ByteCnt - (I - 1) = 254)<br>
ByteCnt = ByteCnt + 1<br>
If ByteCnt &gt; UBound(TempArr) Then Exit Do<br>
Loop<br>
If UBound(ByteArr) = 0 Then<br>
ReDim Preserve ByteArr(UBound(ByteArr) + 1)<br>
ElseIf PrevAdded = False Then<br>
ReDim Preserve ByteArr(UBound(ByteArr) + 2)<br>
End If<br>
ByteArr(UBound(ByteArr) - 1) = (ByteCnt - (I - 1))<br>
ByteArr(UBound(ByteArr)) = TempArr(I)<br>
I = ByteCnt - 1<br>
PrevAdded = False<br>
Else<br>
ReDim Preserve ByteArr(UBound(ByteArr) + 2)<br>
ByteArr(UBound(ByteArr) - 1) = 1<br>
ByteArr(UBound(ByteArr)) = TempArr(I)<br>
PrevAdded = True<br>
End If<br>
Next I<br>
End Sub<br>
<br>
Public Function RLEUncompress(ByteArr() As Byte)<br>
Dim I As Integer<br>
For I = 0 To UBound(ByteArr) Step 2<br>
RLEUncompress = RLEUncompress & String(ByteArr(I), Chr(ByteArr(I + 1)))<br>
Next I<br>
End Function<br>
<br>
<p>Steve Meier<br><a href=mailto:sdmeier@jcn1.com>sdmeier@jcn1.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top