The ISAAC cipher: Difference between revisions

m
(Add Rust implementation)
m (→‎{{header|Wren}}: Minor tidy)
 
(16 intermediate revisions by 9 users not shown)
Line 1:
{{draft task|Encryption}}
ISAAC is a cryptographically secure pseudo-random number generator (CSPRNG) and stream cipher. It was developed by Bob Jenkins from 1993 (http://burtleburtle.net/bob/rand/isaac.html) and placed in the Public Domain. ISAAC is fast - especially when optimised - and portable to most architectures in nearly all programming and scripting languages.
It is also simple and succinct, using as it does just two 256-word arrays for its state.
Line 59:
But building a strong and simple ISAAC-based stream cipher - replacing the irreparably broken RC4 - is our goal here: ISAAC's intended purpose.
<br><br>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
{{trans|C}}
<syntaxhighlight lang="freebasic">' version 03-11-2016
' compile with: fbc -s console
 
Dim Shared As UInteger<32> randrsl(256), randcnt
Static Shared As UInteger<32> mm(256)
Static Shared As UInteger<32> aa, bb ,cc
 
Sub ISAAC()
Dim As UInteger<32> i, x, y
cc = cc + 1
bb = bb + cc
For i = 0 To 256 -1
x = mm(i)
Select Case (i Mod 4)
Case 0 : aa = aa Xor (aa Shl 13)
Case 1 : aa = aa Xor (aa Shr 6)
Case 2 : aa = aa Xor (aa Shl 2)
Case 3 : aa = aa Xor (aa Shr 16)
End Select
aa = mm((i+128) Mod 256) + aa
y = mm((x Shr 2) Mod 256) + aa + bb : mm(i) = y
bb = mm((y Shr 10) Mod 256) + x : randrsl(i) = bb
Next
randcnt = 0
End Sub
 
#Macro mix(a, b, c, d, e, f, g, h)
a Xor= b Shl 11 : d += a : b += c
b Xor= c Shr 2 : e += b : c += d
c Xor= d Shl 8 : f += c : d += e
d Xor= e Shr 16 : g += d : e += f
e Xor= f Shl 10 : h += e : f += g
f Xor= g Shr 4 : a += f : g += h
g Xor= h Shl 8 : b += g : h += a
h Xor= a Shr 9 : c += h : a += b
#EndMacro
 
Sub randinit(flag As Long)
Dim As Long i
Dim As UInteger<32> a = &H9e3779b9 '/* the golden ratio *
Dim As UInteger<32> b = &H9e3779b9
Dim As UInteger<32> c = &H9e3779b9
Dim As UInteger<32> d = &H9e3779b9
Dim As UInteger<32> e = &H9e3779b9
Dim As UInteger<32> f = &H9e3779b9
Dim As UInteger<32> g = &H9e3779b9
Dim As UInteger<32> h = &H9e3779b9
aa = 0 : bb = 0 : cc = 0
For i = 0 To 3
mix(a, b, c, d, e, f, g, h)
Next
For i = 0 To 255 Step 8
If flag = 1 Then
a += randrsl(i ) : b += randrsl(i +1)
c += randrsl(i +2) : d += randrsl(i +3)
e += randrsl(i +4) : f += randrsl(i +5)
g += randrsl(i +6) : h += randrsl(i +7)
mix(a, b, c, d, e, f, g, h)
mm(i ) = a : mm(i +1) = b : mm(i +2) = c : mm(i +3) = d
mm(i +4) = e : mm(i +5) = f : mm(i +6) = g : mm(i +7) = h
End If
Next
If flag = 1 Then
For i = 0 To 255 Step 8
a += mm(i ) : b += mm(i +1)
c += mm(i +2) : d += mm(i +3)
e += mm(i +4) : f += mm(i +5)
g += mm(i +6) : h += mm(i +7)
mix(a, b, c, d, e, f, g, h)
mm(i )= a : mm(i +1) = b : mm(i +2) = c : mm(i +3) = d
mm(i +4)= e : mm(i +5) = f : mm(i +6) = g : mm(i +7) = h
Next
End If
ISAAC()
randcnt = 0
End Sub
 
' // Get a random 32-bit value 0..MAXINT
Function iRandom() As UInteger<32>
Dim As UInteger<32> r = randrsl(randcnt)
randcnt += 1
If randcnt > 255 Then
ISAAC()
randcnt = 0
End If
Return r
End Function
 
' // Get a random character in printable ASCII range
Function iRandA() As UByte
Return iRandom() Mod 95 +32
End Function
 
' // Seed ISAAC with a string
Sub iSeed(seed As String, flag As Long)
Dim As ULong i, m = Len(seed) -1
For i = 0 To 255
mm(i) = 0
Next
For i = 0 To 255
If i > m Then
randrsl(i) = 0
Else
randrsl(i) = seed[i]
End If
Next
randinit(flag)
End Sub
 
' // maximum length of message
'#define MAXMSG 4096
#Define _MOD_ 95 ' mod is FreeBASIC keyword
#Define _START_ 32 ' start is used as variable name
 
' // cipher modes for Caesar
Enum ciphermode
mEncipher
mDecipher
mNone
End Enum
 
' // XOR cipher on random stream. Output: ASCII string
' no maximum lenght for input and output string
Function Vernam(msg As String) As String
Dim As ULong i
Dim As String v
For i = 0 To Len(msg) -1
v += Chr(iRandA() Xor msg[i])
Next
Return v
End Function
 
' // Caesar-shift a printable character
Function Ceasar(m As ciphermode, ch As UByte, shift As UByte, modulo As UByte, _
start As UByte) As UByte
' FreeBASIC Mod does not handle negative numbers correctly
' also there is litte problem with shift (declared UByte)
' the IIF() statement helps with shift
' to avoid a negative n a 8 times modulo is added
' modulo * 8 get translateted by FreeBASIC to modulo shl 3
Dim As Long n = (ch - start) + IIf(m = mDecipher, -shift, shift) + modulo * 8
n = n Mod modulo
Return start + n
End Function
 
' // Caesar-shift a string on a pseudo-random stream
Function CeasarStr(m As ciphermode, msg As String, modulo As UByte, _
start As UByte) As String
Dim As Long i
Dim As String v
For i = 0 To Len(msg) -1
v += Chr(Ceasar(m, msg[i], iRandA(), modulo, start))
Next
Return v
End Function
 
' ------=< MAIN >=------
 
Dim As Long n, l
Dim As String msg = "a Top Secret secret"
Dim As String key = "this is my secret key"
 
Dim As String vctx, vptx
Dim As String cctx, cptx
 
l = Len(msg)
' // Encrypt: Vernam XOR
iSeed(key, 1)
vctx = Vernam(msg)
' // Encrypt: Caesar
cctx = CeasarStr(mEncipher, msg, _mod_, _start_)
' // Decrypt: Vernam XOR
iSeed(key, 1)
vptx = Vernam(vctx)
' // Decrypt: Caesar
cptx = CeasarStr(mDecipher, cctx, _mod_, _start_)
Print "message: "; msg
Print " key: "; key
Print " XOR: ";
' // Output Vernam ciphertext as a string of hex digits
For n = 0 To l -1
Print Hex(vctx[n], 2);
Next
Print
' // Output Vernam decrypted plaintext
Print "XOR dcr: "; vptx
' // Caesar
Print " MOD: ";
' // Output Caesar ciphertext as a string of hex digits
For n= 0 To l -1
Print Hex(cctx[n], 2);
Next
Print
' // Output Caesar decrypted plaintext
Print "MOD dcr: " ; cptx
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>message: a Top Secret secret
key: this is my secret key
XOR: 1C0636190B1260233B35125F1E1D0E2F4C5422
XOR dcr: a Top Secret secret
MOD: 734270227D36772A783B4F2A5F206266236978
MOD dcr: a Top Secret secret</pre>
 
=={{header|C}}==
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 ⟶ 507:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 308 ⟶ 520:
=={{header|C sharp}}==
XOR with decryption check.
<langsyntaxhighlight Clang="c sharp">
using System;
 
Line 468 ⟶ 680:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 479 ⟶ 691:
=={{header|C++}}==
{{trans|Modula-2}}
<langsyntaxhighlight lang="cpp">
#include <iomanip>
#include <iostream>
Line 682 ⟶ 894:
cout << "MOD dcr: " << modPlainText << endl;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 694 ⟶ 906:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defpackage isaac
(:use cl))
 
