REVISED 6-16-01

Improved string cipher. Useful for HTML textarea encryption in tandem with a javascript implementation. Also implemented in Visual Basic. Using string variable types it is trivial to encrypt and decrypt strings with CipherText.

Limiting key domain to ASCII 32 - 63 results in 7-bit ASCII compliant ciphertext. Otherwise a domain 32-95 produces ciphertext with a domain from 32 to 159 with values 128 - 159 shifted so as to be compatible with all standard ISO-8859-1 implementations in Windows and other systems.

New RSORT lookup table replaces key elements with randomly sorted domain values. A key attribute is used to mask the selected values and to articulate shift in the modified keystring. The applied shift is what ensures a greater difference in applied cipher bits when using very similar keys.

The cipher period is diffused to N*(N-1) message elements (where N is key length.) This significantly reduces data available for frequency analysis so that a 2K textarea window can be protected with a 15 character key if it is assumed that more than 5 key periods are required for analysis. Its possible that significant FA requires 20 values. Control values are passed unciphered leaving just the periods for analysis as known plaintext values. Each ciphertext period value is the result of two values. If a key is chosen so that N*(N-1) exceeds message length, then the benefit of knowing the composite of applied bits to result in a ciphertext value derived from a plaintext period is reduced to nothing because the same two key values will not be applied to another message element.

# CipherTextI.pm # # Charles Prichard 6-16-01 # # ################################################# package CipherTextI; use strict; use integer; use bytes; ################################################# # PUBLIC Subroutine # ################################################# sub new($) { my $class = shift; my $self = {}; bless $self, ref $class || $class; $self->{xString} = undef; $self->{xStr} = undef; $self->{R_KEY} = undef; $self->{ATT} = undef; $self->{S_KEY} = undef; $self->{cipher_key} = undef; $self->{RSORT} = []; $self->init(@_); return $self; } sub init($){ my $self = shift; my ($params) = shift; my @temp1 = split /\;/, $params; my %params = (); my @temp2=(); foreach my $x (@temp1){ @temp2 = split /\=/, $x; $self->{$temp2[0]} = $temp2[1]; } $self->{RSORT} = [3,28,7,5,39,62,34,32,56,18,17,6,29,49,63,45,13,1 +3,29,40,2,17,17,9,12,61,56,23,55,37,31,13,27,52,8,23,38,53,9,60,31,30 +,39,27,37,14,14,34,8,29,58,10,25,3,19,37,11,35,52,39,48,24,22,19]; $self->{S_KEY} = $self->Make_shiftedKeys(); return; } ################################################# # PUBLIC Subroutine # ################################################# sub encipher($) { my $self = shift; my $MSG = shift;; $MSG = $self->Encode($self->Encode($MSG,$self->{R_KEY}),$self->{S_ +KEY}); return $MSG; } ################################################# # PUBLIC Subroutine # ################################################# sub decipher($) { my $self = shift; my $MSG = shift;; $MSG = $self->Decode($self->Decode($MSG,$self->{S_KEY}),$self->{R_ +KEY}); return $MSG; } ################################################# # PRIVATE Subroutine # ################################################# sub Make_shiftedKeys(){ my $self = shift; my $shift = $self->setAttribute(); $self->{S_KEY} = $self->modifyKeys(); if (($shift % 2) == 1){$self->{S_KEY} = (substr($self->{S_KEY},1,( +length $self->{S_KEY})));} else {$self->{S_KEY} = (substr($self->{S_KEY},0,(length $self->{S_ +KEY}) - 1));} my $key; my $v; for (my $i=0; $i <= ($shift - 1) % length $self->{S_KEY}; $i++){ + my $key = substr($self->{S_KEY},0,1); $v = ord($key) - 32; $self->{S_KEY} = (substr($self->{S_KEY},1,(length $self->{ +S_KEY}) - 1)); $self->{S_KEY} .= ord($v ^ $self->{RSORT}[$v] + 32); } return $self->{S_KEY}; } ################################################# # PRIVATE Subroutine # ################################################# sub setAttribute(){ my $self = shift; $self->{ATT} = 0x00; for (my $i=0; $i < (length $self->{R_KEY}); $i++){ $self->{ATT} = $self->{ATT} ^ (ord(substr($self->{R_KEY},$i,1) +) - 0x20); } return $self->{ATT}; } ################################################# # PRIVATE Subroutine # ################################################# sub modifyKeys(){ my $self = shift; my S_KEY = ""; my $i, $x; For $x = 1 To length($self->{R_KEY}) $i = ord(substr($self->{R_KEY}, $x, 1)) - 32 S_KEY = S_KEY + ord(($i ^ $self->{RSORT}[$i] ^ $self->{ATT}) + + 32) Next x return S_KEY; } ################################################# # PRIVATE Subroutine # ################################################# sub Encode($$){ my $self = shift; $self->{xString} = shift; $self->{cipher_key} = shift; $self->cipher(); return $self->{xStr}; } ################################################# # PRIVATE Subroutine # ################################################# sub Decode($$){ my $self = shift; $self->{xString} = shift; $self->{cipher_key} = shift; $self->cipher(); return $self->{xStr}; } ################################################# # PRIVATE Subroutine # ################################################# sub cipher(){ my $self = shift; $self->{xStr} = ""; my $keyval; my $xch; my @akeys = split "",$self->{cipher_key}; my $keylen = length $self->{cipher_key}; my $i = (length $self->{xString} % $keylen); # DATA DEPENDENT +SHIFT for(my $x=0; $x < length ($self->{xString}); $x++){ $keyval = ord($akeys[$i]) - 0x20; $xch = ord(substr($self->{xString},$x,1)); if ($xch > 0x7f){ $xch = $xch - 0x21;} if ($xch > 0x1f){ $xch = ($keyval ^ ($xch - 0x20)) + 0x20; # Apply ciph +er } if ($xch > 0x7f){ $xch = $xch + 0x21;} $self->{xStr} .= chr($xch); $i++; if ($i == $keylen){$i = 0;} } return; } 1;

In reply to Re: CipherText by Steeeeeve
in thread CipherText by NodeReaper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.