-
Notifications
You must be signed in to change notification settings - Fork 0
/
ciphersaber-algorithm.html
65 lines (65 loc) · 4.34 KB
/
ciphersaber-algorithm.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>CipherSaber Algorithm</title>
<meta name="generator" content="TextMate 2.0.6" />
<style>
html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}a{background-color:transparent}a:active,a:hover{outline:0}strong{font-weight:700}pre{overflow:auto}code,pre{font-family:monospace,monospace;font-size:1em}.container{position:relative;width:100%;max-width:960px;margin:40px auto;padding:0 20px;box-sizing:border-box}@media (min-width:400px){.container{width:85%;padding:0}}@media (min-width:550px){.container{width:80%}}html{font-size:62.5%}body{font-size:2.0em;line-height:1.6;font-weight:400;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;color:#222}h2,h3{margin-top:0;margin-bottom:2rem;font-weight:300}h2{font-size:3.6rem;line-height:1.25;letter-spacing:-.1rem}h3{font-size:3rem;line-height:1.3;letter-spacing:-.1rem}@media (min-width:550px){h2{font-size:4.2rem}h3{font-size:3.6rem}}p{margin-top:0}a{color:#1eaedb}a:hover{color:#0fa0ce}ul{list-style:circle inside}ol{list-style:decimal inside}ol,ul{padding-left:0;margin-top:0}li{margin-bottom:1rem}code{padding:.2rem .5rem;margin:0 .2rem;font-size:90%;white-space:nowrap;background:#f1f1f1;border:1px solid #e1e1e1;border-radius:4px}pre>code{display:block;padding:1rem 1.5rem;white-space:pre}ol,p,pre,ul{margin-bottom:2.5rem}.container:after{content:"";display:table;clear:both}
</style>
</head>
<body>
<div class="container">
<h2>
CipherSaber Algorithm
</h2>
<h3>
Ingredients
</h3>
<p>
You will need:
</p>
<ul>
<li>A secret key (up to 246 bytes)</li>
<li>Binary data to be encrypted or decrypted (as many bytes as you like)</li>
<li>A number <code>num_rounds</code> which should equal 1 for the original CipherSaber algorithm, or 20 for <a href="http://ciphersaber.gurus.org/faq.html#cs2">CipherSaber-2</a> (recommended). Or you can choose some other value if you like.</li>
</ul>
<h3>
Method
</h3>
<ol>
<li>Create two 256-byte arrays called <code>S</code> and <code>S2</code>.</li>
<li>Initialize <code>S</code> by filling it with all the values from 0 to 255 (i.e., <code>S[0]=0</code>, <code>S[1]=1</code>, <code>S[2]=2</code>, and so on.)</li>
<li> Copy the secret key to the bytes at the start of <code>S2</code>.</li>
<li><strong>If you’re encrypting</strong>, you should then generate ten bytes of random data (called the <em>initialization vector</em>). Write a copy of these ten bytes to your output file. <strong>If you’re decrypting</strong>, read these ten bytes back in from the start of the binary data.</li>
<li>Append the initialization vector to <code>S2</code>, directly after the secret key. Then fill up the remainder of <code>S2</code> by repeating the secret key and initialization vector until you have set all 256 positions in <code>S2</code>.</li>
<li>Now we have to randomize the contents of <code>S</code> based on the contents of <code>S2</code>. This is done by swapping bytes in <code>S</code> according to the following method, using the value of <code>num_rounds</code> you chose earlier:
<pre><code>j = 0
for n in (1 .. num_rounds)
for i in (0 .. 255)
j = (j + S[i] + S2[i]) mod 256
swap S[i], S[j]
end
end</code></pre>
<p>
You can now discard <code>S2</code>; it won’t be used any more.
</p>
</li>
<li>Use <code>S</code> to generate a pseudo-random stream of bytes to combine with the input data (using exclusive-or (XOR) operations). Since this is a symmetric cipher, the procedure is exactly the same for encryption and decryption:
<pre><code>i = 0; j = 0
for each byte b of binary data:
i = (i + 1) mod 256
j = (j + S[i]) mod 256
swap S[i], S[j]
k = (S[i] + S[j]) mod 256
output (b xor S[k])
end</code></pre>
</li>
</ol>
<p>
Well I hope that isn’t <em>too</em> complicated. Frankly it’s about at the limit of what I’d be able to reproduce from memory, and I’m not sure I’d be able to get it right first time either. But do have a go at writing your own. You’ll probably want somewhere to test your code, and for that purpose I’ve set up an <a href="https://ruletheweb.co.uk/cgi-bin/saber.cgi">online encryption/decryption tool</a> that you’re welcome to use.
</p>
<address> <a href="https://ruletheweb.co.uk/blog/2014/04/ciphersaber/">Philip Ronan</a> </address>
</div>
</body>
</html>