Skip to content

Instantly share code, notes, and snippets.

@ex
Created September 21, 2013 20:48
Show Gist options
  • Save ex/6654036 to your computer and use it in GitHub Desktop.
Save ex/6654036 to your computer and use it in GitHub Desktop.
# Decrypt
my $text = "Uid nx, aex jcdjipx iu wzux zp, ta wxtpa jtdaws, ai etkx vis.\n" .
"Dcos zyexdzaxr aex Jxdw jezwipijes iu etkzyg nidx aety iyx hts\n" .
"ai ri aex ptnx aezyg. Z zyexdzaxr aeta jezwipijes udin Wtdds Htww,\n" .
"hei zp ns exdi tqactwws. Z htya ai ntfx Dcos cpxdp udxx. Z htya ai\n" .
"gzkx aexn aex udxxrin ai qeiipx. Jxijwx tdx rzuuxdxya. Jxijwx qeiipx\n" .
"rzuuxdxya qdzaxdzt. Oca zu aexdx zp t oxaaxd hts tniyg ntys\n" .
"twaxdytazkxp, Z htya ai xyqicdtgx aeta hts os ntfzyg za qinuidatowx.\n" .
"Pi aeta'p heta Z'kx adzxr ai ri.\n" .
"Z htya ai piwkx jdiowxnp Z nxxa zy aex rtzws wzux os cpzyg qinjcaxdp,\n" .
"pi Z yxxr ai hdzax jdigdtnp. Os cpzyg Dcos, Z htya ai qiyqxyadtax aex\n" .
"aezygp Z ri, yia aex ntgzqtw dcwxp iu aex wtygctgx, wzfx patdazyg hzae\n" .
"jcowzq kizr pinxaezyg pinxaezyg pinxaezyg ai pts, \"jdzya exwwi hidwr.\"\n" .
"Z vcpa htya ai pts, \"jdzya aezp!\" Z riy'a htya tww aex pcddicyrzyg\n" .
"ntgzq fxshidrp. Z vcpa htya ai qiyqxyadtax iy aex atpf. Aeta'p aex otpzq\n" .
"zrxt. Pi Z etkx adzxr ai ntfx Dcos qirx qiyqzpx tyr pcqqzyqa.\n" .
"Scfzezdi Ntapcniai. (hhh.tdaznt.qin/zyak/dcos)";
my $freqLang = "TEOIARNSHLMYUCWDGPFBVKJ";
my $len = length( $text );
my %frequency;
for( my $k = 0; $k < $len; $k++ )
{
my $c = uc( substr( $text, $k, 1 ) );
if( $c =~ /[A-Z]/ )
{
if( exists( $frequency{ $c } ) ) { $frequency{ $c } = $frequency{ $c } + 1; }
else { $frequency{ $c } = 1; }
}
}
my %dic;
my $freqText;
my $index = 0;
# Sort in descending order
foreach( sort( {$frequency{$b}<=>$frequency{$a}} keys( %frequency ) ) )
{
#print( $_, " - ", $frequency{ $_ }, "\n" );
$freqText .= $_;
$dic{ uc( $_ ) } = substr( $freqLang, $index++, 1);
}
println( $freqText );
println( $freqLang );
my $decrypted;
$len = length( $text );
for( my $k = 0; $k < $len; $k++ )
{
my $uper = 0;
my $c = substr( $text, $k, 1 );
if( $c =~ /[A-Z]/ ) { $uper = 1; }
else { $c = uc( $c ); }
if( exists( $dic{ $c } ) )
{
if( $uper ) { $decrypted .= $dic{ $c }; }
else { $decrypted .= lc( $dic{ $c } ); }
}
else { $decrypted .= $c; }
}
println( $decrypted );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment