PowerBASIC Forums
  Programming
  Need a very fast way to mangle text (no protection)

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   Need a very fast way to mangle text (no protection)
Edwin Knoppert
Member
posted August 23, 2000 04:44 AM     Click Here to See the Profile for Edwin Knoppert     Edit/Delete Message   Reply w/Quote
Need a procedure wich mangles text a little.
It must be VERY fast, allmost not noticable compared to the original.

For example, a byte's bits could be shifted, this takes to long for complete strings.
Shifting complete strings seems to be impossible.

Every text length can occur, from 1 byte to let's say 100bytes.

"ABCDEFGHIJKLMNO" should be made unreadable.
It's not important that it's simply to discover what 'encryption' was used.

How to shift a complete string in one command (and back)?

Thanks,

------------------
ebknoppert@hotmail.com

IP: Logged

Borje Hagsten
Member
posted August 23, 2000 05:30 AM     Click Here to See the Profile for Borje Hagsten     Edit/Delete Message   Reply w/Quote
The following, from my TextCase sample, avaliable for download from: http://www.tolken99.com/pb/pbfile_e.htm is pretty fast and maybe
can be of help.

Use as: CALL asmROT13(txt)


'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
' asmROT13 encryption/decryption - no extended ANSI here (a-z, A-Z only).
' Repeat action to encrypt/decrypt
'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
SUB asmROT13(a$)
LOCAL ln AS LONG
ln = LEN(a$) ' get string's len into ln

! cmp ln, 0 ; does the string have a length?
! je Exit13Loop ; exit if zero

! mov eax, a$ ; eax = pointer to string handle
! mov eax, [eax] ; eax = pointer to string data

Bgn13Loop:
! mov edx, [eax] ; move current char into edx
! cmp ln, 0 ; exit when ln = zero
! jne Continue13Loop
! jmp Exit13Loop ; jump out on end of string

Continue13Loop:
! cmp dl, 65 ; CASE 65 TO 90
! jb NoRot ; if dl < 65 then get next character
! cmp dl, 90
! jbe Rot13Upper ; if dl <= 90
! cmp dl, 97 ; CASE 97 TO 122
! jb NoRot ; if dl < 97 then next character
! cmp dl, 122
! jbe Rot13Lower ; if dl <= 122
! jmp NoRot ; CASE ELSE - get next character

Rot13Lower:
! add dl, 13 ; Rotate 13 steps forward
! cmp dl, 122
! ja AdjChar ; if dl became > 122
! mov [eax], dl ; else write changed char back into a$
! jmp NoRot

Rot13Upper:
! add dl, 13 ; Rotate 13 steps forward
! cmp dl, 90
! ja AdjChar ; if dl became > 90
! mov [eax], dl ; else write changed char back into a$
! jmp NoRot

AdjChar:
! sub dl, 26
! mov [eax], dl ; else write changed char back into a$
! jmp NoRot

NoRot:
! inc eax
! dec ln ; decrease the ln (length) counter
! jmp bgn13Loop

Exit13Loop:

END SUB

------------------

IP: Logged

Peter Lameijn
Member
posted August 23, 2000 07:17 AM     Click Here to See the Profile for Peter Lameijn     Edit/Delete Message   Reply w/Quote

'==============================================================================
'Scramble - Very simple and fast scrambling of a string
' For descrambling : call sub again
' The MOD is added to prevent adjacent chars to be scrambled equal.
'------------------------------------------------------------------------------
Sub Scramble (pStr As String)
Local lStrPtr As Byte Ptr, lCnt As Long
lStrPtr = StrPtr (pStr)
For lCnt = 0 To Len(pStr) -1
@lStrPtr[lCnt] = @lStrPtr[lCnt] Xor (lCnt Mod 4) + 1
Next
End Sub

Function PbMain() As Long
Local pStr As String
pStr = Command$
Print "Before :" ;pStr
Scramble pStr
Print "Scrambled :" ;pStr
Scramble pStr
Print "After :" ;pStr
End Function

------------------
Peter.
peterL@pabx-group.com

[This message has been edited by Peter Lameijn (edited August 23, 2000).]

IP: Logged

Edwin Knoppert
Member
posted August 23, 2000 11:57 AM     Click Here to See the Profile for Edwin Knoppert     Edit/Delete Message   Reply w/Quote
Cool!

I think the ASM procedure will do very well here..

T! = Timer

AA = repeat$( 100, "ABCDEFABCDEFABCDEFABCDEFABCDEF" )

For a = 1 to 10000

ASMROT13 AA

Next a

Took about ~2.5 seconds on my P333.

My strings will allways around 10 to 20 char's.

Repeat (1) took : 4.84374999941792E-2 seconds

--------------------

The for next and XOR is known to me, i assumed it would be very slow.

------------------
ebknoppert@hotmail.com

IP: Logged

Peter Lameijn
Member
posted August 23, 2000 12:42 PM     Click Here to See the Profile for Peter Lameijn     Edit/Delete Message   Reply w/Quote
Actually, the asm code takes 1.21 secs on my PII-350, and the scramble routine 3.01 secs.
If you change the MOD to an AND (MOD seems to be slower) then the basic code runs in 0.62 secs
(The speed is in the pointer use...)

Sub Scramble (pStr As String)
Local lStrPtr As Byte Ptr, lCnt As Long
lStrPtr = StrPtr (pStr)
For lCnt = 0 To Len(pStr) -1
@lStrPtr[lCnt] = @lStrPtr[lCnt] Xor (lCnt And 6) + 1
Next
End Sub

