Jump to content

Vigenère cipher/Cryptanalysis: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 33:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">-V ascii_uppercase = Array(‘A’..‘Z’)
 
F vigenere_decrypt(target_freqs, input)
Line 125:
V (key, decoded) = vigenere_decrypt(english_frequences, encoded)
print(‘Key: ’key)
print("\nText: "decoded)</langsyntaxhighlight>
 
{{out}}
Line 136:
=={{header|Ada}}==
The program is not fully auto, but makes a small number of suggestions for the right key and plaintext.
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Vignere_Cryptanalysis is
Line 302:
end;
end loop;
end Vignere_Cryptanalysis;</langsyntaxhighlight>
 
=={{header|C}}==
This finds the right key (I think, I didn't try to decode it after getting the key). The program is not fully auto, but by its output, the result is pretty obvious.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 408:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
Not guaranteed to give a 100% correct answer, but it works here. Requires C++0x.
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <vector>
Line 579:
cout << "Key: " << output.second << endl << endl;
cout << "Text: " << output.first << endl;
}</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.typecons, std.string,
std.array, std.numeric, std.ascii;
 
Line 714:
immutable key_dec = vigenereDecrypt(englishFrequences, encoded);
writefln("Key: %s\n\nText: %s", key_dec[0], key_dec[1]);
}</langsyntaxhighlight>
{{out|Output (cut)}}
<pre>Key: THECHESHIRECAT
Line 722:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 840:
fmt.Println("\nBest key :", bestKey)
fmt.Printf("\nDecrypted text:\n%s\n", decrypt(enc, bestKey))
}</langsyntaxhighlight>
 
{{out}}
Line 890:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-}
import Data.List(transpose, nub, sort, maximumBy)
import Data.Ord (comparing)
Line 987:
\BXVCB XBAWG LQKCM ICRRX MACUO IKHQU AJEGL OIJHH XPVZW JEWBA\
\FWAML ZZRXJ EKAHV FASMU LVVUT TGK\
\"</langsyntaxhighlight>
{{out}}
<pre style="font-size:80%">
Line 997:
{{trans|C}}
 