Line 936 ⟶ 1,148:
(terpri)
(format t "MOD dcr: ~a~%" cptx))))
(values))</langsyntaxhighlight>
{{out}}
<pre>ISAAC> (main-test)
Line 948 ⟶ 1,160:
=={{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 ⟶ 1,324:
 
writeln("Decrypted: ", decrypted.assumeUTF);
}</langsyntaxhighlight>
{{out}}
<pre>Message : a Top Secret secret
Line 1,121 ⟶ 1,333:
=={{header|Delphi}}==
Translation of Pascal.
<syntaxhighlight lang="delphi">
<lang Delphi>
{$apptype console}
PROGRAM RosettaIsaac;
Line 1,320 ⟶ 1,532:
Writeln('MOD : ',mctx);
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,332 ⟶ 1,544:
{{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 ⟶ 1,659:
xptx = z[1];
console.log('xor: '+printable_hex(xctx))
console.log('decrypted: '+xptx)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,455 ⟶ 1,667:
decrypted: a Top Secret secret
</pre>
=={{header|Forth}}==
<syntaxhighlight lang="forth">
Tested for VFX Forth and GForth 64bit in Linux
The code was based on and debugged v python
 
\ ******* Words for 32 bit fetching and storing *******
=={{header|FreeBASIC}}==
\ * Implement words for 32 bit fields (assuming 64 bit Forth) *
{{trans|C}}
\ *************************************************************
<lang freebasic>' version 03-11-2016
' compile with: fbc -s console
 
: halves 4 * ; \ half a cell.
Dim Shared As UInteger<32> randrsl(256), randcnt
Static Shared As UInteger<32> mm(256)
Static Shared As UInteger<32> aa, bb ,cc
 
\ VFX Forth
Sub ISAAC()
\ : h@ L@ ; : h! L! ; : h+! L+! ;
\ GFORTH
: h@ UL@ ; : h! L! ; : h+! DUP h@ ROT + SWAP h! ;
\ a 32 bit Forth
\ : h@ @ ; : h! ! ; : h+! +! ;
 
: half-array \ n <'name'> -- ; DOES> n -- a ;
Dim As UInteger<32> i, x, y
\ Use: 8 half-array test - creates an array of 8 32 bit elements.
\ Does> 4 test - returns the address of the 4th element of test.
CREATE HERE SWAP halves DUP ALLOT ERASE
DOES> SWAP halves + ;
 
\ ***** Words to implement an isaac rng *****
cc = cc + 1
8 half-array ]init-state
bb = bb + cc
 
: init+! \ ix-tgt ix-src -- ;
For i = 0 To 256 -1
]init-state h@ SWAP ]init-state h+! ;
x = mm(i)
Select Case (i Mod 4)
Case 0 : aa = aa Xor (aa Shl 13)
Case 1 : aa = aa Xor (aa Shr 6)
Case 2 : aa = aa Xor (aa Shl 2)
Case 3 : aa = aa Xor (aa Shr 16)
End Select
aa = mm((i+128) Mod 256) + aa
y = mm((x Shr 2) Mod 256) + aa + bb : mm(i) = y
bb = mm((y Shr 10) Mod 256) + x : randrsl(i) = bb
Next
 
: init-right-xor! \ ix-tgt ix-src shift-bits -- ;
randcnt = 0
SWAP ]init-state h@ SWAP RSHIFT SWAP ]init-state TUCK h@ XOR SWAP h! ;
 
: init-left-xor! \ ix-tgt ix-src shift-bits -- ;
End Sub
SWAP ]init-state h@ SWAP LSHIFT SWAP ]init-state TUCK h@ XOR SWAP h! ;
 
: mix
0 1 11 init-left-xor! 3 0 init+! 1 2 init+!
1 2 2 init-right-xor! 4 1 init+! 2 3 init+!
2 3 8 init-left-xor! 5 2 init+! 3 4 init+!
3 4 16 init-right-xor! 6 3 init+! 4 5 init+!
4 5 10 init-left-xor! 7 4 init+! 5 6 init+!
5 6 4 init-right-xor! 0 5 init+! 6 7 init+!
6 7 8 init-left-xor! 1 6 init+! 7 0 init+!
7 0 9 init-right-xor! 2 7 init+! 0 1 init+! ;
 
\ State variables and arrays
#Macro mix(a, b, c, d, e, f, g, h)
VARIABLE aa VARIABLE bb VARIABLE cc VARIABLE rand-count
256 half-array ]mm
256 half-array ]result
 
\ Jump table of xts used in isaac-iteration
a Xor= b Shl 11 : d += a : b += c
: shift-xor0 aa bDUP Xor=h@ cDUP Shr13 2LSHIFT XOR :SWAP eh! +=; b : c\ +=-- d;
: shift-xor1 aa cDUP Xor=h@ dDUP Shl6 8 RSHIFT :XOR fSWAP +=h! ; c : d\ +=-- e;
: shift-xor2 aa dDUP Xor=h@ eDUP Shr2 16 :LSHIFT gXOR +=SWAP h! ; d : e\ +=-- f;
: shift-xor3 aa eDUP Xor=h@ fDUP Shl16 10RSHIFT :XOR SWAP h! +=; e : f\ +=-- g;
f Xor= g Shr 4 : a += f : g += h
g Xor= h Shl 8 : b += g : h += a
h Xor= a Shr 9 : c += h : a += b
 
HERE
#EndMacro
' shift-xor0 , ' shift-xor1 , ' shift-xor2 , ' shift-xor3 ,
CONSTANT shift-xor-xts
 
: ]execute-shift-xorn \ ix -- ; Executes the xt in shift-xor-xts
Sub randinit(flag As Long)
CELLS shift-xor-xts + @ EXECUTE ;
 
: isaac-iteration \ -- ;
Dim As Long i
1 cc h+! cc h@ bb h+!
Dim As UInteger<32> a = &H9e3779b9 '/* the golden ratio *
256 0 DO \ Python code
Dim As UInteger<32> b = &H9e3779b9
I ]mm h@ \ x = mm[i] Store mm[i] on the stack
Dim As UInteger<32> c = &H9e3779b9
Dim As UInteger<32> d = &H9e3779b9
Dim As UInteger<32> e = &H9e3779b9
Dim As UInteger<32> f = &H9e3779b9
Dim As UInteger<32> g = &H9e3779b9
Dim As UInteger<32> h = &H9e3779b9
aa = 0 : bb = 0 : cc = 0
 
ForI 3 AND ]execute-shift-xorn i = 0 To 3
\ Executes shift-xor0 .. 3 from shift-xor-xts above.
mix(a, b, c, d, e, f, g, h)
Next
 
aa DUP h@ I 128 XOR ]mm h@ + SWAP h! \ aa = (mm[i^128] + aa)
For i = 0 To 255 Step 8
If flag = 1 Then
a += randrsl(i ) : b += randrsl(i +1)
c += randrsl(i +2) : d += randrsl(i +3)
e += randrsl(i +4) : f += randrsl(i +5)
g += randrsl(i +6) : h += randrsl(i +7)
 