------------------
Peter.
peterL@pabx-group.com

IP: Logged

Edwin Knoppert
Member
posted August 24, 2000 01:40 PM     Click Here to See the Profile for Edwin Knoppert     Edit/Delete Message   Reply w/Quote
Indeed, the scramble is faster, i'll reconsider.

Used;
AA = repeat$( 100, "ABCDEFABCDEFABCDEFABCDEFABCDEF" )

Results;
ROT13 2.69968750000407
SCRAMBLE1 @C@G@CFEBEFEDGDCDGBAFABA@C@G@CFEBEFEDGDCDGBAFABA@C@G@CFEBEFE
SCRAMBLE2 ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEF
SCRAMBLE 1.74656250000407

Thank you both, both procedures are preserved.

------------------
ebknoppert@hotmail.com

IP: Logged

Peter P Stephensen
Member
posted August 25, 2000 01:06 AM     Click Here to See the Profile for Peter P Stephensen     Edit/Delete Message   Reply w/Quote
Try this one:

#compile exe
#dim all
#register none

declare function GetFile(byval File as string, Buffer as string) as long
declare function PutFile(byval File as string, Buffer as string) as long

function Scrample(File as string, NewFile as string) as long

local pl as long pointer
local Buffer as string
local n as long, i as long

if ucase$(File) = ucase$(NewFile) then exit function ' security
if GetFile(File, Buffer) = 0 then exit function

pl = strptr(Buffer)
n = len(Buffer)/4

for i = 1 to n
@pl = - @pl
incr pl
next i

PutFile NewFile, Buffer

function = 1

end function

'===============================

function pbmain

local t as double

t = timer

Scrample "c:\pbdll60\winapi\win32api.inc", "c:\pbdll60\winapi\win32api.scp"

msgbox format$(timer-t,"#.###"),,"Time consumed..."

end function

'===============================

function GetFile(byval File as string, Buffer as string) as long

local hFile as long

err = 0 : hFile = freefile

open file for binary as hFile
if err then exit function
get$ hFile, lof(hFile), Buffer
close hFile

function = 1

end function

'===============================

function PutFile(byval File as string, Buffer as string) as long

local hFile as long

err = 0 : hFile = freefile

open File for binary as hFile
if err then exit function
put$ hFile, Buffer
close hFile

function = 1

end function

------------------

[This message has been edited by Peter P Stephensen (edited August 25, 2000).]

IP: Logged

Borje Hagsten
Member
posted August 25, 2000 09:51 AM     Click Here to See the Profile for Borje Hagsten     Edit/Delete Message   Reply w/Quote
If security isn't that important, but speed is, test the following:


SUB asmScramble (a$)
LOCAL ln AS LONG
ln = LEN(a$) ' get string's len into ln

! cmp ln, 0 ; does the string have a length?
! je ExitScLoop ; exit if zero

! mov eax, a$ ; eax = pointer to string handle
! mov eax, [eax] ; eax = pointer to string data

BgnScLoop:
! mov edx, [eax] ; move current char into edx
! cmp ln, 0 ; exit when ln = zero
! jne ContinueScLoop
! jmp ExitScLoop ; jump out on end of string

ContinueScLoop:
! xor dl, 2 ; xor dl with 2 (or 1, 3, whatever..)
! mov [eax], dl ; write changed char back into a$
! inc eax ; get next character
! dec ln ; decrease the ln (length) counter
! jmp bgnScLoop

ExitScLoop:
END SUB

------------------

IP: Logged

G Grant
Member
posted August 25, 2000 04:20 PM     Click Here to See the Profile for G Grant     Edit/Delete Message   Reply w/Quote
You'd get a significant speed improvement I think if you used a
32 bit mask and then XORed your string 4 characters at a time. Just
pass your XOR mask along with the string (that way your mask can
vary), and extend it to all 4 bytes of a 32 bit register. When you
get to less than 4 bytes remaining in the string, XOR the remaining
bytes one at a time. This would make more difference on larger
strings of course, but if you use small strings on average, then
one byte at a time would probably be better. Just an idea you might
want to experiment with.

------------------

IP: Logged

Edwin Knoppert
Member
posted August 26, 2000 12:49 AM     Click Here to See the Profile for Edwin Knoppert     Edit/Delete Message   Reply w/Quote
It's already very fast, read: acceptable.
However, i would have asked for an eq. for the for/next version
And there it is, i don't prog. in asm, the remarks are ok to adapt it a little.

Note that it's indeed not a meant to encrypt data but simply make it unreadable.
A (dynamic) string up to 2mb must be converted but only at fileread and filewrite.
The filewrite/update could happen often in this app.
Therefore speed is important.

Thanks!

------------------
ebknoppert@hotmail.com

IP: Logged

Edwin Knoppert
Member
posted August 26, 2000 12:54 AM     Click Here to See the Profile for Edwin Knoppert     Edit/Delete Message   Reply w/Quote
BTW,
This stuff is all part of another thingy.
The file is read in a ordinary string and decrypted.
But the string is constantly changed in length to append data and even remove data.
This is slowing the app because of these resizes.
I REALLY prefer to use a string because i can't know how long the data will be.
Can i better use API with globalalloc or something or is PB equal in speed?

??

------------------
ebknoppert@hotmail.com

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c