Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 5th, 2004, 04:06 PM
JunkCookie JunkCookie is offline
Vote Libertarian
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: N'wallins
Posts: 277 JunkCookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 27 sec
Reputation Power: 6
can you reverse this cipher?

This cipher is really simple, but I tried for 5 minutes to find a strategy to reverse it and can't (yet). I'm sure this has been thought of before, but here goes:

Code:
plaintext: devshed
ciphertext: iaoamih

Algorithm: 
convert the letters to their ordinal values (a = 1, b = 2, etc.)
devshed = 4 5 22 19 8 5 4

each position then gets the sum of its value and the next:
 = 9 27 41 27 13 9 8
(the last position gets the sum of its value and the very first position's initial value)

mod 26 (not exactly, but essentially):
 = 9 1 15 1 13 9 8

convert back to alpha based on ordinal values
 = iaoamih


given ciphertext: idhfnsln
what is the plaintext?

this is not case sensitive.. assume all lowercase
__________________
  • "Write programs as if the most important communication they do is not to the computer that executes them but to the human beings who will read and maintain the source code in the future" - ESR, The Art of UNIX Programming
  • "Programs must be written for people to read, and only incidentally for machines to execute." - Abelson & Sussman, SICP, preface to the first edition
  • "Programs must be written for machines to execute or else you just have a boring book" - DaWei_M
  • Vote Libertarian

Reply With Quote
  #2  
Old October 5th, 2004, 04:11 PM
JunkCookie JunkCookie is offline
Vote Libertarian
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: N'wallins
Posts: 277 JunkCookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 27 sec
Reputation Power: 6
here is some PHP code to create the ciphertext from plaintext:

PHP Code:
<?
define
('LOWER_ASCII_OFFSET'96);
define('UPPER_ASCII_OFFSET'64);

function 
str_split($str$len=1)
{
  if (
$len 1)
   return 
false;

  
$str_ary = array();
  
$input_len strlen($str);

  
// don't use substr unless you have to
  
if($len == 1)
   for(
$cursor 0$cursor $input_len$cursor++)
     
$str_ary[] = $str{$cursor};
  else
   for (
$cursor 0$cursor $input_len$cursor += $len)
     
$str_ary[] = substr($str$offset$len);

  return 
$str_ary;
}

function 
string_to_ord_ary($string$case_sens true)
{
  if(!
$case_sens)
    
$string strtoupper($string);

  
$ret_ary str_split($string);
  foreach(
$ret_ary as $index => $char)
    
$ret_ary[$index] = ord($char) - UPPER_ASCII_OFFSET;
  return 
$ret_ary;
}

function 
scramble(&$ord_ary)
{
  
$first $ord_ary[0];
  
$lastindex count($ord_ary) - 1;
  
$last $ord_ary[$lastindex];

  foreach(
$ord_ary as $index => $value)
  {
    
$value += (isset($ord_ary[$index 1]) ? $ord_ary[$index+1] : 0);
    
$ord_ary[$index] = (($value 1) % 26) + 1;
  }

  
$last += $first;
  
$ord_ary[$lastindex] = (($last 1) % 26) + 1;
}

function 
ord_ary_to_string($ord_ary)
{
  foreach(
$ord_ary as $index => $value)
    
$ord_ary[$index] = chr($value UPPER_ASCII_OFFSET);
  return(
implode(''$ord_ary));
}

print 
'<pre>';

$plaintext = (isset($_REQUEST['text']) ? $_REQUEST['text'] : false);

$p_ary string_to_ord_ary($plaintextfalse);
print 
"$plaintext:\n";
print_r($p_ary);

print 
"Scrambled:\n";
scramble($p_ary);
print_r($p_ary);

$ciphertext ord_ary_to_string($p_ary);
print 
"$ciphertext\n";


print 
'</pre>';
?>


just put the above code into a file (cipher.html ?) on a PHP webserver, then request this (fake) URL:

http://mywebserver.com/cipher.html?text=mytext

I have it running right here.

Last edited by JunkCookie : October 5th, 2004 at 04:19 PM.

Reply With Quote
  #3  
Old October 5th, 2004, 07:16 PM
JunkCookie JunkCookie is offline
Vote Libertarian
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: N'wallins
Posts: 277 JunkCookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 27 sec
Reputation Power: 6
have been playing around with this, and, at least for the input 'oaxaca', connecting the output to the input and interating a few times eventually puts you into a loop, where the ouput is eventually an output you have seen before.

I'm pretty new to crypto, but if anyone wants to enlighten me, I'm all ears

//EDIT: these two inputs give the same output: pyyddp & cllqqc
each letter of the two strings have the same offset, except one string is relative to A and the other to N, take your pick

Last edited by JunkCookie : October 5th, 2004 at 07:23 PM.

Reply With Quote
  #4  
Old October 11th, 2004, 12:15 AM
par par is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 1 par User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
if you take each position as variables with letters a b c d e f g etc

4 5 22 19 8 5 4

maps into
a+b b+c c+d d+e e+f f+g g+a (all mod 26)


9 1 15 1 13 9 8

now just undo by adding and subtracting alternately


9-1+15-1+13-9+8 =34 mod 26= = 2a 8/2 = 'd'

(a+b)-(b+c)+(c+d)-(d+e)+(e+f)-(f+g)+(g+a) ->
a +b-b -c+c +d-d -e+e +f-f -g+g +a = 2a

1-15+1-13+9-8+9 = 10 mod 26 = 2b 10/2 = 'e'
15-1+13-9+8-9+1 = -8 mod 26 = 2c -8/2 = -4 mod 26 = 22 = 'v'

etc.
you have 7 linear equations you can think of it that way too

Reply With Quote
  #5  
Old October 13th, 2004, 06:36 PM
JunkCookie JunkCookie is offline
Vote Libertarian
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: N'wallins
Posts: 277 JunkCookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 27 sec
Reputation Power: 6
Quote:
Originally Posted by JunkCookie
given ciphertext: idhfnsln
what is the plaintext?


idhfnsln = 9 4 8 6 14 19 12 14
Code:
$a := 9 - 4 + 8 - 6 + 14 - 19 + 12 - 14 == 0;
// since 0 represents 'z' and not 'a' by my definition, we don't do mod 26 exactly..
// rather do: $letternum = (($value - 1) % 26) + 1
$a := ($a - 1) % 26 + 1 == 26;
$a := $a/2 == 13;
char($a) == 'm';


hmm.. the first letter of the plaintext is not m. I must be missing something..

Reply With Quote
  #6  
Old October 17th, 2004, 11:00 AM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
It's not reversible, taking the modulus means that different plaintexts may produce the same cyphertext.

Consider the cyphertext "zz" - it is the result of encoding the plaintext "mm" and also the result of encoding "zz".

Not hard to reverse otherwise, I'd guess, haven't looked at it, but if you know the first letter then the rest will follow. Hence only twenty six alternatives to consider.

As for adding and subtracting alternately, it will give twice the first character (as above) only for odd-numerbered length strings, even numbered length strings will always give zero (which is not in the range 1 to 26 and should have been an obvious indication of a flaw in your method...)

Last edited by epl : October 17th, 2004 at 11:05 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > can you reverse this cipher?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT