XOR_encryption

  behavior for XOR encryption
XOR encryption




  Text XOR encryption
James Newton wrote on encryption:


Hi,

The following handler encrypts a text of 10000 characters in under a
second on my pentium II at 400 MHz.  It should work safely with all
standard ascii alphanumeric characters.  I haven't tested fully with
charToNum() < 49 or charToNum() > 122.

If you find this useful, perhaps you could post it to the other lists
that you cross-posted initially.

Cheers,

James


=====================================================================


on encrypt(plainString, encryptString) ------------------------------
  -- Returns a Xor-encrypted string, where each character of
  -- plainString is encoded according to the corresponding character
  -- of encryptString.  If encryptString is not long enough, the same
  -- series of characters is used repeatedly.
  --
  -- To unencode, simply re-encrypt the encrypted result using the
  -- original encrypt key.
  --
  -- Example:
  --   x = encrypt("Handler written by James Newton", "using D8")
  --   put x
  --   -- "=
  --   E6iE*
  --   I$M!KU=   --   O*"
  --   put encrypt(x, "using D8")
  --   -- "Handler written by James Newton"
  ---------------------------------------------------------------------

  if not stringP(plainString) then
    return #invalidString
  else if not stringP(encryptString) then
    return plainString
  else if encryptString = "" then
    return plainString
  end if

  plainLength   = the number of chars of plainString
  encryptLength = the number of chars of encryptString

  -- Ensure that encryptString is at least as long as plainString
  if encryptLength < plainLength then
    temp = encryptString
    shortFall = (plainLength / encryptLength)
    repeat while shortFall
      put temp after encryptString
      shortFall = shortFall - 1
    end repeat
  end if

  -- Encrypt each character of plainString using the corresponding
  -- character of encryptString.  If the plain character is the same
  -- as the encrypt character, no encryption occurs (the result of the
  -- encryption would be numToChar(0), which is coerced to any empty
  -- string).

  encryption = ""
  repeat while plainLength
    plainChar  = plainString.char[1]
    plainNum   = charToNum(plainChar)
    encryptNum = charToNum(encryptString.char[1])

    if plainNum <> encryptNum then
      put numToChar(bitXor(plainNum, encryptNum)) after encryption
    else
      -- Use the non encrypted character rather than numToChar(0)
      put plainChar after encryption
    end if

    delete plainString.char[1]
    delete encryptString.char[1]

    plainLength = plainLength - 1
  end repeat

  return encryption
end encrypt



Home shock + cgi Bits 'n pieces Director Lingo ShockLets Contact