XOR two strings


 
Thread Tools Search this Thread
# 8  
Password1 xor with _(ASCII 95)

---------- Post updated at 08:17 AM ---------- Previous update was at 05:03 AM ----------

Password1 xor _ then base64 encode
result should be Dz4sLCgwLTtu

---------- Post updated 03-15-12 at 05:17 AM ---------- Previous update was 03-14-12 at 08:17 AM ----------

Password1 should be xored with _(underscore ASCII value 95) and then i need to base64 encoding to get Dz4sLCgwLTtu
# 9  
Hi.

With a perl helper:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate bit-string XOR, perl, with base64 encoding.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C perl base64

PASS=${1-"Password1"}

pl " Results, expecting \"Dz4sLCgwLTtu\" for \"Password1\":"

perl -e 'print '"$PASS"' ^ ( "_" x length('"$PASS"'));' |
base64

exit 0

roducing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
perl 5.10.0
base64 (GNU coreutils) 6.10

-----
 Results, expecting "Dz4sLCgwLTtu" for "Password1":
Dz4sLCgwLTtu

and
Code:
% ./s1 snazzy

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
perl 5.10.0
base64 (GNU coreutils) 6.10

-----
 Results, expecting "Dz4sLCgwLTtu" for "Password1":
LDE+JSUm

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 10  
Here is an example of one way of handling the string XOR-ing with ASCII 95 issue in a Bash, Korn or Zsh shell:
Code:
plaintext="abcdefg"

echo "Plaintext: $plaintext"

cyphertext=""
for ((i=0; i < ${#plaintext}; i++ ))
do
   ord=$(printf "%d" "'${plaintext:$i:1}")
   tmp=$(printf \\$(printf '%03o' $((ord ^ 95)) ))
   ciphertext="${ciphertext}${tmp}"
done

echo "Ciphertext: $ciphertext"

plaintext=""
for ((i=0; i < ${#ciphertext}; i++ ))
do
   ord=$(printf "%d" "'${ciphertext:$i:1}")
   tmp=$(printf \\$(printf '%03o' $((ord ^ 95)) ))
   plaintext="${plaintext}${tmp}"
done

echo "Plaintext: $plaintext"

This outputs:
Code:
Plaintext: abcdefg
Ciphertext: >=<;:98
Plaintext: abcdefg

This User Gave Thanks to fpmurphy For This Post:
# 11  
thanks fpmurphy and drl
i am not using perl so i need to do it using shell script
hi fpmurphy,
add following line
myb64=`echo "$ciphertext"|base64`
expected result was Dz4sLCgwLTtu
but i am getting Dz4sLCgwLTtuCg==
thank you for your help
# 12  
Hi.

I got a result similar to that, "Dz4sLCgwLTtuCg==", with perl until I omitted the newline.

Your comment about shell only makes me think that this might be homework. Is there some other reason for that condition? ... cheers, drl
# 13  
hi drl,
its not homework, its just that i m new to shell
anyway thank you, i have embedded it with shell script its working fine thank you
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. FAQ Submission Queue

Analysis in bitwise XOR

The purpose of this article is revealing the unrevealed parts of the bitwise XOR. As we aware, the truth table for the XOR operator is : A B A^B 0 0 0 0 1 1 1 0 1 1 1 0 For example , 1^2 will be calculated as given below: First the operands... (1 Reply)
Discussion started by: pandeesh
1 Replies

3. UNIX for Dummies Questions & Answers

XOR between strings

I am aware of truth table for XOR between binary values . Out of curious in would like to know how XOR works between 2 strings which contain alphabets . For example A ^ B How it works internally? Please help me to understand this Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

6. Programming

A trivial XOR doubt in a program

Hi, I am trying to reverse a string using the following program utilizing the Exclusive OR bit operation: int main() { char str = "Quraish"; char *p = str, temp; char *q = str + strlen(str) - 1; while ( p != q ) { if (*p != *q) { *p ^= *q; *q ^= *p; *p ^= *q;... (1 Reply)
Discussion started by: royalibrahim
1 Replies

7. Shell Programming and Scripting

xor 2 values in ksh?

i have to xor two variables in ksh. how to do that? tia, DN2 (5 Replies)
Discussion started by: DukeNuke2
5 Replies

8. Programming

resetting counter using bitwise XOR

Hi ! How to reset a variable to 0 after a reset value, say 10 using bitwise XOR. For example, int cnt=0; if(cnt<10) cnt++; else cnt = 0; How can we achieve this by using XOR only. thanks, (1 Reply)
Discussion started by: mrgubbala
1 Replies

9. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies

Featured Tech Videos