<langsyntaxhighlight Javalang="java">public class Vig{
static String encodedMessage =
"MOMUD EKAPV TQEFM OEVHP AJMII CDCTI FGYAG JSPXY ALUYM NSMYH VUXJE LEPXJ FXGCM JHKDZ RYICU HYPUS PGIGM OIYHF WHTCQ KMLRD ITLXZ LJFVQ GHOLW CUHLO MDSOE KTALU VYLNZ RFGBX PHVGA LWQIS FGRPH JOOFW GUBYI LAPLA LCAFA AMKLG CETDW VOELJ IKGJB XPHVG ALWQC SNWBU BYHCU HKOCE XJEYK BQKVY KIIEH GRLGH XEOLW AWFOJ ILOVV RHPKD WIHKN ATUHN VRYAQ DIVHX FHRZV QWMWV LGSHN NLVZS JLAKI FHXUF XJLXM TBLQV RXXHR FZXGV LRAJI EXPRV OSMNP KEPDT LPRWM JAZPK LQUZA ALGZX GVLKL GJTUI ITDSU REZXJ ERXZS HMPST MTEOE PAPJH SMFNB YVQUZ AALGA YDNMP AQOWT UHDBV TSMUE UIMVH QGVRW AEFSP EMPVE PKXZY WLKJA GWALT VYYOB YIXOK IHPDS EVLEV RVSGB JOGYW FHKBL GLXYA MVKIS KIEHY IMAPX UOISK PVAGN MZHPW TTZPV XFCCD TUHJH WLAPF YULTB UXJLN SIJVV YOVDJ SOLXG TGRVO SFRII CTMKO JFCQF KTINQ BWVHG TENLH HOGCS PSFPV GJOKM SIFPR ZPAAS ATPTZ FTPPD PORRF TAXZP KALQA WMIUD BWNCT LEFKO ZQDLX BUXJL ASIMR PNMBF ZCYLV WAPVF QRHZV ZGZEF KBYIO OFXYE VOWGB BXVCB XBAWG LQKCM ICRRX MACUO IKHQU AJEGL OIJHH XPVZW JEWBA FWAML ZZRXJ EKAHV FASMU LVVUT TGK";
Line 1,105:
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
 
<langsyntaxhighlight Julialang="julia"># ciphertext block {{{1
const ciphertext = filter(isalpha, """
MOMUD EKAPV TQEFM OEVHP AJMII CDCTI FGYAG JSPXY ALUYM NSMYH
Line 1,315:
gc()
t = @elapsed cryptanalyze(ciphertext)
println("\nelapsed time: ", t, " seconds")</langsyntaxhighlight>
 
{{out}}
Line 1,328:
{{trans|C}}
This is a reasonably faithful translation of the C entry though I've restricted the key lengths examined to 26 to automatically produce the correct key and hence decrypted text. This is because the C entry examines key lengths up to 29 and a value of 28 gives a slightly better fit even though the key produced (THECHESCIRECATTHECHESHIRECAT) and resulting text don't make as much sense and so would be rejected if one were examining the candidate keys manually.
<langsyntaxhighlight lang="scala">// version 1.1.3
 
val encoded =
Line 1,429:
println("Best key : $bestKey")
println("\nDecrypted text:\n${decrypt(enc, bestKey)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,471:
{{trans|Phix}}
This is a translation of Julia algorithm with some ideas from Phix translation.
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils, sugar, tables, times
 
const
Line 1,573:
echo "key: ", key, '\n'
echo dec, '\n'
echo "Elapsed time: ", (cpuTime() - t0).formatFloat(ffDecimal, precision = 3), " s"</langsyntaxhighlight>
 
{{out}}
Line 1,587:
Uses the Vigenere decrypt function from the Vigenere task solution (not included in the code below).
{{works with|OCaml|above 4.05}}
<syntaxhighlight lang="ocaml">
<lang OCaml>
(* Task : Vigenere cipher/Cryptanalysis *)
 
Line 1,709:
Printf.printf "Key: %s\n\nText: %s" key pt
;;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,718:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,823:
"Key length " . length($key) . "\n" .
"Plaintext " . substr(mycrypt($text, $key), 0, 80) . "...\n";
}</langsyntaxhighlight>
 
{{out}}
Line 1,848:
=={{header|Phix}}==
{{trans|Julia}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Cryptanalysis.exw
Line 2,060:
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{Out}}
<pre>
Line 2,082:
=={{header|Python}}==
{{trans|D}}
<langsyntaxhighlight lang="python">from string import uppercase
from operator import itemgetter
 
Line 2,187:
print "\nText:", decoded
 
main()</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 2,194:
This is a simple method that just tries to find a key of any length that minimizes the difference from the expected English character distributions.
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang at-exp racket
 
Line 2,266:
(display (integer->char (+ first-char (decode-num n))))))
(newline)
</syntaxhighlight>
</lang>
 
Output:
Line 2,279:
This is an attempt at following the [http://en.wikipedia.org/wiki/Index_of_coincidence#Example Wikipedia] description. However, it performs just as well as the simple version. Most likely because I know almost nothing about cryptography...
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang at-exp racket
 
Line 2,385:
(display (integer->char (+ first-char (decode-num n))))))
(newline)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line># from Wikipedia
constant %English-letter-freq = (
E => 12.70, L => 4.03, Y => 1.97, P => 1.93, T => 9.06, A => 8.17, O => 7.51, I => 6.97, N => 6.75,
Line 2,482:
"Key length {$key.chars}\n" ~
"Plaintext {substr(mycrypt($cipher-text, $key), 0, 80)}...\n";
}</langsyntaxhighlight>
{{out}}
<pre>Key THECHESHIRECAT
Line 2,507:
{{trans|Kotlin}}
Note that the character to/from byte ('''u8''') conversions work here only because the key and cryptogram are composed of ASCII characters only. Indeed, Rust's '''char''' type is a ''Unicode scalar value'', how they are represented is well summarized in the Rust book's [https://doc.rust-lang.org/std/primitive.char.html subchapter on strings].
<syntaxhighlight lang="rust">
<lang Rust>
use std::iter::FromIterator;
 
Line 2,617:
println!("\nDecrypted text:\n{}", decrypt(&enc, &best_key));
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,628:
=={{header|Tcl}}==
{{trans|Python}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create VigenereAnalyzer {
Line 2,745:
return [my GetKeyFromFreqs $freqs]
}
}</langsyntaxhighlight>
Demonstration (that assumes that the Tcl solution to [[Vigenère cipher#Tcl|Vigenère cipher]] task is present):
<langsyntaxhighlight lang="tcl">set encoded "
MOMUD EKAPV TQEFM OEVHP AJMII CDCTI FGYAG JSPXY ALUYM NSMYH
VUXJE LEPXJ FXGCM JHKDZ RYICU HYPUS PGIGM OIYHF WHTCQ KMLRD
Line 2,771:
set decoded [decoder decrypt $encoded]
puts "Key: $key"
puts "Text: $decoded"</langsyntaxhighlight>
 
=={{header|Vedit macro language}}==
Line 2,787:
If this does not solve some encrypted text, you could increase the number of key combinations to be checked.
 
<langsyntaxhighlight lang="vedit">// (1) Copy text into tmp buffer and remove non-alpha chars.
 
Chdir(PATH_ONLY)
Line 3,103:
BOL
#37 = Search_Block("|V", Cur_Pos, EOL_Pos, ALL+NOERR)
Return </langsyntaxhighlight>
 
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">import strings
const encoded =
"MOMUD EKAPV TQEFM OEVHP AJMII CDCTI FGYAG JSPXY ALUYM NSMYH" +
Line 3,220:
println("\nBest key : $best_key")
println("\nDecrypted text:\n${decrypt(enc, best_key)}")
}</langsyntaxhighlight>
 
{{out}}
Line 3,275:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Nums
import "/trait" for Stepped
import "/str" for Char, Str
Line 3,379:
System.print()
System.print("Best key : %(bestKey)")
System.print("\nDecrypted text:\n%(decrypt.call(enc, bestKey))")</langsyntaxhighlight>
 
{{out}}
Line 3,419:
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl">var[const] uppercase=["A".."Z"].pump(String),
english_frequences=T( // A..Z
0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015,
Line 3,480:
}).concat() :
T(key,_);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">encryptedText:=
#<<<
"MOMUD EKAPV TQEFM OEVHP AJMII CDCTI FGYAG JSPXY ALUYM NSMYH
Line 3,503:
key,decoded:=vigenere_decrypt(english_frequences,encryptedText);
println("Key:", key);
println("Decoded text:", decoded);</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.