Data Encryption Standard: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|Raku}}: revised) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 14:
=={{header|C}}==
{{trans|D}}
<
#include <stdlib.h>
#include <string.h>
Line 514:
driver(keys[2], message3, len);
return 0;
}</
{{out}}
<pre>Key : 133457799BBCDFF1
Line 532:
=={{header|C sharp|C#}}==
<
using System.IO;
using System.Security.Cryptography;
Line 602:
}
}
}</
{{out}}
<pre>Encoded: 0000000000000000A913F4CB0BD30F97
Line 609:
=={{header|C++}}==
{{trans|D}}
<
#include <array>
#include <bitset>
Line 1,030:
return 0;
}</
{{out}}
<pre>Key : 133457799BBCDFF1
Line 1,049:
=={{header|D}}==
{{trans|kotlin}}
<
import std.bitmanip;
import std.stdio;
Line 1,424:
}
}
</syntaxhighlight>
{{out}}
<pre>Key : 133457799BBCDFF1
Line 1,443:
=={{header|F Sharp|F#}}==
{{trans|C#}}
<
open System.Security.Cryptography
open System.IO
Line 1,501:
printfn "Decoded: %s" (ByteArrayToString decBytes)
0 // return an integer exit code</
{{out}}
<pre>Encoded: 0000000000000000A913F4CB0BD30F97
Line 1,507:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,784:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> key 133457799BBCDFF1
Line 1,803:
=={{header|Go}}==
'''Library solution:'''
<
import (
Line 1,828:
c.Encrypt(dst, src)
fmt.Printf("%x\n", dst)
}</
{{out}}
<pre>
Line 1,836:
=={{header|Java}}==
{{trans|Kotlin}}
<
import javax.crypto.spec.SecretKeySpec;
Line 1,877:
printHexBytes(decBytes, "Decoded");
}
}</
{{out}}
<pre>Encoded: 0000000000000000a913f4cb0bd30f97
Line 1,884:
=={{header|Julia}}==
===Using the MbedTLS library===
<
const testdata = [
Line 1,907:
println("Decoded : $(bytes2hex(decoded))\n")
end
</
<pre>
Key : 133457799bbcdff1
Line 1,926:
===Base Julia only===
{{trans|Phix}}
<
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
Line 2,089:
println("Decoded : $(bytes2hex(decoded))\n")
end
</syntaxhighlight>
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:
<
import javax.crypto.Cipher
Line 2,132:
val decBytes = decCipher.doFinal(encBytes)
decBytes.printHexBytes("Decoded")
}</
{{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:
<
import java.util.BitSet
Line 2,459:
println()
}
}</
{{out}}
<pre>Key : 133457799BBCDFF1
Line 2,477:
=={{header|Modula-2}}==
<
FROM SYSTEM IMPORT BYTE,ADR;
FROM DES IMPORT DES,Key1,Create,Destroy,EncryptECB,DecryptECB;
Line 2,530:
Destroy(cipher);
ReadChar
END DataEncryptionStandard.</
{{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.
<
Line 2,871:
let decoded = decrypt(Keys[i], encoded)
echo "Decoded: ", decoded.toHex()
echo()</
{{out}}
Line 2,890:
=={{header|Perl}}==
<
use strict;
Line 2,902:
print "Encoded : ", unpack("H*", $ciphertext), "\n";
print "Decoded : ", unpack("H*", $cipher->decrypt($ciphertext)), "\n";
</syntaxhighlight>
{{out}}
<pre>
Line 2,915:
easier to debug/verify, probably sidestep a few fiddly endian issues, and certainly
simplify bit-wise permutations.
<!--<
<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>
<!--</
{{out}}
<pre>
Line 3,127:
=={{header|PicoLisp}}==
<
*PC1
(57 49 41 33 25 17 9
Line 3,295:
16
(hex
(bin (mapcar '((I) (get R I)) *IP_INV)))) ) )</
{{out}}
<pre>
Line 3,310:
implemented like in the article linked in description. <br>
really good article btw
<
#!/usr/bin/python
Line 3,555:
prove(k2, m2)
</syntaxhighlight>
{{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"
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; </
{{out}}
<pre>Encryption examples:
Line 3,741:
Implementation of the algorithm described in the cited article.
<br>Decryption is now supported as well
<
Parse Upper Arg action
Select
Line 4,030:
Return r
debug: /* Say arg(1) */ Return</
{{out}}
<pre>I:\>rexx des2
Line 4,043:
=={{header|Scala}}==
<
import javax.crypto.spec.SecretKeySpec
Line 4,081:
printHexBytes(decBytes, "Decoded")
}</
{{Out}}See it running in your browser by [https://scastie.scala-lang.org/t6nGq1ebShKEA42LSIQ6Hg Scastie (JVM)].
=={{header|Symsyn}}==
<
: 48
: 40
Line 5,261:
endif
call Wds2Data
return</
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>
{{out}}
<pre>0000000000000000</pre>
Line 5,277:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Imports System.Security.Cryptography
Line 5,346:
End Sub
End Module</
{{out}}
<pre>Encoded: 0000000000000000A913F4CB0BD30F97
Line 5,356:
{{libheader|Wren-math}}
The second Kotlin version.
<
import "/math" for Boolean
Line 5,673:
System.print("Decoded : %(decoded)")
System.print()
}</
{{out}}
|