• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Simple Data Encryption

Started by Theo Gottwald, November 18, 2014, 10:59:58 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

In Cryptology we have a difference between "Hiding" and "Encryption".
This is more like "Hiding". The Data is XOR'ed against itself.

-> Byte 1 is XOR'ed against the last Byte
-> Byte 2 against the pre-last Byte
->  and so on ...

This is better, then to XOR things against a Single Number.

In some cases (when you have strings like "aaaaa") it is worse then an XOR against a "One Time Pad" - at least with a low number of rounds.

In other cases (when you use a "one time Pad multiple times", this thing here is possibly a better choice, especially when you:

1. Add a fixed-Lenght Key in front of the Data ...
2. Call the Subprogramm multiple times (like you see in the SUB below).
3. Remove the Key after decryption

Example:

XByte_Hide(A$,1024)

Of course to decrypt it, you have to use the same number of "Rounds".

PS: Does anybody like to test, if after a specific number of rounds, the Original Text could theoretically be back?

'##################################################################################################
'
'##################################################################################################
'--------------------------------------------------------------------------------
' XOR b$ byteweise verschlüsseln
SUB XByte2(BYREF b$)
#REGISTER NONE
LOCAL av&,bv&,cv AS BYTE
av&=STRPTR(b$):bv&=LEN(b$)-1
' eax -> Startadresse ecx -> Anzahl
! MOV eax,av&
! MOV ecx,0
! MOV ebx,0
Ly0:
! MOV BL,[eax+ecx] ; Hole erstes Byte aus Memory
! INC ecx
! MOV BH,[eax+ecx] ; Hole nächstes Byte aus Memory
! MOV cv,BL
! XOR BH,cv
! MOV [eax+ecx],BH ; Lege zurück in Speicher
! CMP ecx,bv&
! JNA Ly1
! JMP Ly2
Ly1:
! JMP Ly0
Ly2:
! NOP
END SUB
'##################################################################################################
'
'##################################################################################################

' XOR b$ byteweise entschlüsseln
SUB XByte3(BYREF b$)
#REGISTER NONE
LOCAL av&,bv&,cv AS BYTE
av&=STRPTR(b$):bv&=LEN(b$)
' eax -> Startadresse ecx -> Anzahl
! MOV eax,av&
! MOV ecx,bv&
! MOV ebx,0
Ly0:
! MOV BH,[eax+ecx] ; Hole letztes Byte aus Memory
! DEC ecx
! MOV BL,[eax+ecx] ; Hole vorletztes
! MOV cv,BL
! XOR BH,cv
! INC ecx
! MOV [eax+ecx],BH ; Lege zurück in Speicher
! DEC ecx
! CMP ecx,0
! JNZ Ly0
Ly2:
! NOP
END SUB
'##################################################################################################
'
'##################################################################################################
SUB XByte_Hide(BYREF U01 AS STRING,OPT BYVAL U02 AS DWORD)
   REGISTER R01 as LONG,R02 as LONG
   R01=LEN(U01)
   IF R01<2 THEN GOTO enx
   IF U02 THEN R01*=U02
   FOR R02=1 TO R01
       XByte2(U01)
   NEXT
   enx:
END SUB
'##################################################################################################
'
'##################################################################################################
SUB XByte_UnHide(BYREF U01 AS STRING,OPT BYVAL U02 AS DWORD)
   REGISTER R01 as LONG,R02 as LONG
   R01=LEN(U01)
   IF R01<2 THEN GOTO enx
   IF U02 THEN R01*=U02
   FOR R02=1 TO R01
       XByte3(U01)
   NEXT
   enx:
END SUB
'##################################################################################################
'
'##################################################################################################