DUP 2 RSHIFT 0xFF AND ]mm h@ aa mix(a,h@ b,+ c,bb d,h@ e,+ f, g,\ hmm[(x>>2) & 0xFF] + aa + bb)
DUP I ]mm h! \ y = mm([i )] = a; :Store mm(i +1) = b :in ]mm(i +2)leave =y con : mm(i +3) =the dstack
mm(i +4) = e : mm(i +5) = f : mm(i +6) = g : mm(i +7) = h
End If
Next
 
10 RSHIFT 0xFF AND ]mm h@ + \ mm[(y>>10) & 0xFF] + x)
If flag = 1 Then
DUP bb h! I For]result h! \ result[i] = 0self.bb To= 255; Stepstore 8in bb and result[i]
a += mm(i ) : b += mm(i +1)
c += mm(i +2) : d += mm(i +3)
e += mm(i +4) : f += mm(i +5)
g += mm(i +6) : h += mm(i +7)
 
LOOP
mix(a, b, c, d, e, f, g, h)
0 rand-count ! ;
mm(i )= a : mm(i +1) = b : mm(i +2) = c : mm(i +3) = d
mm(i +4)= e : mm(i +5) = f : mm(i +6) = g : mm(i +7) = h
Next
End If
 
256 half-array ]seed
ISAAC()
: zero-fill \ a ct -- ;
randcnt = 0
halves ERASE ;
 
: seed-mt 0 ]seed 256 zero-fill ;
End Sub
 
: string>seed \ a ct -- ;
' // Get a random 32-bit value 0..MAXINT
seed-mt 256 MIN ]seed 0 ]seed ?DO COUNT I h! 4 +LOOP DROP ;
Function iRandom() As UInteger<32>
 
: mm-mt 0 ]mm 256 zero-fill ;
Dim As UInteger<32> r = randrsl(randcnt)
: init-vars 0 aa ! 0 bb ! 0 cc ! 256 rand-count ! ;
randcnt += 1
: res-mt 0 ]result 256 zero-fill ;
If randcnt > 255 Then
ISAAC()
randcnt = 0
End If
 
: base-init-state
Return r
8 ]init-state 0 ]init-state DO 0x9e3779b9 i h! 4 +LOOP
mix mix mix mix ;
 
: init>mm-ix \ ix -- ;
End Function
0 ]init-state SWAP ]mm 8 halves MOVE ;
 
: init>mm \ -- ;
' // Get a random character in printable ASCII range
256 0 DO mix I init>mm-ix 8 +LOOP
Function iRandA() As UByte
0 aa h! 0 bb h! 0 cc h! ;
 
: default-seed \ -- ;
Return iRandom() Mod 95 +32
base-init-state init>mm ;
 
: seed>init-state>mm
End Function
256 0 DO
8 0 DO I J + ]seed h@ I ]init-state h+! LOOP
mix
0 ]init-state I ]mm 8 halves MOVE
8 +LOOP ;
 
: init-mm-mix
' // Seed ISAAC with a string
256 0 DO
Sub iSeed(seed As String, flag As Long)
8 0 DO I J + ]mm h@ I ]init-state h+! LOOP
mix
I init>mm-ix
8 +LOOP ;
 
: string-seed \ str ct -- ;
Dim As ULong i, m = Len(seed) -1
string>seed base-init-state seed>init-state>mm
init-mm-mix init-vars res-mt ;
 
: random-next \ -- h ; 32 bit result
For i = 0 To 255
rand-count @ 255 > IF isaac-iteration 0 rand-count ! THEN
mm(i) = 0
rand-count @ DUP ]result h@ SWAP 1+ rand-count ! ;
Next
 
: rand-char \ -- ch ;
For i = 0 To 255
random-next 95 MOD 32 + ;
 
\ Encode, Decode and display.
If i > m Then
randrsl(i) = 0
Else
randrsl(i) = seed[i]
End If
 
\ Working buffers
Next
1024 CONSTANT buff-size
CREATE buff-in buff-size ALLOT
CREATE xor-out buff-size ALLOT
CREATE caesar-out buff-size ALLOT
 
: buff-count \ adr -- addr+cell count ; \ Prepares buff for TYPE
randinit(flag)
DUP CELL + SWAP @ ;
 
: buff. buff-count TYPE ; \ addr -- ;
End Sub
 
: byte>hexstr \ b -- str ct ; \ Generates a 2 character hex string.
' // maximum length of message
BASE @ SWAP HEX 0 <# # # #> ROT BASE ! ;
'#define MAXMSG 4096
#Define _MOD_ 95 ' mod is FreeBASIC keyword
#Define _START_ 32 ' start is used as variable name
 
: buff-hex. \ addr --- ;
' // cipher modes for Caesar
buff-count BOUNDS ?DO I C@ byte>hexstr TYPE LOOP ;
Enum ciphermode
mEncipher
mDecipher
mNone
End Enum
 
: buff-empty 0 SWAP ! ; \ addr -- ; \ Empty the buffer. Sets length to zero.
' // XOR cipher on random stream. Output: ASCII string
' no maximum lenght for input and output string
Function Vernam(msg As String) As String
 
: char-append \ ch adr -- ; \ Append ch to the buffer
Dim As ULong i
tuck buff-count + C! 1 SWAP +! ;
Dim As String v
 
: buff-copy \ src dest -- ; \ Copy buffer to buffer
For i = 0 To Len(msg) -1
OVER @ CELL + MOVE ;
v += Chr(iRandA() Xor msg[i])
Next
 
: buff-fill \ str ct buff -- ; \ Fill buffer with the contents of str ct
Return v
2DUP ! CELL + SWAP MOVE ;
 
\ ***** XOR encode/decode *****
End Function
: xor-byte \ b -- b' ;
rand-char XOR ;
 
: xor-code \ str ct -- ;
' // Caesar-shift a printable character
xor-out buff-empty
Function Ceasar(m As ciphermode, ch As UByte, shift As UByte, modulo As UByte, _
BOUNDS ?DO I C@ xor-byte xor-out char-append LOOP ;
start As UByte) As UByte
 
: init-rng" [CHAR] " WORD COUNT string-seed ;
' FreeBASIC Mod does not handle negative numbers correctly
' also there is litte problem with shift (declared UByte)
' the IIF() statement helps with shift
' to avoid a negative n a 8 times modulo is added
' modulo * 8 get translateted by FreeBASIC to modulo shl 3
Dim As Long n = (ch - start) + IIf(m = mDecipher, -shift, shift) + modulo * 8
n = n Mod modulo
Return start + n
 
: xor-code-with" \ str ct <key"> -- ; str ct points to the text to encode
End Function
\ Use: s" Message" encode-xor-key" This is the key."
\ Prints the encoded bytes in hex to the terminal.
init-rng" xor-code ;
 
\ ***** Caesar encode/decode *****
' // Caesar-shift a string on a pseudo-random stream
DEFER op \ ' + for encode, ' - for decode in caesar-code-ch
Function CeasarStr(m As ciphermode, msg As String, modulo As UByte, _
start As UByte) As String
 
