• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Source Code --- Example O2 Code -- Beginer Encryption and decryption

Started by Chris Chancellor, May 04, 2018, 11:01:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello all

this is an example O2 code for Beginer Encryption and decryption

Thanxx to Charles help on this code


' BgEncryptor.o2bas
$ filename "BgEncryptor.exe"
use rtl64
use corewin

  uses chaos

! GetTickCount lib "kernel32.dll" alias "GetTickCount" () as   sys
! srand lib "msvcrt.dll" (int seed)
! rand lib "msvcrt.dll" () as int
   seed=GetTickCount

   def Trim ltrim(rtrim(%1))

'=============================
' To seed the pseudorandom-number
sub Randomize()
      srand GetTickCount()
end sub


function StrReverse(string givenSt) as string
=======================================
int LenSt=len givenSt
int j=LenSt
int i
string RevStt=nuls LenSt
byte StOrig at strptr givenSt
byte StRev at strptr RevStt
indexbase 1

for i=1 to LenSt
  StRev[j]=StOrig[i]
  j--
next
return RevStt
end function



'==============================
' convert string to hex
FUNCTION Str2Hex(givenStr AS STRING) AS STRING

LOCAL i  AS LONG
Local  sHex AS STRING
byte  bbt
dim as byte pB at strptr(givenStr)

' need to start from element 1
  FOR i = 1 TO LEN(givenStr)
    bbt = pB[i]
    sHex += HEX(bbt,2)
  NEXT i
  return sHex
END FUNCTION       



'==============================
' convert  hex to string -- removing a 38 padding
' to the hex string value

FUNCTION  Hex2Str38(givenHexStr AS STRING) AS STRING
   Local  j     As Long

   Local AscStr   As String
   Local HexChar   As String
   Local CharPtr ,  imChar  As Long

        AscStr  = ""
       CharPtr = -1

      '    similar to       CHR$(VAL("&h" & MID$(sLine,lCnt,2)))
For j = 1 To Len(givenHexStr) \ 2
         CharPtr = CharPtr + 2
         HexChar = Mid(givenHexStr,CharPtr,2)
          imChar  = Val("&H" & HexChar) - 38         
         AscStr  += Chr( imChar)
Next j
   
  return  AscStr


END FUNCTION






'====================================================
' Encryption by Beginner Special
' Note that the 38 numeric padding is to convert the original Hex code
' to a different code so that it is not readily seen by the user
FUNCTION BeginnerspEN_C(BYVAL givSt AS STRING ) AS STRING
    LOCAL ia,FourLengSt,LengSt,ka AS LONG
   
    LOCAL RndByte AS  Long   
    Local  RndSt as string

    LengSt = LEN(givSt)
    FourLengSt = LengSt * 4

     string RetSt =  nuls  FourLengSt
     string RetStRev  =  nuls  FourLengSt

    indexbase 1

    RANDOMIZE
    ka = 0
    FOR ia = 1 TO LengSt

  '      Hex the given string character and patch into the RetSt
  '      and added a 38 numeric padding
         ka++
         mid RetSt,ka,hex(asc(givSt,ia)+38,2)
         ka++

'       interleave each character of the given string with a random string
'        and patch into the RetSt  and added a 38 numeric padding
         RndByte   = iRND(66, 126)
         RndSt = Chr(RndByte)
         ka++
          mid RetSt,ka,hex(asc(RndSt)+38,2)
         ka++

    NEXT ia

     '  Reverse the string to add complexity to the return string
       RetStRev = StrReverse(RetSt)
   

     return RetStRev
     
END FUNCTION







'=======================================
' Decryption by Beginner Special
'
FUNCTION BeginnerspDE_C(BYVAL givSt AS STRING) AS STRING
   
    LOCAL ia,LengSt,QrtLengSt, ka , prevka AS LONG
    string partSt , RetSt , decRetSt

    LengSt = LEN(givSt)

    QrtLengSt = LengSt/4
  '  string RetSt =  nuls  QrtLengSt

    ' Reverse the string
      string StRev = StrReverse(givSt )

     indexbase 1
 

    FOR ia = 1 TO QrtLengSt
           if ia = 1 then
              ka = 1
          else
             ka = prevka +4
          end if
          partSt = mid(StRev,ka,2)
          RetSt =  RetSt + partSt

          prevka = ka
    NEXT ia

'  transform back to a decrypted string
    decRetSt = Hex2Str38(RetSt)

     return  decRetSt       
END FUNCTION
             




'-------------------------------------------------
STRING  givenSt, outEHxst , decSt  , OutEnc


givenSt = " Oxygen is the best basic compiler ever and we do NOT want toxic people in this forum"



  outEHxst =  BeginnerspEN_C(givenSt)
  OutEnc =    Hex2Str38 outEHxst

print  "ENCODED  : " + chr(13,10) +   outEHxst +
      chr(13,10) + chr(13,10) + OutEnc


' decryption
  decSt=  BeginnerspDE_C(outEHxst)
 

print  " decoded  :  " + chr(13,10) +  decSt