Sponsored Content
Top Forums UNIX for Dummies Questions & Answers UNIX fun stuff - echo and dc - obfuscate/garble a string sort of Post 302938893 by Don Cragun on Thursday 19th of March 2015 07:45:16 PM
Old 03-19-2015
If you save the following script in a file named obfuscate:
Code:
#!/bin/ksh
# NAME:		obfuscate -- obfuscate text read from one or more files into a
#			     string that can be decrypted by the dc utility
#
# SYNOPSIS:	obfuscate file...
#
# OPERANDS:	file	The name of a text file containing text to be
#			obfuscated or "-" to obfuscate text read from
#			standard input.
#
# APPLICATION USAGE:
#	To decrypt the obfuscated string produced by obfuscate, use:
#		obfuscate file | read string	# Get obfuscated text
# 	Note: Do not use "read -r string" in the above command!
#		printf '%s\n' "$string"		# Show obfuscated text
#		echo "$string" | dc		# Decrypt obfuscated text
#
#	Although dc can produce artibrary length output, feeding the objuscated
#	string back into dc for decryption may be limited by {LINE_MAX} and/or
#	{ARG_MAX} restrictions.

# Initialize a to ASCII character set (skipping NUL)...
a='\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31\32\33'
a="$a"'\34\35\36\37 !"#$%&'"'"'()*+,-./0123456789:;<=>?@'
a="$a"'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\377'

awk -v A="$a" '
function cline(inline,		i) {
	printf("256*%d+\n", index(A, "\n"))
	for(i = length(inline); i; i--) {
		printf("256*%d+\n", index(A, substr(inline, i, 1)))
	}
}
BEGIN {	print 0
}
{	line[NR] = $0
}
END {	for(i = NR; i; i--)
		cline(line[i])
	printf("[[q]sa[ln0=aln256%%Pln256/snlbx]sb]Pn[snlbxq\n]Pq\n")
}' "$@" | tee .dc_input | dc

make it executable with:
Code:
chmod +x obfuscate

and execute the command:
Code:
printf "Hello World.\nAre we there yet?\nLet's go home, now!\n" | ./obfuscate - | read string

then the command:
Code:
echo "$string"

will give you:
Code:
[q]sa[ln0=aln256%Pln256/snlbx]sb26160072918627741401952510855241017735603346265259888938898171600856988789569756293233903076568696999873394858335331444040snlbxq

and the command:
Code:
echo "$string"|dc

will give you:
Code:
Hello World.
Are we there yet?
Let's go home, now!

Is this what you're looking for? If won't work with characters that aren't in the 7-bit ASCII character set and it won't work if the text you want to obfuscate contains any NUL bytes, and on many systems it won't work if line in files you want to obfuscate are longer than LINE_MAX bytes and if the output produced by obfuscate produces a string longer than LINE_MAX, dc may not be able to decrypt it for you on some systems.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.

The standards didn't require awk to treat a file name operand of - as a request to read standard input (although most implementations of awk already did this) until the 2013 editions of the POSIX Standards and the Single UNIX Specifications. If the implementation of awk on your system gives an diagnostic message similar to:
Code:
awk: can't open file -

you'll need a two step process (or three step, if you remove the temp file when you're done) similar to:
Code:
printf "Hello World.\nAre we there yet?\nLet's go home, now!\n" > ob$$
./obfuscate ob$$ | read string
rm -f ob$$

to make it work.

If you want to see the dc commands awk created in obfuscate for the input you fed it, the command:
Code:
cat .dc_input

