The ISAAC cipher: Difference between revisions
m
syntax highlighting fixup automation
(temporarily bumping this down to draft because of keying issue -- see talk page) |
Thundergnat (talk | contribs) 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">
/* Known to compile and work with tcc in win32 & gcc on Linux (with warnings)
------------------------------------------------------------------------------
Line 295:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 308:
=={{header|C sharp}}==
XOR with decryption check.
<
using System;
Line 468:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 479:
=={{header|C++}}==
{{trans|Modula-2}}
<
#include <iomanip>
#include <iostream>
Line 682:
cout << "MOD dcr: " << modPlainText << endl;
}
</syntaxhighlight>
{{out}}
<pre>
Line 694:
=={{header|Common Lisp}}==
<
(:use cl))
Line 936:
(terpri)
(format t "MOD dcr: ~a~%" cptx))))
(values))</
{{out}}
<pre>ISAAC> (main-test)
Line 948:
=={{header|D}}==
Improved from the C# version. XOR with decryption check.
<
import std.algorithm: copy;
import std.typetuple: TypeTuple;
Line 1,112:
writeln("Decrypted: ", decrypted.assumeUTF);
}</
{{out}}
<pre>Message : a Top Secret secret
Line 1,121:
=={{header|Delphi}}==
Translation of Pascal.
<syntaxhighlight lang="delphi">
{$apptype console}
PROGRAM RosettaIsaac;
Line 1,320:
Writeln('MOD : ',mctx);
END.
</syntaxhighlight>
{{out}}
<pre>
Line 1,332:
{{works with|node.js|0.10.32}}
{{trans|C#}}
<
randcnt = 0;
mm = new Uint32Array(256);
Line 1,447:
xptx = z[1];
console.log('xor: '+printable_hex(xctx))
console.log('decrypted: '+xptx)</
{{out}}
<pre>
Line 1,456:
</pre>
=={{header|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>
{{out}}
<pre>
Line 1,712:
=={{header|FreeBASIC}}==
{{trans|C}}
<
' compile with: fbc -s console
Line 1,950:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>message: a Top Secret secret
Line 1,961:
=={{header|Go}}==
XOR version
<
import "fmt"
Line 2,079:
}
return fmt.Sprintf("%X", b)
}</
{{out}}
<pre>
Line 2,088:
=={{header|Haskell}}==
<
import Data.Word (Word, Word32)
import Data.Bits (shift, xor)
Line 2,222:
putStrLn $ "Key : " ++ key
putStrLn $ "XOR : " ++ hexify ver
putStrLn $ "XOR dcr: " ++ unver</
{{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">
package src ;
import haxe.Int32;
Line 2,398:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,412:
{{trans|C}}
In this draft, only the XOR implementation (vernam) is implemented:
<
ub4=: (#.33{.1)&|
xor=: ub4@(2b10110 b.)
Line 2,480:
}}
vernam=: {{ y xor&.(3&u:) iRandA #y }}</
Task example:<
1c0636190b1260233b35125f1e1d0e2f4c5422
vernam E [ 1 iSeed 'this is my secret key'
a Top Secret secret</
=={{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.
<
import java.util.Arrays;
import java.util.Random;
Line 2,719:
System.out.printf("MOD dcr: %s\n", caesarDecrypted);
}
}</
=={{header|Julia}}==
<syntaxhighlight lang="julia">
"""
Julia translation of code from the following:
Line 2,903:
const key = "this is my secret key"
test(IState(), msg, key)
</syntaxhighlight>
{{output}}<pre>
Message: a Top Secret secret
Line 2,913:
=={{header|Kotlin}}==
{{trans|C}}
<
/* external results */
Line 3,075:
println("MOD : ${cctx.toHexByteString()}")
println("MOD dcr : $cptx")
}</
{{out}}
Line 3,088:
=={{header|Lua}}==
<
-- ISAAC - Lua 5.3
Line 3,277:
main()
</syntaxhighlight>
{{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.
<
MODULE RosettaIsaac;
Line 3,543:
WriteString('MOD dcr: '); WriteString(ModPlainText); WriteLn;
END RosettaIsaac.
</syntaxhighlight>
{{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.
<
type
Line 3,719:
echo " MOD: ", mctx.toHex
echo "XOR dcr: ", xptx
echo "MOD dcr: ", mptx</
{{out}}
Line 3,731:
=={{header|Pascal}}==
Free Pascal. A fully functional and complete reference solution of the task.
<syntaxhighlight lang="pascal">
PROGRAM RosettaIsaac;
USES
Line 3,953:
Writeln('MOD dcr: ', mptx);
END.
</syntaxhighlight>
{{out}}
<pre>
Line 3,971:
as well as additional convenience functions.
<
use strict;
use Math::Random::ISAAC;
Line 4,001:
map { ord($_) ^ shift(@iranda) } # xor it with rand char
split "", $msg; # Take each character
}</
{{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.
<!--<
<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>
<!--</
{{out}}
<pre>
Line 4,173:
=={{header|PicoLisp}}==
<
(mod32 (pass +)) )
Line 4,277:
95 ) ) ) ) ) ) ) ) ) )
(bye)</
=={{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.
<
import collections
Line 4,483:
print('MOD :', hexify(caesar_encoded))
print('MOD dcr:', caesar_decoded)
</syntaxhighlight>
=={{header|Racket}}==
Line 4,495:
left from after the XOR, and one with a cleanly reseeded state engine.
<
;; Imperative version: Translation of C
;; Vigenère: Translation of Pascal
Line 4,749:
(randctx-b C) => #x902c0691
(randctx-c C) => 10))
}</
{{out}}
Line 4,770:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my \ϕ := 2654435769; constant MOD = 95; constant START = 32;
Line 4,883:
say "MOD : ", $cctx2hex;
say "MOD dcr : ", $cptx;
}</
{{out}}
<pre>
Line 4,896:
=={{header|REXX}}==
===version 1===
<
* 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</
{{out}}
<pre>Message: a Top Secret secret
Line 5,114:
===version 2===
This can be used to encrypt a file and thereafter decrypt it.
<
* 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</
{{out}}
<pre>Please enter a key
Line 5,366:
=={{header|Rust}}==
<
//! includes the XOR version of the encryption scheme
Line 5,537:
}
</syntaxhighlight>
{{out}}
<pre>
Line 5,548:
=={{header|Sidef}}==
{{trans|Perl}}
<
func xor_isaac(key, msg) {
Line 5,567:
say "Key : #{key}"
say "XOR : #{enc}"
say "XOR dcr: #{pack('H*', dec)}"</
{{out}}
<pre>
Line 5,579:
{{works with|Tcl|8.6}}
{{trans|Go}}
<
oo::class create ISAAC {
Line 5,688:
return [binary encode hex [binary format c* $b]]
}
}</
Demonstrating:
<
set msg "a Top Secret secret"
ISAAC create demo $key
puts "Message: $msg"
puts "Key : $key"
puts "XOR : [demo vernam $msg]"</
{{out}}
<pre>
Line 5,708:
{{libheader|Wren-dynamic}}
{{libheader|Wren-fmt}}
<
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)")</
{{out}}
|