: encode ['] + IS op ; \ Add the offset while encoding.
Dim As Long i
: decode ['] - IS op ; \ Substract it to decode
Dim As String v
 
: caesar-code-ch \ c -- c' ;
For i = 0 To Len(msg) -1
rand-char op 32 - 95 MOD
v += Chr(Ceasar(m, msg[i], iRandA(), modulo, start))
BEGIN DUP 0< WHILE 95 + REPEAT 32 + ;
Next
 
: caesar \ str ct -- ;
Return v
\ The direction of caesar-code is modified by the encode / decode words
\ eg. encode s" Message" caesar
\ decode s" DntP8hg" caesar
caesar-out buff-empty
BOUNDS ?DO I C@ caesar-code-ch caesar-out char-append LOOP ;
 
: caesar-with" \ str ct <key" -- ;
End Function
\ The direction of caesar-code is modified by the encode / decode words
\ eg. encode s" Message" caesar-with" Key"
\ decode s" DntP8hg" caesar-with" Key"
init-rng" caesar ;
 
\ ***** Demonstrate the encode/decode working *****
' ------=< MAIN >=------
: message s" a Top Secret secret" ;
 
: out>in \ buff-out -- buff-in' count ;
Dim As Long n, l
buff-in buff-copy buff-in buff-count ;
Dim As String msg = "a Top Secret secret"
Dim As String key = "this is my secret key"
 
: xor>in xor-out out>in ; \ -- buff-in' ct ;
Dim As String vctx, vptx
: caesar>in caesar-out out>in ; \ -- buff-in' ct ;
Dim As String cctx, cptx
 
: examples
l = Len(msg)
CR ." Raw message : " message TYPE
' // Encrypt: Vernam XOR
CR ." First encode."
iSeed(key, 1)
s" this is my secret key" string-seed \ Set key
vctx = Vernam(msg)
message xor-code
' // Encrypt: Caesar
CR ." XOR encoded as hex : " xor-out buff-hex.
cctx = CeasarStr(mEncipher, msg, _mod_, _start_)
message encode caesar
' // Decrypt: Vernam XOR
CR ." Caesar encoded as hex: " caesar-out buff-hex.
iSeed(key, 1)
CR ." Now decode "
vptx = Vernam(vctx)
s" this is my secret key" string-seed \ Set key
' // Decrypt: Caesar
xor>in xor-code
cptx = CeasarStr(mDecipher, cctx, _mod_, _start_)
CR ." XOR decoded : " xor-out buff.
Print "message: "; msg
caesar>in decode caesar
Print " key: "; key
CR ." Caesar decoded : " caesar-out buff. ;
Print " XOR: ";
</syntaxhighlight>
' // Output Vernam ciphertext as a string of hex digits
For n = 0 To l -1
Print Hex(vctx[n], 2);
Next
Print
' // Output Vernam decrypted plaintext
Print "XOR dcr: "; vptx
' // Caesar
Print " MOD: ";
' // Output Caesar ciphertext as a string of hex digits
For n= 0 To l -1
Print Hex(cctx[n], 2);
Next
Print
' // Output Caesar decrypted plaintext
Print "MOD dcr: " ; cptx
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>
<pre>message: a Top Secret secret
message key:xor-code-with" this is my secret key" ok
CR ." XOR encoded as hex : " xor-out buff-hex.
XOR: 1C0636190B1260233B35125F1E1D0E2F4C5422
XOR encoded as hex : 1C0636190B1260233B35125F1E1D0E2F4C5422 ok
XOR dcr: a Top Secret secret
ok
MOD: 734270227D36772A783B4F2A5F206266236978
message encode caesar ok
MOD dcr: a Top Secret secret</pre>
CR ." Caesar encoded as hex: " caesar-out buff-hex.
Caesar encoded as hex: 734270227D36772A783B4F2A5F206266236978 ok
ok
ok
xor>in xor-code-with" this is my secret key" ok
CR ." XOR decoded : " xor-out buff.
XOR decoded : a Top Secret secret ok
ok
caesar>in decode caesar ok
CR ." Caesar decoded : " caesar-out buff.
Caesar decoded : a Top Secret secret ok
ok
examples
Raw message : a Top Secret secret
First encode.
XOR encoded as hex : 1C0636190B1260233B35125F1E1D0E2F4C5422
Caesar encoded as hex: 734270227D36772A783B4F2A5F206266236978
Now decode
XOR decoded : a Top Secret secret
Caesar decoded : a Top Secret secret ok
</pre>
 
=={{header|Go}}==
XOR version
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,825 ⟶ 2,043:
}
return fmt.Sprintf("%X", b)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,834 ⟶ 2,052:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.Array (Array, (!), (//), array, elems)
import Data.Word (Word, Word32)
import Data.Bits (shift, xor)
Line 1,968 ⟶ 2,186:
putStrLn $ "Key : " ++ key
putStrLn $ "XOR : " ++ hexify ver
putStrLn $ "XOR dcr: " ++ unver</langsyntaxhighlight>
{{out}}
<pre>Message: a Top Secret secret
Line 1,982 ⟶ 2,200:
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,144 ⟶ 2,362:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,154 ⟶ 2,372:
MOD dcr: a Top Secret secret
</pre>
 
=={{header|J}}==
{{trans|C}}
In this draft, only the XOR implementation (vernam) is implemented:
<syntaxhighlight lang="j">NB. bitwise logic on 32 bit unsigned values
ub4=: (#.33{.1)&|
xor=: ub4@(2b10110 b.)
shl=: ub4@(33 b.~)
add=: ub4@+
 
isaac=: {{
cc=: cc add 1
bb=: bb add cc
for_i.i.256 do.
aa=. aa xor aa shl 13 _6 2 _16{~4|i
X=. i{mm
aa=. aa add mm{~ 256| i+128
y=. aa add bb add mm{~ 256| X shl _2
randrsl=: randrsl i}~ bb=. X add mm{~ 256| y shl _10
end.
randcnt=: 0
}}
mix=: {{
b=: b add c [ d=: d add a=: a xor b shl 11
c=: c add d [ e=: e add b=: b xor c shl _2
d=: d add e [ f=: f add c=: c xor d shl 8
e=: e add f [ g=: g add d=: d xor e shl _16
f=: f add g [ h=: h add e=: e xor f shl 10
g=: g add h [ a=: a add f=: f xor g shl _4
h=: h add a [ b=: b add g=: g xor h shl 8
a=: a add b [ c=: c add h=: h xor a shl _9
}}
 
randinit=: {{
aa=: bb=: cc=: 0
a=: b=: c=: d=: e=: f=: g=: h=: 16b9e3779b9
mix^:4''
if. y do.
for_i. _8]\i.256 do.
mix 'a b c d e f g h'=: (a,b,c,d,e,f,g,h) add i{randrsl
mm=: mm i}~ a,b,c,d,e,f,g,h
end.
for_i. _8]\i.256 do.
mix 'a b c d e f g h'=: (a,b,c,d,e,f,g,h) add i{mm
mm=: mm i}~ a,b,c,d,e,f,g,h
end.
else.
for_i. _8]\i.256 do.
mix ''
mm=: mm i}~ a,b,c,d,e,f,g,h
end.
end.
isaac''
}}
 
iRandom=: {{
r=. randcnt { randrsl
if. 255 < randcnt=: randcnt+1 do. isaac'' end.
r
}}
 
iRandA=: {{ 7 u: 32+95|iRandom^:(1+i.y)'' }}
 
iSeed=: {{ NB. y: seed, x: flag
0 iSeed y
:
mm=: 256#0
randrsl=: 256{.3 u: y
randinit x
}}
 
vernam=: {{ y xor&.(3&u:) iRandA #y }}</syntaxhighlight>
 
Task example:<syntaxhighlight lang="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</syntaxhighlight>
 
=={{header|Java}}==
Line 2,163 ⟶ 2,459:
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,387 ⟶ 2,683:
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,571 ⟶ 2,867:
const key = "this is my secret key"
test(IState(), msg, key)
</syntaxhighlight>
</lang>
{{output}}<pre>
Message: a Top Secret secret
Line 2,581 ⟶ 2,877:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
/* external results */
Line 2,743 ⟶ 3,039:
println("MOD : ${cctx.toHexByteString()}")
println("MOD dcr : $cptx")
}</langsyntaxhighlight>
 
{{out}}
Line 2,753 ⟶ 3,049:
MOD : 734270227D36772A783B4F2A5F206266236978
MOD dcr : a Top Secret secret
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">#!/usr/bin/env lua
-- ISAAC - Lua 5.3
 
-- External Results
local randRsl = {};
local randCnt = 0;
 
-- Internal State
local mm = {};
local aa,bb,cc = 0,0,0;
 
-- Cap to maintain 32 bit maths
local cap = 0x100000000;
 
-- CipherMode
local ENCRYPT = 1;
local DECRYPT = 2;
 
function isaac()
 
cc = ( cc + 1 ) % cap; -- cc just gets incremented once per 256 results
bb = ( bb + cc ) % cap; -- then combined with bb
 
for i = 0,255 do
local x = mm[i];
local y;
local imod = i % 4;
if imod == 0 then aa = aa ~ (aa << 13);
elseif imod == 1 then aa = aa ~ (aa >> 6);
elseif imod == 2 then aa = aa ~ (aa << 2);
elseif imod == 3 then aa = aa ~ (aa >> 16);
end
aa = ( mm[(i+128)%256] + aa ) % cap;
y = ( mm[(x>>2) % 256] + aa + bb ) % cap;
mm[i] = y;
bb = ( mm[(y>>10)%256] + x ) % cap;
randRsl[i] = bb;
end
 
randCnt = 0; -- Prepare to use the first set of results.
 
end
 
function mix(a)
a[0] = ( a[0] ~ ( a[1] << 11 ) ) % cap; a[3] = ( a[3] + a[0] ) % cap; a[1] = ( a[1] + a[2] ) % cap;
a[1] = ( a[1] ~ ( a[2] >> 2 ) ) % cap; a[4] = ( a[4] + a[1] ) % cap; a[2] = ( a[2] + a[3] ) % cap;
a[2] = ( a[2] ~ ( a[3] << 8 ) ) % cap; a[5] = ( a[5] + a[2] ) % cap; a[3] = ( a[3] + a[4] ) % cap;
a[3] = ( a[3] ~ ( a[4] >> 16 ) ) % cap; a[6] = ( a[6] + a[3] ) % cap; a[4] = ( a[4] + a[5] ) % cap;
a[4] = ( a[4] ~ ( a[5] << 10 ) ) % cap; a[7] = ( a[7] + a[4] ) % cap; a[5] = ( a[5] + a[6] ) % cap;
a[5] = ( a[5] ~ ( a[6] >> 4 ) ) % cap; a[0] = ( a[0] + a[5] ) % cap; a[6] = ( a[6] + a[7] ) % cap;
a[6] = ( a[6] ~ ( a[7] << 8 ) ) % cap; a[1] = ( a[1] + a[6] ) % cap; a[7] = ( a[7] + a[0] ) % cap;
a[7] = ( a[7] ~ ( a[0] >> 9 ) ) % cap; a[2] = ( a[2] + a[7] ) % cap; a[0] = ( a[0] + a[1] ) % cap;
end
 
function randInit(flag)
 
-- The golden ratio in 32 bit
-- math.floor((((math.sqrt(5)+1)/2)%1)*2^32) == 2654435769 == 0x9e3779b9
local a = { [0] = 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, };
 
aa,bb,cc = 0,0,0;
 
for i = 1,4 do mix(a) end -- Scramble it.
 
for i = 0,255,8 do -- Fill in mm[] with messy stuff.
if flag then -- Use all the information in the seed.
for j = 0,7 do
a[j] = ( a[j] + randRsl[i+j] ) % cap;
end
end
mix(a);
for j = 0,7 do
mm[i+j] = a[j];
end
end
 
if flag then
-- Do a second pass to make all of the seed affect all of mm.
for i = 0,255,8 do
for j = 0,7 do
a[j] = ( a[j] + mm[i+j] ) % cap;
end
mix(a);
for j = 0,7 do
mm[i+j] = a[j];
end
end
end
 
isaac(); -- Fill in the first set of results.
randCnt = 0; -- Prepare to use the first set of results.
 
end
 
-- Seed ISAAC with a given string.
-- The string can be any size. The first 256 values will be used.
function seedIsaac(seed,flag)
local seedLength = #seed;
for i = 0,255 do mm[i] = 0; end
for i = 0,255 do randRsl[i] = seed:byte(i+1,i+1) or 0; end
randInit(flag);
end
 
-- Get a random 32-bit value 0..MAXINT
function getRandom32Bit()
local result = randRsl[randCnt];
randCnt = randCnt + 1;
if randCnt > 255 then
isaac();
randCnt = 0;
end
return result;
end
 
-- Get a random character in printable ASCII range
function getRandomChar()
return getRandom32Bit() % 95 + 32;
end
 
-- Convert an ASCII string to a hexadecimal string.
function ascii2hex(source)
local ss = "";
for i = 1,#source do
ss = ss..string.format("%02X",source:byte(i,i));
end
return ss
end
 
-- XOR encrypt on random stream.
function vernam(msg)
local msgLength = #msg;
local destination = {};
for i = 1, msgLength do
destination[i] = string.char(getRandomChar() ~ msg:byte(i,i));
end
return table.concat(destination);
end
 
-- Caesar-shift a character <shift> places: Generalized Vigenere
function caesar(m, ch, shift, modulo, start)
local n
local si = 1
if m == DECRYPT then shift = shift*-1 ; end
n = (ch - start) + shift;
if n < 0 then si,n = -1,n*-1 ; end
n = ( n % modulo ) * si;
if n < 0 then n = n + modulo ; end
return start + n;
end
 
-- Vigenere mod 95 encryption & decryption.
function vigenere(msg,m)
local msgLength = #msg;
local destination = {};
for i = 1,msgLength do
destination[i] = string.char( caesar(m, msg:byte(i,i), getRandomChar(), 95, 32) );
end
return table.concat(destination);
end
 
function main()
local msg = "a Top Secret secret";
local key = "this is my secret key";
local xorCipherText, modCipherText, xorPlainText, modPlainText;
 
-- (1) Seed ISAAC with the key
seedIsaac(key, true);
-- (2) Encryption
-- (a) XOR (Vernam)
xorCipherText = vernam(msg);
-- (b) MOD (Vigenere)
modCipherText = vigenere(msg, ENCRYPT);
-- (3) Decryption
seedIsaac(key, true);
-- (a) XOR (Vernam)
xorPlainText = vernam(xorCipherText);
-- (b) MOD (Vigenere)
modPlainText = vigenere(modCipherText, DECRYPT);
-- Program output
print("Message: " .. msg);
print("Key : " .. key);
print("XOR : " .. ascii2hex(xorCipherText));
print("XOR dcr: " .. xorPlainText);
print("MOD : " .. ascii2hex(modCipherText));
print("MOD dcr: " .. modPlainText);
 
end
 
main()
</syntaxhighlight>
{{out}}
<pre>
Message: a Top Secret secret
Key : this is my secret key
XOR : 1c0636190b1260233b35125f1e1d0e2f4c5422
MOD : 734270227d36772a783b4f2a5f206266236978
XOR dcr: a Top Secret secret
MOD dcr: a Top Secret secret
</pre>
 
Line 2,759 ⟶ 3,256:
{{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 2,775 ⟶ 3,272:
TMode = (iEncrypt, iDecrypt);
TString = ARRAY [0 .. MaxStrLength - 1] OF CHAR;
THexString = ARRAY [0 .. 2 * MaxStrLength - 1] OF CHAR;
TCardIndexedFrom0To7 = ARRAY [0 .. 7] OF CARDINAL;
 
Line 2,785 ⟶ 3,283:
XorPlainText: TString = '';
ModPlainText: TString = '';
HexText: THexString;
Mode: TMode = iEncrypt;
HexText: TString;
 
(* ISAAC globals *)
(* external results *)
RandRsl: ARRAY [0 .. 256255] OF CARDINAL;
RandCnt: CARDINAL;
 
(* internal state *)
MM: ARRAY [0 .. 256255] OF CARDINAL;
AA: CARDINAL = 0;
BB: CARDINAL = 0;
Line 2,881 ⟶ 3,378:
PROCEDURE SeedIsaac(Seed: ARRAY OF CHAR; Flag: BOOLEAN);
VAR
I, MSeedLength: CARDINAL;
BEGIN
FOR I := 0 TO 255 DO
MM[I] := 0;
END;
MSeedLength := Length(Seed);
FOR I := 0 TO 255 DO
(* In case seed has less than 256 elements *)
IF I > MSeedLength THEN
RandRsl[I] := 0
ELSE
Line 2,942 ⟶ 3,439:
OrdMsgI: SHORTCARD;
BEGIN
Assign(''Msg, Destination);
FOR I := 0 TO Length(Msg) - 1 DO
OrdMsgI := ORD(Msg[I]);
Append(Destination[I] := CHR(GetRandomChar() BXOR OrdMsgI), Destination);
END;
END Vernam;
Line 2,978 ⟶ 3,475:
I: CARDINAL;
BEGIN
Assign(''Msg, Destination);
FOR I := 0 TO Length(Msg) - 1 DO
Append(Destination[I] := Caesar(M, Msg[I], GetRandomChar(), 95, ' '), Destination);
END;
END Vigenere;
Line 2,988 ⟶ 3,485:
SeedIsaac(Key, TRUE);
(* (2) Encryption *)
Mode := iEncrypt;
(* (a) XOR (Vernam) *)
Vernam(Msg, XorCipherText);
(* (b) MOD (Vigenere) *)
Vigenere(Msg, ModeiEncrypt, ModCipherText);
(* (3) Decryption *)
Mode := iDecrypt;
SeedIsaac(Key, TRUE);
(* (a) XOR (Vernam) *)
Vernam(XorCipherText, XorPlainText);
(* (b) MOD (Vigenere) *)
Vigenere(ModCipherText, ModeiDecrypt, ModPlainText);
(* program output *)
WriteString('Message: '); WriteString(Msg); WriteLn;
Line 3,010 ⟶ 3,505:
WriteString('MOD dcr: '); WriteString(ModPlainText); WriteLn;
END RosettaIsaac.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,020 ⟶ 3,515:
MOD dcr: a Top Secret secret
</pre>
 
=={{header|Nim}}==
{{trans|Pascal}}
We choose the translate the Pascal version as the it’s easier to translate to Nim from Pascal rather than from C.
This is not an exact translation: the more important difference is the use of a global state record rather than a list of global variables. This global state is transmitted to each procedure. This way, it is possible to run several PRNG concurrently.
 
We also replaced the eight variables "a" to "h" with an array. This allows to simplify the code at several places by using a loop. And we changed the "mod" to "and", even if the compiler will likely optimize the modulo when the second operand is a power of two.
 
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 a procedure whose performance is critical, expanding the code rather than calling a procedure is not very useful.
 
<syntaxhighlight lang="nim">import strutils
 
type
 
IMode = enum iEncrypt, iDecrypt
 
State = object
# Internal.
mm: array[256, uint32]
aa, bb, cc: uint32
# External.
randrsl: array[256, uint32]
randcnt: uint32
 
 
proc isaac(s: var State) =
 
inc s.cc # "cc" just gets incremented once per 256 results
s.bb += s.cc # then combined with "bb".
 
for i in 0u32..255:
let x = s.mm[i]
case range[0..3](i and 3)
of 0: s.aa = s.aa xor s.aa shl 13
of 1: s.aa = s.aa xor s.aa shr 6
of 2: s.aa = s.aa xor s.aa shl 2
of 3: s.aa = s.aa xor s.aa shr 16
s.aa += s.mm[(i + 128) and 255]
let y = s.mm[(x shr 2) and 255] + s.aa + s.bb
s.mm[i] = y
s.bb = s.mm[(y shr 10) and 255] + x
s.randrsl[i] = s.bb
 
s.randcnt = 0
 
 
proc mix(a: var array[8, uint32]) =
a[0] = a[0] xor a[1] shl 11; a[3] += a[0]; a[1] += a[2]
a[1] = a[1] xor a[2] shr 2; a[4] += a[1]; a[2] += a[3]
a[2] = a[2] xor a[3] shl 8; a[5] += a[2]; a[3] += a[4]
a[3] = a[3] xor a[4] shr 16; a[6] += a[3]; a[4] += a[5]
a[4] = a[4] xor a[5] shl 10; a[7] += a[4]; a[5] += a[6]
a[5] = a[5] xor a[6] shr 4; a[0] += a[5]; a[6] += a[7]
a[6] = a[6] xor a[7] shl 8; a[1] += a[6]; a[7] += a[0]
a[7] = a[7] xor a[0] shr 9; a[2] += a[7]; a[0] += a[1]
 
 
proc iRandInit(s: var State; flag: bool) =
 
s.aa = 0; s.bb = 0; s.cc = 0
var a: array[8, uint32]
for item in a.mitems: item = 0x9e3779b9u32 # The golden ratio.
 
for i in 0..3: # Scramble it.
a.mix()
 
var i = 0u32
while true: # Fill in "mm" with messy stuff.
if flag:
# Use all the information in the seed.
for n in 0u32..7: a[n] += s.randrsl[n + i]
a.mix()
for n in 0u32..7: s.mm[n + i] = a[n]
inc i, 8
if i > 255: break
 
if flag:
# Do a second pass to make all of the seed affect all of "mm".
i = 0
while true:
for n in 0u32..7: a[n] += s.mm[n + i]
a.mix()
for n in 0u32..7: s.mm[n + i] = a[n]
inc i, 8
if i > 255: break
 
s.isaac() # Fill in the first set of results.
s.randcnt = 0 # Prepare to use the first set of results.
 
 
proc iSeed(s: var State; seed: string; flag: bool) =
## Seed ISAAC with a given string.
## The string can be any size. The first 256 values will be used.
s.mm.reset()
let m = seed.high
for i in 0..255:
s.randrsl[i] = if i > m: 0 else: ord(seed[i])
# Initialize ISAAC with seed.
s.iRandInit(flag)
 
 
proc iRandom(s: var State): uint32 =
## Get a random 32-bit value 0..int32.high.
result = s.randrsl[s.randcnt]
inc s.randcnt
if s.randcnt > 255:
s.isaac()
s.randcnt = 0
 
 
proc iRandA(s: var State): byte =
## Get a random character in printable ASCII range.
result = byte(s.iRandom() mod 95 + 32)
 
 
proc vernam(s: var State; msg: string): string =
## XOR encrypt on random stream. Output: ASCII string.
result.setLen(msg.len)
for i, c in msg:
result[i] = chr(s.irandA() xor byte(c))
 
 
template letterNum(letter, start: char): int =
ord(letter) - ord(start)
 
 
proc caesar(m: IMode; ch: char; shift, modulo: int; start: char): char =
let shift = if m == iEncrypt: shift else: -shift
var n = letterNum(ch, start) + shift
n = n mod modulo
if n < 0: inc n, modulo
result = chr(ord(start) + n)
 
 
proc vigenere(s: var State; msg: string; m: IMode): string =
## Vigenere MOD 95 encryption & decryption. Output: ASCII string.
result.setLen(msg.len)
for i, c in msg:
result[i] = caesar(m, c, s.iRanda().int, 95, ' ')
 
 
let
msg = "a Top Secret secret"
key = "this is my secret key"
 
var state: State
 
# 1) seed ISAAC with the key
state.iSeed(key, true)
# 2) Encryption
# a) XOR (Vernam)
let xctx = state.vernam(msg) # XOR ciphertext.
# b) MOD (Vigenere)
let mctx = state.vigenere(msg, iEncrypt) # MOD ciphertext.
# 3) Decryption
state.iSeed(key, true)
# a) XOR (Vernam)
let xptx = state.vernam(xctx) # XOR decryption (plaintext).
# b) MOD (Vigenere)
let mptx = state.vigenere(mctx, iDecrypt) # MOD decryption (plaintext).
# Program output
echo "Message: ", msg
echo " Key: ", key
echo " XOR: ", xctx.tohex
echo " MOD: ", mctx.toHex
echo "XOR dcr: ", xptx
echo "MOD dcr: ", mptx</syntaxhighlight>
 
{{out}}
<pre>Message: a Top Secret secret
Key: this is my secret key
XOR: 1C0636190B1260233B35125F1E1D0E2F4C5422
MOD: 734270227D36772A783B4F2A5F206266236978
XOR dcr: a Top Secret secret
MOD dcr: a Top Secret secret</pre>
 
=={{header|Pascal}}==
Free Pascal. A fully functional and complete reference solution of the task.
<syntaxhighlight lang="pascal">
<lang Pascal>
PROGRAM RosettaIsaac;
USES
Line 3,245 ⟶ 3,915:
Writeln('MOD dcr: ', mptx);
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,263 ⟶ 3,933:
as well as additional convenience functions.
 
<langsyntaxhighlight lang="perl">use warnings;
use strict;
use Math::Random::ISAAC;
Line 3,293 ⟶ 3,963:
map { ord($_) ^ shift(@iranda) } # xor it with rand char
split "", $msg; # Take each character
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,305 ⟶ 3,975:
{{trans|Pascal}}
We need the r32() function to convert our common sense maths into the needed unsigned_and_throw_away_any_high_bits maths.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>-- demo\rosetta\ISAAC_Cipher.exw
<span style="color: #000080;font-style:italic;">--
 
-- demo\rosetta\ISAAC_Cipher.exw
sequence randrsl = repeat(0,256)
--</span>
integer randcnt
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">randrsl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">256</span><span style="color: #0000FF;">)</span>
sequence mm
<span style="color: #004080;">integer</span> <span style="color: #000000;">randcnt</span>
atom aa,bb,cc
<span style="color: #004080;">sequence</span> <span style="color: #000000;">mm</span>
function r32(object a)
<span style="color: #004080;">atom</span> <span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cc</span>
if sequence(a) then
for i=1 to length(a) do
<span style="color: #008080;">function</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
a[i] = r32(a[i])
<span style="color: #008080;">if</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
return a
<span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if a<0 then a+=#100000000 end if
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
return remainder(a,#100000000)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">if</span> <span style="color: #000000;">a</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">+=</span><span style="color: #000000;">#100000000</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#100000000</span><span style="color: #0000FF;">)</span>
function shl(atom word, integer bits)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return r32(word*power(2,bits))
end function
 
function shr(atom v, integer bits)
return floor(v/power(2,bits))
end function
 
procedure Isaac()
cc += 1; -- cc just gets incremented once per 256 results
bb += cc; -- then combined with bb
for i=1 to 256 do
atom x = mm[i]
switch mod(i-1,4) do
case 0: aa := xor_bits(aa,shl(aa,13))
case 1: aa := xor_bits(aa,shr(aa, 6))
case 2: aa := xor_bits(aa,shl(aa, 2))
case 3: aa := xor_bits(aa,shr(aa,16))
end switch
aa = r32(mm[xor_bits(i-1,#80)+1]+aa)
atom y := mm[and_bits(shr(x,2),#FF)+1]+aa+bb
mm[i] := y;
bb := r32(mm[and_bits(shr(y,10),#FF)+1] + x)
randrsl[i]:= bb;
end for
randcnt = 1
end procedure
<span style="color: #008080;">function</span> <span style="color: #000000;">shl</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">bits</span><span style="color: #0000FF;">)</span>
function mix(sequence a8)
<span style="color: #008080;">return</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">))</span>
atom {a,b,c,d,e,f,g,h} = a8
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
a = xor_bits(a,shl(b,11)); {d,b} = r32({d+a,b+c});
b = xor_bits(b,shr(c, 2)); {e,c} = r32({e+b,c+d});
c = xor_bits(c,shl(d, 8)); {f,d} = r32({f+c,d+e});
d = xor_bits(d,shr(e,16)); {g,e} = r32({g+d,e+f});
e = xor_bits(e,shl(f,10)); {h,f} = r32({h+e,f+g});
f = xor_bits(f,shr(g, 4)); {a,g} = r32({a+f,g+h});
g = xor_bits(g,shl(h, 8)); {b,h} = r32({b+g,h+a});
h = xor_bits(h,shr(a, 9)); {c,a} = r32({c+h,a+b});
a8 = {a,b,c,d,e,f,g,h}
return a8
end function
 
procedure iRandInit()
{aa,bb,cc} = {0,0,0}
sequence a8 = repeat(#9e3779b9,8) -- the golden ratio
for i=1 to 4 do -- scramble it
a8 = mix(a8)
end for
for i=1 to 255 by 8 do
a8 = mix(sq_add(a8,randrsl[i..i+7]))
mm[i..i+7] = a8
end for
for i=1 to 255 by 8 do
a8 = mix(r32(sq_add(a8,mm[i..i+7])))
mm[i..i+7] = a8
end for
Isaac() -- fill in the first set of results
end procedure
<span style="color: #008080;">function</span> <span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">bits</span><span style="color: #0000FF;">)</span>
procedure iSeed(string seed)
<span style="color: #008080;">return</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">))</span>
mm = repeat(0,256)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
randrsl = repeat(0,256)
randrsl[1..min(length(seed),256)] = seed
iRandInit()
end procedure
<span style="color: #008080;">procedure</span> <span style="color: #000000;">Isaac</span><span style="color: #0000FF;">()</span>
function randch()
<span style="color: #000000;">cc</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000080;font-style:italic;">-- cc just gets incremented once per 256 results </span>
atom res = mod(randrsl[randcnt],95)+32
<span style="color: #000000;">bb</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">cc</span><span style="color: #0000FF;">;</span> <span style="color: #000080;font-style:italic;">-- then combined with bb </span>
randcnt += 1
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">256</span> <span style="color: #008080;">do</span>
if randcnt>256 then
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
Isaac()
<span style="color: #008080;">switch</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end if
<span style="color: #008080;">case</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">aa</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shl</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">))</span>
return res
<span style="color: #008080;">case</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">aa</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">))</span>
end function
<span style="color: #008080;">case</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">aa</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shl</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">aa</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #000000;">aa</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mm</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#80</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">mm</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">#FF</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">+</span><span style="color: #000000;">bb</span>
<span style="color: #000000;">mm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">;</span>
<span style="color: #000000;">bb</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mm</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">#FF</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">randrsl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]:=</span> <span style="color: #000000;">bb</span><span style="color: #0000FF;">;</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">randcnt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mix</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a8</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a8</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shl</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">));</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">({</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">c</span><span style="color: #0000FF;">});</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">));</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">({</span><span style="color: #000000;">e</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">+</span><span style="color: #000000;">d</span><span style="color: #0000FF;">});</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shl</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">));</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">({</span><span style="color: #000000;">f</span><span style="color: #0000FF;">+</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">e</span><span style="color: #0000FF;">});</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">));</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">({</span><span style="color: #000000;">g</span><span style="color: #0000FF;">+</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">+</span><span style="color: #000000;">f</span><span style="color: #0000FF;">});</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shl</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">));</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">({</span><span style="color: #000000;">h</span><span style="color: #0000FF;">+</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">+</span><span style="color: #000000;">g</span><span style="color: #0000FF;">});</span>
<span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">));</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">({</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">+</span><span style="color: #000000;">h</span><span style="color: #0000FF;">});</span>
<span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shl</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">));</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">({</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">+</span><span style="color: #000000;">a</span><span style="color: #0000FF;">});</span>
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">shr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">));</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r32</span><span style="color: #0000FF;">({</span><span style="color: #000000;">c</span><span style="color: #0000FF;">+</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">});</span>
<span style="color: #000000;">a8</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a8</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">iRandInit</span><span style="color: #0000FF;">()</span>
function Vernam(string msg)
<span style="color: #0000FF;">{</span><span style="color: #000000;">aa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cc</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
string res = ""
<span style="color: #004080;">sequence</span> <span style="color: #000000;">a8</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">#9e3779b9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- the golden ratio</span>
for i=1 to length(msg) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">4</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- scramble it </span>
res &= xor_bits(msg[i],randch())
<span style="color: #000000;">a8</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mix</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a8</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return res
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">255</span> <span style="color: #008080;">by</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
end function
<span style="color: #000000;">a8</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mix</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">randrsl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">7</span><span style="color: #0000FF;">]))</span>
 
<span style="color: #000000;">mm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">7</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a8</span>
function Caesar(integer ch, shift)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return ' '+mod(ch-' '+shift,95)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">255</span> <span style="color: #008080;">by</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
end function
<span style="color: #000000;">a8</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mix</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r32</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">7</span><span style="color: #0000FF;">])))</span>
<span style="color: #000000;">mm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">7</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a8</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">Isaac</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- fill in the first set of results </span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">iSeed</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">seed</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mm</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">256</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">randrsl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">256</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">randrsl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">seed</span><span style="color: #0000FF;">),</span><span style="color: #000000;">256</span><span style="color: #0000FF;">)]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">seed</span>
<span style="color: #000000;">iRandInit</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">randch</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">randrsl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">randcnt</span><span style="color: #0000FF;">],</span><span style="color: #000000;">95</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">32</span>
<span style="color: #000000;">randcnt</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">randcnt</span><span style="color: #0000FF;">></span><span style="color: #000000;">256</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">Isaac</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Vernam</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">randch</span><span style="color: #0000FF;">())</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Caesar</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">shift</span><span style="color: #0000FF;">)</span>
enum ENCRYPT = +1,
<span style="color: #008080;">return</span> <span style="color: #008000;">' '</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">-</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">+</span><span style="color: #000000;">shift</span><span style="color: #0000FF;">,</span><span style="color: #000000;">95</span><span style="color: #0000FF;">)</span>
DECRYPT = -1
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function Vigenere(string msg, integer mode)
<span style="color: #008080;">enum</span> <span style="color: #000000;">ENCRYPT</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
string res = ""
<span style="color: #000000;">DECRYPT</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
for i=1 to length(msg) do
res &= Caesar(msg[i],randch()*mode)
<span style="color: #008080;">function</span> <span style="color: #000000;">Vigenere</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">mode</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
return res
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end function
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">Caesar</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">randch</span><span style="color: #0000FF;">()*</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #004080;">string</span> <span style="color: #000000;">msg</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"a Top Secret secret"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">key</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"this is my secret key"</span>
<span style="color: #000000;">iSeed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
constant string msg = "a Top Secret secret",
<span style="color: #004080;">string</span> <span style="color: #000000;">xctx</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">Vernam</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">),</span>
key = "this is my secret key"
<span style="color: #000000;">mctx</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">Vigenere</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ENCRYPT</span><span style="color: #0000FF;">)</span>
 
iSeed(key)
<span style="color: #000000;">iSeed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
string xctx := Vernam(msg),
<span style="color: #004080;">string</span> <span style="color: #000000;">xptx</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">Vernam</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xctx</span><span style="color: #0000FF;">),</span>
mctx := Vigenere(msg,ENCRYPT)
<span style="color: #000000;">mptx</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">Vigenere</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mctx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">DECRYPT</span><span style="color: #0000FF;">)</span>
 
iSeed(key)
<span style="color: #008080;">function</span> <span style="color: #000000;">ascii2hex</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
string xptx := Vernam(xctx),
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
mptx := Vigenere(mctx,DECRYPT)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
 
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%02x"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
function ascii2hex(string s)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
string res = ""
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
for i=1 to length(s) do
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
res &= sprintf("%02x",s[i])
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Message: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">})</span>
return res
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Key : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">key</span><span style="color: #0000FF;">})</span>
end function
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XOR : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ascii2hex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xctx</span><span style="color: #0000FF;">)})</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MOD : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ascii2hex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mctx</span><span style="color: #0000FF;">)})</span>
printf(1,"Message: %s\n",{msg})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XOR dcr: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">xptx</span><span style="color: #0000FF;">})</span>
printf(1,"Key : %s\n",{key})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MOD dcr: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mptx</span><span style="color: #0000FF;">})</span>
printf(1,"XOR : %s\n",{ascii2hex(xctx)})
printf(1,"MOD : %s\n",{ascii2hex(mctx)})
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
printf(1,"XOR dcr: %s\n",{xptx})
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
printf(1,"MOD dcr: %s\n",{mptx})</lang>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,458 ⟶ 4,135:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de add32 @
(mod32 (pass +)) )
 
Line 3,562 ⟶ 4,239:
95 ) ) ) ) ) ) ) ) ) )
 