after running obfuscate will show you the input dc processed for the last invocation of obfuscate. For the above commands, that would be:
Code:
0
256*10+
256*33+
256*119+
256*111+
256*110+
256*32+
256*44+
256*101+
256*109+
256*111+
256*104+
256*32+
256*111+
256*103+
256*32+
256*115+
256*39+
256*116+
256*101+
256*76+
256*10+
256*63+
256*116+
256*101+
256*121+
256*32+
256*101+
256*114+
256*101+
256*104+
256*116+
256*32+
256*101+
256*119+
256*32+
256*101+
256*114+
256*65+
256*10+
256*46+
256*100+
256*108+
256*114+
256*111+
256*87+
256*32+
256*111+
256*108+
256*108+
256*101+
256*72+
[[q]sa[ln0=aln256%Pln256/snlbx]sb]Pn[snlbxq
]Pq

Are we having fun yet? Smilie
These 4 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo display alignment/sort order

Hi, My script prints a few varibales as each it reads each line of a text file and then prints them on screen, however iam having problem in aligning and sorting them. what happens is this Last First Number Mark leo 87798798... (1 Reply)
Discussion started by: shackman66
1 Replies

2. What is on Your Mind?

FUN POLL: What is the best UNIX/Linux for. . .?

With so many different flavors of UNIX and Linux available in the world, choosing the best one for yourself, your family, or for your organization can be such an overwhelming and rather difficult decision to make! :eek: If you were a salesman and you had to decide the best UNIX/Linux distro for... (1 Reply)
Discussion started by: MrrrrrNiceGuy
1 Replies

3. What is on Your Mind?

Time to have FUN my Unix/Linux friends...(One liners)...MUST read.. !!

As a mind refresher, I was thinking to start a new thread for ONE LINERS....funny/weird or any technical one liners.... Let me start first...... ================================= #!/bin/ssh #The Unix Guru's View of Sex unzip ; strip ; touch ; grep ; finger ; mount ; fsck ; more ; yes ;... (3 Replies)
Discussion started by: Rahulpict
3 Replies

4. Shell Programming and Scripting

intro to UNIX - making a sort-of recycle bin (for fun)

Hello, I'm only taking Intro to UNIX in school right now, so please bear with me. My problem is with a sort-of recycle-bin rig I've created for fun. I'm using Ubuntu 9.04, I am the admin. (only user, actually) of this computer. I'm using this script in ~/.bashrc # if files exist, remove contents... (6 Replies)
Discussion started by: jzacsh
6 Replies

5. Programming

Obfuscate'ing a.out ... ???

Hi all, I've search the forums regarding posts similar to this already but can't find the suitable response. Am actually looking for something very trivial I think. I just want to mask/obfuscate the a.out file and run it like a normal UNIX program. I've look at gpg and encryption but it requires... (4 Replies)
Discussion started by: newbie_01
4 Replies

6. Shell Programming and Scripting

CSH: echo a string having []

I am trying to use echo in CSH and getting an error. I want to use to tell the user which parameters are optional. set msg2 = " -Inmod= -Nxz= -Varp=" echo $msg2 (3 Replies)
Discussion started by: kristinu
3 Replies

7. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

8. Programming

How to hide from UNIX strings - obfuscate or hide a literal or constant?

Hi, I need to somehow pipe the password to a command and run some SQL, for example, something like echo $password | sqlplus -s system @query01.sql To make it not so obvious, I decided to try out writing a small C program that basically just do echo $password. So now I just do x9.out | sqlplus... (8 Replies)
Discussion started by: newbie_01
8 Replies

9. UNIX for Dummies Questions & Answers

String size limit for 'echo'...

Hi guys, man bash doesn't help much here nor does the WWW. (I have discovered there is technically no limit to a bash array.) I am thinking of adding a new full manual inside AudioScope.sh. A few questions here. The main question is, assuming I go ahead with this idea:- Is there a limit... (6 Replies)
Discussion started by: wisecracker
6 Replies

10. Shell Programming and Scripting

UNIX: passing stuff to a shell function

I have users that print files to selected printers. Instead of creating one function for each printer I would like to have just one and passing the files to print as well as the wanted printer. The following code does not work, of course. I'm expecting that $1 is the list of files to be printed... (6 Replies)
Discussion started by: emare
6 Replies
All times are GMT -4. The time now is 10:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy