Jump to content

Data Encryption Standard: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 14:
=={{header|C}}==
{{trans|D}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 514:
driver(keys[2], message3, len);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Key : 133457799BBCDFF1
Line 532:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.IO;
using System.Security.Cryptography;
Line 602:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Encoded: 0000000000000000A913F4CB0BD30F97
Line 609:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <array>
#include <bitset>
Line 1,030:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Key : 133457799BBCDFF1
Line 1,049:
=={{header|D}}==
{{trans|kotlin}}
<langsyntaxhighlight lang="d">import std.array;
import std.bitmanip;
import std.stdio;
Line 1,424:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Key : 133457799BBCDFF1
Line 1,443:
=={{header|F Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
open System.Security.Cryptography
open System.IO
Line 1,501:
printfn "Decoded: %s" (ByteArrayToString decBytes)
 
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>Encoded: 0000000000000000A913F4CB0BD30F97
Line 1,507:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 20-01-2019
' compile with: fbc -s console
 
Line 1,784:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> key 133457799BBCDFF1
Line 1,803:
=={{header|Go}}==
'''Library solution:'''
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,828:
c.Encrypt(dst, src)
fmt.Printf("%x\n", dst)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,836:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
Line 1,877:
printHexBytes(decBytes, "Decoded");
}
}</langsyntaxhighlight>
{{out}}
<pre>Encoded: 0000000000000000a913f4cb0bd30f97
Line 1,884:
=={{header|Julia}}==
===Using the MbedTLS library===
<langsyntaxhighlight lang="julia">using MbedTLS
 
const testdata = [
Line 1,907:
println("Decoded : $(bytes2hex(decoded))\n")
end
</langsyntaxhighlight>{{out}}
<pre>
Key : 133457799bbcdff1
Line 1,926:
===Base Julia only===
{{trans|Phix}}
<langsyntaxhighlight lang="julia">const PC1 = [57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
Line 2,089:
println("Decoded : $(bytes2hex(decoded))\n")
end
</syntaxhighlight>
</lang>
Output: same as MbedTLS library version above.
 
Line 2,095:
===Version 1 (using library functions)===
Presumably, one can use library functions to demonstrate DES as it would be very tedious to implement it from scratch:
<langsyntaxhighlight lang="scala">// version 1.1.3
 
import javax.crypto.Cipher
Line 2,132:
val decBytes = decCipher.doFinal(encBytes)
decBytes.printHexBytes("Decoded")
}</langsyntaxhighlight>
 
{{out}}
Line 2,142:
===Version 2 (from scratch)===
It wasn't as tedious as I expected due to the admirably clear article linked to above:
<langsyntaxhighlight lang="scala">// version 1.1.3
 
import java.util.BitSet
Line 2,459:
println()
}
}</langsyntaxhighlight>
{{out}}
<pre>Key : 133457799BBCDFF1
Line 2,477:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE DataEncryptionStandard;
FROM SYSTEM IMPORT BYTE,ADR;
FROM DES IMPORT DES,Key1,Create,Destroy,EncryptECB,DecryptECB;
Line 2,530:
Destroy(cipher);
ReadChar
END DataEncryptionStandard.</langsyntaxhighlight>
{{out}}
<pre>plain: 8787878787878787
Line 2,543:
Compared to the D (and Kotlin versions), we avoided to use an array to compute left and right part when executing the rounds. We simply switched left and right part at each round. This is the main difference with these two versions.
 
<langsyntaxhighlight Nimlang="nim">import bitops, sequtils, strutils
 
 
Line 2,871:
let decoded = decrypt(Keys[i], encoded)
echo "Decoded: ", decoded.toHex()
echo()</langsyntaxhighlight>
 
{{out}}
Line 2,890:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl"># 20200723 added Perl programming solution
 
use strict;
Line 2,902:
print "Encoded : ", unpack("H*", $ciphertext), "\n";
print "Decoded : ", unpack("H*", $cipher->decrypt($ciphertext)), "\n";
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,915:
easier to debug/verify, probably sidestep a few fiddly endian issues, and certainly
simplify bit-wise permutations.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Data_Encryption_Standard.exw</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">PC1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">57</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">33</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">17</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span>
Line 3,107:
<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;">"Decoded : %s%s\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">dectxt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">derror</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,127:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(setq
*PC1
(57 49 41 33 25 17 9
Line 3,295:
16
(hex
(bin (mapcar '((I) (get R I)) *IP_INV)))) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 3,310:
implemented like in the article linked in description. <br>
really good article btw
<langsyntaxhighlight Pythonlang="python">#!/usr/bin/python
#!/usr/bin/python
 
Line 3,555:
prove(k2, m2)
 
</syntaxhighlight>
</lang>
{{Out}}
Note: This is just the algorithm for single 64-bit blocks. No padding or block chipher operation mode
Line 3,572:
Thanks to SqrtNegInf for pointing out that \r\n is a single grapheme. ([https://docs.raku.org/type/Str#routine_chomp link 1], [https://docs.raku.org/language/newline link 2])
{{trans|Phix}}
<syntaxhighlight lang="raku" perl6line>20220222 Updated Raku programming solution
 
my \PC1 = <
Line 3,721:
say h2B des "0E329232EA6D0D73", h2B("C0999FDDE378D7ED727DA00BCA5A84EE47F269A4D6438190D9D52F78F535849980A2E7453703513E"), True;
say h2B des "0E329232EA6D0D73", h2B("C0999FDDE378D7ED727DA00BCA5A84EE47F269A4D6438190D9D52F78F53584997F922CCB5B068D99"), True;
say h2u des "0E329232EA6D0D73", h2B("C040FB6A6E72D7C36D60CA9B9A35EB38D3194468AD808103C28E33AEF0B268D0E0366C160B028DDACF340003DCA8969343EBBD289DB94774"), True; </langsyntaxhighlight>
{{out}}
<pre>Encryption examples:
Line 3,741:
Implementation of the algorithm described in the cited article.
<br>Decryption is now supported as well
<langsyntaxhighlight lang="rexx">/* REXX for the sake of some platforms such as good old iron */
Parse Upper Arg action
Select
Line 4,030:
Return r
 
debug: /* Say arg(1) */ Return</langsyntaxhighlight>
{{out}}
<pre>I:\>rexx des2
Line 4,043:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
 
Line 4,081:
printHexBytes(decBytes, "Decoded")
 
}</langsyntaxhighlight>
{{Out}}See it running in your browser by [https://scastie.scala-lang.org/t6nGq1ebShKEA42LSIQ6Hg Scastie (JVM)].
 
=={{header|Symsyn}}==
<langsyntaxhighlight Symsynlang="symsyn">pc1 : 56
: 48
: 40
Line 5,261:
endif
call Wds2Data
return</langsyntaxhighlight>
A trivial solution using the des encryption instruction:
<syntaxhighlight lang="text">key : x'0e329232ea6d0d73'
data : x'8787878787878787'
 
Line 5,271:
$s [] | output result - 0000000000000000
 
</syntaxhighlight>
</lang>
{{out}}
<pre>0000000000000000</pre>
Line 5,277:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.IO
Imports System.Security.Cryptography
 
Line 5,346:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Encoded: 0000000000000000A913F4CB0BD30F97
Line 5,356:
{{libheader|Wren-math}}
The second Kotlin version.
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt, Conv
import "/math" for Boolean
 
Line 5,673:
System.print("Decoded : %(decoded)")
System.print()
}</langsyntaxhighlight>
 
{{out}}
10,333

edits

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