Skip to content

Instantly share code, notes, and snippets.

@cadanalyst
Last active March 12, 2018 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cadanalyst/9d0ec275e58bc28521dffc89bf7d8407 to your computer and use it in GitHub Desktop.
Save cadanalyst/9d0ec275e58bc28521dffc89bf7d8407 to your computer and use it in GitHub Desktop.
obfus.lsp
( (lambda ( / safe-ascii random-ascii make-mask offset-mask mask )
;;======================================================================
;; \|// obfus.lsp
;; |Oo| (c) 2018 Michael Puckett
;; |- | mp@cadanalyst.org
;;----------------------------------------------------------------------
;; 1.01 2018-03-10. MP. Initial code.
;; 1.02 2018-03-11. MP. Document ad nauseum.
;;----------------------------------------------------------------------
;; Define temporary local data and functions which will be used to
;; define an obfuscation function that will reside in the global space.
;; Think of the technique as kind of a function factory -- once the
;; desired function has been defined the temporary data and functions
;; used to define it are destroyed.
;;
;; The obfuscation function defined here isn't intended to be robust
;; or of industrial strength. Neither is it designed to provide the
;; ability to pass in a mask or password so that unlimited variations
;; of encoded data may be realized.
;;
;; Just a simple obfuscater function that will return the original
;; value when invoked twice:
;;
;; (obfus "2018-03-10 | Log entry.") >> "éYv4üYgüvY | Ó£3 scêàæã"
;; (obfus "éYv4üYgüvY | Ó£3 scêàæã") >> "2018-03-10 | Log entry."
;;
;; With high byes excluded from safe-ascii:
;;
;; (obfus "2018-03-10 | Log entry.") >> "eEX4vEovXE | Z3Y 2c$sxj"
;; (obfus "eEX4vEovXE | Z3Y 2c$sxj") >> "2018-03-10 | Log entry."
;;
;; You may note some characters are not encoded, for example white
;; spaces " ", "\t" as well as the xref delimiter "|". This is by
;; design. Any charcater that is inappropriate for encoding, e.g. new
;; line chars "\n", or characters that fail snvalid tests, e.g. "|"
;; simply pass thru the function as is. These characters also will
;; never be generated by the encoding scheme. By eliminating the
;; processing of spaces potential errors due to white space trimming
;; of encoded values is eliminated.
(setq safe-ascii
;; This list was generated via examing candidate (0-255) values
;; with snvalid (with extnames set to 0) as well as oberving if
;; characters rendered properly on my screen. It would be prudent
;; to review the string it produces (vl-list->string safe-ascii)
;; on your machine(s) before using in a production environment to
;; ensure data produced will be appropriate for your needs. I left
;; place holders in the data so you could easily see what codes
;; were considered unsafe / unprintable etc.
;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;; Do not change this data once you have encoded information with
;; it lest you break the ability to decode it!
(vl-remove nil
(list
nil nil nil 35 36 37 38 39 40 41 nil 43 nil 45 46
nil 48 49 50 51 52 53 54 55 56 57 nil nil nil nil
nil nil nil 65 66 67 68 69 70 71 72 73 74 75 76
77 78 79 80 81 82 83 84 85 86 87 88 89 90 nil
nil nil 94 95 nil 97 98 99 100 101 102 103 104 105 106
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
122 nil nil nil 126 nil
;; high bytes separated in case you want to exclude them
128 nil 130 131 132 133 134 135 136 137 138 139 140 nil nil
nil nil 145 146 147 148 149 150 151 152 153 154 155 156 nil
nil 159 nil 161 162 163 164 165 166 167 168 169 170 171 172
nil 174 nil 176 177 178 179 180 181 182 183 184 185 186 187
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
248 249 250 251 252 253 254 255
)
)
;; This is what the safe character set looks like:
;;
;; # $ % & ' ( ) + - .
;; 0 1 2 3 4 5 6 7 8 9
;; A B C D E F G H I J K L
;; M N O P Q R S T U V W X Y Z
;; ^ _ a b c d e f g h i j
;; k l m n o p q r s t u v w x y
;; z ~
;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;; € ‚ ƒ „ … † ‡ ˆ ‰ Š ‹ Œ
;; ‘ ’ “ ” • – — ˜ ™ š › œ
;; Ÿ ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬
;; ® ° ± ² ³ ´ µ ¶ · ¸ ¹ º »
;; ¼ ½ ¾ ¿ À Á Â Ã Ä Å Æ Ç È É Ê
;; Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù
;; Ú Û Ü Ý Þ ß à á â ã ä å æ ç è
;; é ê ë ì í î ï ð ñ ò ó ô õ ö ÷
;; ø ù ú û ü ý þ ÿ
)
(setq random-ascii
;; This 33-255 random sequence sample was generated using the web
;; site http://www.random.org/sequences. You can generate this data
;; any way you wish (I have my own sequence generator I use to
;; generate a static list of random sequenced integers in a range
;; but won't be publishing it). The list of ints must include all
;; ints from 33-255 (you can include values less than 33 they'll
;; just be ignored) randomly arranged & unique. You may notice many
;; entries below (e.g. 141) do not reside in safe-ascii's data. No
;; matter -- that's what safe-ascii is for -- to remove candidate
;; values that are nfg.
;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;; Do not change this data once you have encoded information with
;; it lest you break the ability to decode it!
(list
88 242 182 99 56 175 128 161 215 46 136 142 205 250 59 184
208 72 172 116 81 248 109 226 107 200 100 90 219 75 211 60
124 148 210 50 193 61 115 102 224 173 58 220 93 37 164 67
179 34 137 239 232 113 214 170 96 201 206 253 176 147 121 228
127 243 255 87 53 189 207 203 119 41 143 140 146 156 151 247
240 144 133 62 39 213 79 65 174 38 229 112 132 40 98 218
55 97 111 84 103 45 192 48 138 149 135 49 178 145 159 64
171 254 199 110 52 63 223 106 158 134 227 166 168 94 150 36
68 216 234 231 82 202 195 244 209 222 44 221 80 108 76 154
251 42 233 105 141 101 197 114 95 191 43 123 196 180 71 162
160 238 35 120 153 188 187 129 246 167 85 92 230 235 212 104
165 217 91 177 47 126 241 54 169 33 157 130 83 155 186 249
245 86 236 66 237 198 74 183 204 194 78 122 57 225 77 185
163 117 32 51 252 73 89 190 139 131 125 118 181 152 69 70
)
)
(defun make-mask ( / codes )
(vl-list->string
(if
(zerop
(rem
(length
(setq codes
(vl-remove-if-not
(function
(lambda (x)
(member x safe-ascii)
)
)
random-ascii
)
)
)
2
)
)
codes
(reverse (cdr (reverse codes)))
)
)
)
(defun offset-mask ( mask / n )
(substr
(strcat mask mask)
(1+ (/ (setq n (strlen mask)) 2))
n
)
)
;; Ok, factory data and functions established, let's create our
;; public function:
;;
;; (defun obfus (text) ...)
(eval
(list 'defun 'obfus '(text)
(list
'vl-string-translate
(setq mask (make-mask))
(offset-mask mask)
'text
)
)
)
;;----------------------------------------------------------------------
;;
;; The following data and functions were defined as locals to the host
;; lambda construct & thus will evaporate (they will not appear in the
;; atoms-family) once this code is executed:
;;
;; safe-ascii-codes
;; random-ascii-codes
;; make-mask
;; offset-mask
;;
;; Try it:
;;
;; (load "obfus")
;;
;; !safe-ascii >> nil
;; !random-ascii-codes >> nil
;; !make-mask >> nil
;; !offset-mask >> nil
;;
;; Yet the function we desired to publish is available:
;;
;; !obfus >> #<SUBR @46a8e5c0 OBFUS>
;;
;; Cool.
;;
;;----------------------------------------------------------------------
;;
;; Testing the code in AutoCAD and BricsCAD produces the same output
;; as was expected (but prudent to check).
;;
;; Finally, you don't have to name the function obfus. In fact, you
;; might elect to call it something else entirely, something that
;; does not suggest its intent.
;;
;; Cheers, Michael Puckett.
;;
;;----------------------------------------------------------------------
;; end obfus.lsp
;;----------------------------------------------------------------------
(princ)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment