Jump to content

Data Encryption Standard: Difference between revisions

→‎{{header|D}}: Update to DMD v2.102.0
m (syntax highlighting fixup automation)
(→‎{{header|D}}: Update to DMD v2.102.0)
Line 1,049:
=={{header|D}}==
{{trans|kotlin}}
<syntaxhighlight lang="d">import std.stdio, std.array, std.bitmanip;
import std.bitmanip;
import std.stdio;
 
immutable PC1 = [
Line 1,172 ⟶ 1,170:
immutable SHIFTS = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
 
BitArray bitArrayOfSize(uintulong count) {
bool[] buffer = new bool[count];
return BitArray(buffer);
Line 1,179 ⟶ 1,177:
ubyte[] encrypt(const ubyte[] key, const ubyte[] message) in {
assert(key.length == 8, "Incorrect key size");
} bodydo {
BitArray[] ks = getSubKeys(key);
ubyte[] m = message.dup;
Line 1,202 ⟶ 1,200:
ubyte[] decrypt(const ubyte[] key, const ubyte[] encoded) in {
assert(key.length == 8, "Incorrect key size");
} bodydo {
BitArray[] ks = getSubKeys(key);
// reverse the subkeys
Line 1,227 ⟶ 1,225:
private BitArray[] getSubKeys(const ubyte[] key) in {
assert(key.length == 8);
} bodydo {
auto k = key.toBitArray();
 
Line 1,407 ⟶ 1,405:
[0x0E, 0x32, 0x92, 0x32, 0xEA, 0x6D, 0x0D, 0x73],
];
 
immutable ubyte[][] messages = [
[cast(ubyte)0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF],
[0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87],
[0x59, 0x6F, 0x75, 0x72, 0x20, 0x6C, 0x69, 0x70, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x73, 0x6D, 0x6F, 0x6F, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x76, 0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65, 0x0D, 0x0A],
0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x73, 0x6D,
0x6F, 0x6F, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74,
0x68, 0x61, 0x6E, 0x20, 0x76, 0x61, 0x73, 0x65,
0x6C, 0x69, 0x6E, 0x65, 0x0D, 0x0A],
];
 
assert(keys.length == messages.length);
 
Line 1,417 ⟶ 1,421:
writefln("Key : %(%02X%)", keys[i]);
writefln("Message : %(%02X%)", messages[i]);
 
ubyte[] encoded = encrypt(keys[i], messages[i]);
writefln("Encoded : %(%02X%)", encoded);
 
ubyte[] decoded = decrypt(keys[i], encoded);
writefln("Decoded : %(%02X%)", decoded);
 
writeln;
}
117

edits

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