Save the following perl script with whatever name you want, and chmod it u+a.
#!/usr/bin/perl # Quantum Crypto Key Exchange in Perl, Phil Carmody 2003 @zo=('|','-','/','\\');%sf=('|'=>0,'-'=>1,'/'=>2,'\\'=>3);srand();($lt=shift (@ARGV))&&($lt=~m/^[rw]$/)or die("First parameter is r or w for read/write") ;%po=();($oa=shift(@ARGV))&&(($lt eq'r')?$oa=~m/^[+x]+$/:$oa=~m#^[-\\/|]+$#) or die("Second parameter, read with [+x], write with [-|\/]");@cs=@yb=();$rl =$fq=0;open(M,"<$0")or die("Can't read self");while($dt=){while($dt=~ m/[\$\%\@]([a-z])([a-z]*)/){if(!defined($po{$1})){$po{$1}=$2;$rl|=1<<(ord($1 )-97);}elsif($po{$1}ne$2){print("$. $1$2 clashes with $1$po{$1}\n");}$dt=~ s/([\$\%\@])(\Q$1\E)(\Q$2\E)/${1}_$2$3/g;}push @yb,$dt;}close(M);$ul='';for $ms(sort(keys(%po))){$ul.=$ms;@vl=split(m/ */,$po{$ms});for$te(@vl){$bt=ord( $te)-ord('a');$cs[$fq++]=$bt&3;if($bt<16){$cs[$fq++]=($bt>>2)&3;}}}if(!-w$0) {die("This script must be writable, and will be overwritten");}open(M,">$0") or die("Can't write self");if($lt eq'r'){$xe=0;for$as(split(/ */,$oa)){if( $xe>=$fq){last;}$as=$as eq'x';if($as!=($cs[$xe]>>1)){$cs[$xe]=$as*2+(rand()> .5);}print("$zo[$cs[$xe]]");++$xe;}print("\n");}else{$fq=0;foreach $kt(split (/ */,$oa)){$cs[$fq++]=$sf{$kt};}}$js=0;for $xe(0..25){$js+=($rl>>$xe)&1;} for(0..10){$iq=1+int(rand()*23);$ne=$rl>>$iq;$rl&=(1<<$iq)-1;while($iq++<26) {$rl=($rl<<1)|($ne&1);$ne>>=1;}$iq=1+int(rand()*($js-2));$ul=substr($ul,$iq) .reverse(substr($ul,0,$iq));}%gr=();for $xe(0..26){if(($rl>>$xe)&1){$te=chr( $xe+97);$gr{$te}='';$hr=int($fq/$js);while($hr>1){$gr{chr($xe+97)}.=chr(97+ $cs[0]+$cs[1]*4);shift(@cs);shift(@cs);$fq-=2;$hr-=2;}if($hr>0){$gr{chr($xe+ 97)}.=chr(97+$cs[0]+16);shift(@cs);$fq--;$hr--;}--$js;$ne=substr($ul,$js,1); map s/([\$\%\@])_$ne$po{$ne}/$1$te$gr{$te}/g,@yb;}}print M (@yb);close(M);
It's fairly rudimentary at the moment; it permits one person to inject a stream of random {|,-,/,\}-bits "into" the script, which can then be given to the recipient, who can choose which basis, x or +, to use for each read. Then simply perform the rest of the key exchange as normal.
Of course, it ain't quantum crypto as you can copy the script; shall we agree that it's quantum crapto? However, if you forbid actions that are impossible with quantum states it's quite an accurate mimic. It achieves this, as you can see in the code, by self-modification on every operation; i.e. reading changes its state.
Alice $ ./otp.pl w '|\/-|-/\||\|/-|\--\-/\\\|-\//\/|/-' Bob $ ./otp.pl r xxxx++++xxxx++++xxxx++++xxxx++++xx \\/\|---\/\/--||\\\\|||-\/\/--||//
Alice $ ./otp.pl w '|\/-|-/\||\|/-|\--\-/\\\|-\//\/|/-' Eve $ ./otp.pl r xxxx++++xxxx++++xxxx++++xxxx++++xx /\/\|--|//\/|-||\\\/|--|//\/--||/\ Bob $ ./otp.pl r +xx+++xx++x+x++x++x+xxxx++xxxxx+x+ |\/-|-\\-|\-/-|/|-\-\\/\-|\///\|/-
Another hastily constructed page by Phil Carmody
Home /
Crypto /
QKE.html