The ISAAC cipher: Difference between revisions

m
syntax highlighting fixup automation
(temporarily bumping this down to draft because of keying issue -- see talk page)
m (syntax highlighting fixup automation)
Line 63:
At the top is Bob Jenkins' reference code for ISAAC.
Below and in main() is the task's complete solution for XOR and MOD.
<syntaxhighlight lang="c">
<lang C>
/* Known to compile and work with tcc in win32 & gcc on Linux (with warnings)
------------------------------------------------------------------------------
Line 295:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 308:
=={{header|C sharp}}==
XOR with decryption check.
<langsyntaxhighlight Clang="c sharp">
using System;
 
Line 468:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 479:
=={{header|C++}}==
{{trans|Modula-2}}
<langsyntaxhighlight lang="cpp">
#include <iomanip>
#include <iostream>
Line 682:
cout << "MOD dcr: " << modPlainText << endl;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 694:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defpackage isaac
(:use cl))
 
Line 936:
(terpri)
(format t "MOD dcr: ~a~%" cptx))))
(values))</langsyntaxhighlight>
{{out}}
<pre>ISAAC> (main-test)
Line 948:
=={{header|D}}==
Improved from the C# version. XOR with decryption check.
<langsyntaxhighlight lang="d">import std.algorithm: min;
import std.algorithm: copy;
import std.typetuple: TypeTuple;
Line 1,112:
 
writeln("Decrypted: ", decrypted.assumeUTF);
}</langsyntaxhighlight>
{{out}}
<pre>Message : a Top Secret secret
Line 1,121:
=={{header|Delphi}}==
Translation of Pascal.
<syntaxhighlight lang="delphi">
<lang Delphi>
{$apptype console}
PROGRAM RosettaIsaac;
Line 1,320:
Writeln('MOD : ',mctx);
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,332:
{{works with|node.js|0.10.32}}
{{trans|C#}}
<langsyntaxhighlight lang="ecmascript">randrsl = new Uint32Array(256);
randcnt = 0;
mm = new Uint32Array(256);
Line 1,447:
xptx = z[1];
console.log('xor: '+printable_hex(xctx))
console.log('decrypted: '+xptx)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,456:
</pre>
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">
Tested for VFX Forth and GForth 64bit in Linux
The code was based on and debugged v python
Line 1,681:
caesar>in decode caesar
CR ." Caesar decoded : " caesar-out buff. ;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,712:
=={{header|FreeBASIC}}==
{{trans|C}}
<langsyntaxhighlight lang="freebasic">' version 03-11-2016
' compile with: fbc -s console
 
Line 1,950:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>message: a Top Secret secret
Line 1,961:
=={{header|Go}}==
XOR version
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 2,079:
}
return fmt.Sprintf("%X", b)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,088:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.Array (Array, (!), (//), array, elems)
import Data.Word (Word, Word32)
import Data.Bits (shift, xor)
Line 2,222:
putStrLn $ "Key : " ++ key
putStrLn $ "XOR : " ++ hexify ver
putStrLn $ "XOR dcr: " ++ unver</langsyntaxhighlight>
{{out}}
<pre>Message: a Top Secret secret
Line 2,236:
It is possible in Haxe to create your own 32bit unsigned type,
but that is outside this exercise.
<syntaxhighlight lang="haxe">
<lang Haxe>
package src ;
import haxe.Int32;
Line 2,398:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,412:
{{trans|C}}
In this draft, only the XOR implementation (vernam) is implemented:
<langsyntaxhighlight Jlang="j">NB. bitwise logic on 32 bit unsigned values
ub4=: (#.33{.1)&|
xor=: ub4@(2b10110 b.)
Line 2,480:
}}
 
vernam=: {{ y xor&.(3&u:) iRandA #y }}</langsyntaxhighlight>
 
Task example:<langsyntaxhighlight Jlang="j"> ,hfd 3 u:E=: vernam 'a Top Secret secret' [ 1 iSeed 'this is my secret key'
1c0636190b1260233b35125f1e1d0e2f4c5422
vernam E [ 1 iSeed 'this is my secret key'
a Top Secret secret</langsyntaxhighlight>
 
=={{header|Java}}==
Line 2,495:
This implementation extends the java.util.Random class, so it inherits methods that generate booleans, floats, doubles and longs, and can also generate numbers with Gaussian and uniform distribution. It can also be plugged in to standard library methods that receive a Random instance as a source of randomness. The toHexString() and main() methods are for demo purposes only and can be removed without changing main functionality.
 
<langsyntaxhighlight Javalang="java">import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Random;
Line 2,719:
System.out.printf("MOD dcr: %s\n", caesarDecrypted);
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
"""
Julia translation of code from the following:
Line 2,903:
const key = "this is my secret key"
test(IState(), msg, key)
</syntaxhighlight>
</lang>
{{output}}<pre>
Message: a Top Secret secret
Line 2,913:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
/* external results */
Line 3,075:
println("MOD : ${cctx.toHexByteString()}")
println("MOD dcr : $cptx")
}</langsyntaxhighlight>
 