(bye)</langsyntaxhighlight>
 
=={{header|Python}}==
Line 3,572 ⟶ 4,249:
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 3,768 ⟶ 4,445:
print('MOD :', hexify(caesar_encoded))
print('MOD dcr:', caesar_decoded)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 3,780 ⟶ 4,457:
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,034 ⟶ 4,711:
(randctx-b C) => #x902c0691
(randctx-c C) => 10))
}</langsyntaxhighlight>
 
{{out}}
Line 4,055 ⟶ 4,732:
=={{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,168 ⟶ 4,845:
say "MOD : ", $cctx2hex;
say "MOD dcr : ", $cptx;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,181 ⟶ 4,858:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 24.07.2014 Walter Pachl translated from Pascal
* extend with decryption (following Pascal)
Line 4,388 ⟶ 5,065:
If arg(3)<>'' Then
res=res+arg(3)
return res//4294967296</langsyntaxhighlight>
{{out}}
<pre>Message: a Top Secret secret
Line 4,399 ⟶ 5,076:
===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 4,637 ⟶ 5,314:
parse Arg fid
Parse Var fid fn '.' ft
Return fn</langsyntaxhighlight>
{{out}}
<pre>Please enter a key
Line 4,651 ⟶ 5,328:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
//! includes the XOR version of the encryption scheme
 
Line 4,822 ⟶ 5,499:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,833 ⟶ 5,510:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('Math::Random::ISAAC')
 
func xor_isaac(key, msg) {
Line 4,852 ⟶ 5,529:
say "Key : #{key}"
say "XOR : #{enc}"
say "XOR dcr: #{pack('H*', dec)}"</langsyntaxhighlight>
{{out}}
<pre>
Line 4,864 ⟶ 5,541:
{{works with|Tcl|8.6}}
{{trans|Go}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create ISAAC {
Line 4,973 ⟶ 5,650:
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 4,990 ⟶ 5,667:
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./traititerate" for Stepped
import "./dynamic" for Enum
import "./fmt" for Fmt
 
/* external results */
Line 5,172 ⟶ 5,849:
System.print("XOR dcr : %(vptx)")
System.print("MOD : %(toHexByteString.call(cctx))")
System.print("MOD dcr : %(cptx)")</langsyntaxhighlight>
 
{{out}}
9,476

edits