Announcement

Collapse
No announcement yet.

Super simple encryption needed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Keith Waters
    replied
    Just another one of many ideas - try

    Code:
    ' --------------------------------------------------
    'FUNCTION EnCoded$ (aString$)
    '    Returns encoded aString$.  Before using, initialize the global
    '    variable, Rand.Byte?, with a key value (eg. Rand.Byte? = &B01001011).
    '
    FUNCTION EnCoded$ (aString$)
      LOCAL i%, answer$
    '
      answer$ = SPACE$(LEN(aString$))
      FOR i% = 1 TO LEN(aString$)
        MID$(answer$, i%, 1) = CHR$(ASCII(MID$(aString$, i%, 1)) + 1 + RandByte? \ 2)
      NEXT
      EnCoded$ = answer$
    END FUNCTION
    '
    ' --------------------------------------------------
    'FUNCTION DeCoded$ (aString$)
    '    Returns decoded aString$.  Before using, initialize the global
    '    variable, Rand.Byte?, with the key used at the time of encoding
    '    (eg. Rand.Byte? = &B01001011).
    '
    FUNCTION DeCoded$ (aString$)
      LOCAL i%, answer$
    '
      answer$ = SPACE$(LEN(aString$))
      FOR i% = 1 TO LEN(aString$)
        MID$(answer$, i%, 1) = CHR$(ASCII(MID$(aString$, i%, 1)) - 1 - RandByte? \ 2)
      NEXT
      DeCoded$ = answer$
    END FUNCTION
    '
    ' -------------------------------------------------------------
    'FUNCTION RandByte?
    '    Returns next byte value in random sequence of values.  The routine
    '    is seeded by setting the global, Rand.Byte?, to a value between 1
    '    and 255.  The cycle length is 216.
    '
    FUNCTION RandByte?
      SHARED Rand.Byte?
    '
      IF BIT(Rand.Byte?, 0) XOR BIT(Rand.Byte?, 3) THEN
        BIT SET Rand.Byte?, 0
      ELSE
        BIT RESET Rand.Byte?, 0
      END IF
      ROTATE RIGHT Rand.Byte?, 1
      RandByte? = Rand.Byte?
    END FUNCTION
    To test it, try

    Code:
    text$ = COMMAND$
    '
    Rand.Byte? = &B01001011
    coded$ = EnCoded$ (text$)
    '
    PRINT coded$
    '
    Rand.Byte? = &B01001011
    PRINT DeCoded$(coded$)
    Keith

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




    [This message has been edited by Keith Waters (edited February 14, 2001).]

    Leave a comment:


  • Guest's Avatar
    Guest replied
    You could use the HEX function.
    EG like this for encrypting:

    Code:
    string1$ = "abc"
    string2$ = ""
    for i = 1 to len(string1$)
      string2$ = string2$ + mid$(string1$, i, 1
    next i

    Leave a comment:


  • Mel Bishop
    replied
    If all you want is to simply encrypt is printable text along
    with some control characters, you may want to try this:

    Set up a For-Next loop, take the ASCII characters of the text
    and add 128 to convert them to the extended character set. For
    the odd numbered characters, subtract 1 and for the even
    numbered characters, add 1 (or visa-vis). Take into account the
    possibility of a "wrap-around". To decrypt, simply reverse
    the procedure.

    This would be about as simple as you can get. It won't stand up
    to any kind of serious hacking but it should keep casual
    browsers out.


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

    Leave a comment:


  • Mike Burns
    replied
    bl,

    i use that routine when in a write to a disk and read from
    a disk routine ..

    i have never used it to change a string and display it. in order to do that,
    you would need to check what the xor function would produce for
    each character. i wouldn't think that would be feasible. for a better
    way to encrypt in memory or to control the encryption results
    look at the thread
    http://www.powerbasic.com/support/pb...ead.php?t=3267

    semen has posted a nice translate routine .. using that routine and
    two tables, you could change the printable characters to other
    printable characters and back again. then you would have control
    over what each character is encrypted as ..

    mike


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

    Leave a comment:


  • Tom Hanlin
    replied
    Aliases are not allowed in these forums, BL BL. Please re-register using
    your real name.

    ------------------
    Tom Hanlin
    PowerBASIC Staff

    Leave a comment:


  • Guest's Avatar
    Guest replied
    I managed to compile it by making it a subroutine and skipping
    END FUNCTION statement. It really works good (good encryption),
    but can I limit the type of coded characters it uses in some
    simple way (a range)? As the code is now, if I encrypt the text
    'PowerBASIC' the first character 'P' will be converted to the
    paragraph character which makes it impossible(?) to insert into
    the PowerBASIC editor since it it contains illegal characters.

    If the encrypted data would always be A-Z & 0-9 then it should
    work 100% I guess... or?

    *Thanks* again and sorry for bothering you again!

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

    Leave a comment:


  • Mike Burns
    replied
    Sorry ... I missed a line on cut and paste

    Function EncryptData$(Stuffin$)

    that is the line I missed

    Mike


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

    Leave a comment:


  • Guest's Avatar
    Guest replied
    Mike! Great, thanks! But I can't compile it, and I assume it is
    missing the function statement... can you possibly fill in the
    missing data? Also, how would I call it?

    Again, thanks!

    Originally posted by Mike Burns:
    You can call this routine to encrypt data .. then call the same
    routine to decrypt it. Change the key however you like.

    Code:
      Dim EKey?(1:8)
      EKey?(1) = Asc("E")
      EKey?(2) = Asc("N")
      EKey?(3) = Asc("C")
      EKey?(4) = Asc("R")
      EKey?(5) = Asc("Y")
      EKey?(6) = Asc("P")
      EKey?(7) = Asc("T")
      EKey?(8) = Asc("X")
    
      eptr% = 0
      x = Len(Stuffin$)
      z$ = stuffin$  'output string
      inseg??? = STRSEG(stuffin$)
      inptr??? = StrPtr(stuffin$)
      outseg??? = STRSEG(z$)
      outptr??? = StrPtr(z$)
      For I = 1 To x
        Incr eptr%
        If eptr% > 8 Then
          eptr% = 1
        End If
        DEF SEG = inseg???
        tin? = Peek(inptr???)
        DEF SEG
        cout? = tin? Xor eKey?(eptr%)
        DEF SEG = outseg???
        Poke outptr???,cout?
        DEF SEG
        Incr inptr???
        Incr outptr???
      Next
      EncryptData$ = z$
     End Function
    No doubt someone can help make it faster ...

    Mike



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

    Leave a comment:


  • Mike Burns
    replied
    You can call this routine to encrypt data .. then call the same
    routine to decrypt it. Change the key however you like.

    Code:
      Dim EKey?(1:8)
      EKey?(1) = Asc("E")
      EKey?(2) = Asc("N")
      EKey?(3) = Asc("C")
      EKey?(4) = Asc("R")
      EKey?(5) = Asc("Y")
      EKey?(6) = Asc("P")
      EKey?(7) = Asc("T")
      EKey?(8) = Asc("X")
    
      eptr% = 0
      x = Len(Stuffin$)
      z$ = stuffin$  'output string
      inseg??? = STRSEG(stuffin$)
      inptr??? = StrPtr(stuffin$)
      outseg??? = STRSEG(z$)
      outptr??? = StrPtr(z$)
      For I = 1 To x
        Incr eptr%
        If eptr% > 8 Then
          eptr% = 1
        End If
        DEF SEG = inseg???
        tin? = Peek(inptr???)
        DEF SEG
        cout? = tin? Xor eKey?(eptr%)
        DEF SEG = outseg???
        Poke outptr???,cout?
        DEF SEG
        Incr inptr???
        Incr outptr???
      Next
      EncryptData$ = z$
     End Function
    No doubt someone can help make it faster ...

    Mike

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

    Leave a comment:


  • Guest's Avatar
    Guest started a topic Super simple encryption needed

    Super simple encryption needed

    Hi!

    I'm in need of a very easy to implement encryption routine which
    does not have to be advanced at all. I'm sure that there are
    advanced routines out there which can do a much nicer job but I
    really only need about this; Put encrypted in a string and call a
    subroutine which converts/interprets it and place the decrypted
    data in the same or in another string which I can print. The
    reason is that I want to prevent that a user edits my file with
    a hex editor or similar and replace characters.

    Anybody who has such a simple thing around?

    Many thanks in advance!

    ------------------
Working...
X