{{out}}
Line 3,088:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">#!/usr/bin/env lua
-- ISAAC - Lua 5.3
 
Line 3,277:
 
main()
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,292:
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
I changed the identifiers to clearer ones and I changed the variables <code>a</code>, <code>b</code>, ..., <code>h</code> to an array, because they made my blood boil.
<langsyntaxhighlight lang="modula2">
MODULE RosettaIsaac;
 
Line 3,543:
WriteString('MOD dcr: '); WriteString(ModPlainText); WriteLn;
END RosettaIsaac.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,563:
Note that the "mix" procedure could possibly be transformed in a template or be marked as "inline" (in the C version, it is a "define"). But as we are not in procedure whose performance is critical, expanding the code rather than calling a procedure is not very useful.
 
<langsyntaxhighlight Nimlang="nim">import strutils
 
type
Line 3,719:
echo " MOD: ", mctx.toHex
echo "XOR dcr: ", xptx
echo "MOD dcr: ", mptx</langsyntaxhighlight>
 
{{out}}
Line 3,731:
=={{header|Pascal}}==
Free Pascal. A fully functional and complete reference solution of the task.
<syntaxhighlight lang="pascal">
<lang Pascal>
PROGRAM RosettaIsaac;
USES
Line 3,953:
Writeln('MOD dcr: ', mptx);
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,971:
as well as additional convenience functions.
 
<langsyntaxhighlight lang="perl">use warnings;
use strict;
use Math::Random::ISAAC;
Line 4,001:
map { ord($_) ^ shift(@iranda) } # xor it with rand char
split "", $msg; # Take each character
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,013:
{{trans|Pascal}}
We need the r32() function to convert our common sense maths into the needed unsigned_and_throw_away_any_high_bits maths.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\ISAAC_Cipher.exw
Line 4,161:
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<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 4,173:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de add32 @
(mod32 (pass +)) )
 
Line 4,277:
95 ) ) ) ) ) ) ) ) ) )
 
(bye)</langsyntaxhighlight>
 
=={{header|Python}}==
Line 4,287:
This implementation extends the Random class of the built-in random module, so it automatically inherits methods for generating several distributions, as well as support for shuffling and sampling collections.
 
<langsyntaxhighlight Pythonlang="python">import random
import collections
 
Line 4,483:
print('MOD :', hexify(caesar_encoded))
print('MOD dcr:', caesar_decoded)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 4,495:
left from after the XOR, and one with a cleanly reseeded state engine.
 
<langsyntaxhighlight lang="racket">#lang racket
;; Imperative version: Translation of C
;; Vigenère: Translation of Pascal
Line 4,749:
(randctx-b C) => #x902c0691
(randctx-c C) => 10))
}</langsyntaxhighlight>
 
{{out}}
Line 4,770:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my uint32 (@mm, @randrsl, $randcnt, $aa, $bb, $cc);
my \ϕ := 2654435769; constant MOD = 95; constant START = 32;
 
Line 4,883:
say "MOD : ", $cctx2hex;
say "MOD dcr : ", $cptx;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,896:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 24.07.2014 Walter Pachl translated from Pascal
* extend with decryption (following Pascal)
Line 5,103:
If arg(3)<>'' Then
res=res+arg(3)
return res//4294967296</langsyntaxhighlight>
{{out}}
<pre>Message: a Top Secret secret
Line 5,114:
===version 2===
This can be used to encrypt a file and thereafter decrypt it.
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 25.07.2014 Walter Pachl framing version 1 for processing a file
*--------------------------------------------------------------------*/
Line 5,352:
parse Arg fid
Parse Var fid fn '.' ft
Return fn</langsyntaxhighlight>
{{out}}
<pre>Please enter a key
Line 5,366:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
//! includes the XOR version of the encryption scheme
 
Line 5,537:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 5,548:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('Math::Random::ISAAC')
 
func xor_isaac(key, msg) {
Line 5,567:
say "Key : #{key}"
say "XOR : #{enc}"
say "XOR dcr: #{pack('H*', dec)}"</langsyntaxhighlight>
{{out}}
<pre>
Line 5,579:
{{works with|Tcl|8.6}}
{{trans|Go}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create ISAAC {
Line 5,688:
return [binary encode hex [binary format c* $b]]
}
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">set key "this is my secret key"
set msg "a Top Secret secret"
ISAAC create demo $key
puts "Message: $msg"
puts "Key : $key"
puts "XOR : [demo vernam $msg]"</langsyntaxhighlight>
{{out}}
<pre>
Line 5,708:
{{libheader|Wren-dynamic}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/trait" for Stepped
import "/dynamic" for Enum
import "/fmt" for Fmt
Line 5,887:
System.print("XOR dcr : %(vptx)")
System.print("MOD : %(toHexByteString.call(cctx))")
System.print("MOD dcr : %(cptx)")</langsyntaxhighlight>
 
{{out}}
10,327

edits