Data Encryption Standard: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
(→‎{{header|D}}: Update to DMD v2.102.0)
Line 1,049: Line 1,049:
=={{header|D}}==
=={{header|D}}==
{{trans|kotlin}}
{{trans|kotlin}}
<syntaxhighlight lang="d">import std.array;
<syntaxhighlight lang="d">import std.stdio, std.array, std.bitmanip;
import std.bitmanip;
import std.stdio;


immutable PC1 = [
immutable PC1 = [
Line 1,172: Line 1,170:
immutable SHIFTS = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
immutable SHIFTS = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];


BitArray bitArrayOfSize(uint count) {
BitArray bitArrayOfSize(ulong count) {
bool[] buffer = new bool[count];
bool[] buffer = new bool[count];
return BitArray(buffer);
return BitArray(buffer);
Line 1,179: Line 1,177:
ubyte[] encrypt(const ubyte[] key, const ubyte[] message) in {
ubyte[] encrypt(const ubyte[] key, const ubyte[] message) in {
assert(key.length == 8, "Incorrect key size");
assert(key.length == 8, "Incorrect key size");
} body {
} do {
BitArray[] ks = getSubKeys(key);
BitArray[] ks = getSubKeys(key);
ubyte[] m = message.dup;
ubyte[] m = message.dup;
Line 1,202: Line 1,200:
ubyte[] decrypt(const ubyte[] key, const ubyte[] encoded) in {
ubyte[] decrypt(const ubyte[] key, const ubyte[] encoded) in {
assert(key.length == 8, "Incorrect key size");
assert(key.length == 8, "Incorrect key size");
} body {
} do {
BitArray[] ks = getSubKeys(key);
BitArray[] ks = getSubKeys(key);
// reverse the subkeys
// reverse the subkeys
Line 1,227: Line 1,225:
private BitArray[] getSubKeys(const ubyte[] key) in {
private BitArray[] getSubKeys(const ubyte[] key) in {
assert(key.length == 8);
assert(key.length == 8);
} body {
} do {
auto k = key.toBitArray();
auto k = key.toBitArray();


Line 1,407: Line 1,405:
[0x0E, 0x32, 0x92, 0x32, 0xEA, 0x6D, 0x0D, 0x73],
[0x0E, 0x32, 0x92, 0x32, 0xEA, 0x6D, 0x0D, 0x73],
];
];

immutable ubyte[][] messages = [
immutable ubyte[][] messages = [
[cast(ubyte)0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF],
[cast(ubyte)0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF],
[0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87],
[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],
[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],
];
];

assert(keys.length == messages.length);
assert(keys.length == messages.length);


Line 1,417: Line 1,421:
writefln("Key : %(%02X%)", keys[i]);
writefln("Key : %(%02X%)", keys[i]);
writefln("Message : %(%02X%)", messages[i]);
writefln("Message : %(%02X%)", messages[i]);

ubyte[] encoded = encrypt(keys[i], messages[i]);
ubyte[] encoded = encrypt(keys[i], messages[i]);
writefln("Encoded : %(%02X%)", encoded);
writefln("Encoded : %(%02X%)", encoded);

ubyte[] decoded = decrypt(keys[i], encoded);
ubyte[] decoded = decrypt(keys[i], encoded);
writefln("Decoded : %(%02X%)", decoded);
writefln("Decoded : %(%02X%)", decoded);

writeln;
writeln;
}
}