Implement DES decryption
All checks were successful
ChaosTest CI / build-and-test (push) Successful in 41s

This commit is contained in:
hashlag
2026-01-23 22:22:28 +03:00
parent 5fd92e0c9d
commit 15dd7398d2
2 changed files with 225 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
#ifndef CHAOS_CIPHER_DES_DESCRYPT_HPP
#define CHAOS_CIPHER_DES_DESCRYPT_HPP
#include <algorithm>
#include <utility>
#include "Service/ChaosException.hpp"
@@ -98,7 +99,13 @@ public:
using RoundKey48 = uint64_t;
KeySchedule(const RawKey & rawKey)
enum class Direction
{
Encrypt,
Decrypt
};
KeySchedule(Direction direction, const RawKey & rawKey)
{
Key56 key56 = Pc1(Bitwise::PackUInt64(rawKey.Begin(), rawKey.End()));
@@ -119,6 +126,11 @@ public:
Schedule_[i] = Pc2(Bitwise::Merge<28>(c28, d28));
}
if (direction == Direction::Decrypt)
{
std::reverse(Schedule_.Begin(), Schedule_.End());
}
}
RoundKey48 operator[](int_fast8_t i) const
@@ -211,7 +223,7 @@ public:
{
public:
Encryptor(const Key & key)
: Schedule_(key.Key_)
: Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_)
{ }
template<typename OutputIt, typename InputIt>
@@ -226,7 +238,7 @@ public:
}
Block encrypted
= DesCrypt::EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(),
= DesCrypt::ProcessBlock(Inner_::Bitwise::PackUInt64(block.Begin(),
block.End()),
Schedule_);
@@ -237,6 +249,36 @@ public:
Inner_::KeySchedule Schedule_;
};
class Decryptor
{
public:
Decryptor(const Key & key)
: Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_)
{ }
template<typename OutputIt, typename InputIt>
void DecryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd)
{
RawBlockArray block;
int_fast8_t i = 0;
for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in)
{
block[i] = *in;
}
Block decrypted
= DesCrypt::ProcessBlock(Inner_::Bitwise::PackUInt64(block.Begin(),
block.End()),
Schedule_);
Inner_::Bitwise::CrunchUInt64(out, decrypted);
}
private:
Inner_::KeySchedule Schedule_;
};
private:
using Block = uint64_t;
using BlockHalf = uint32_t;
@@ -405,7 +447,7 @@ private:
FP_TABLE + std::size(FP_TABLE));
}
static Block EncryptBlock(Block block, const Inner_::KeySchedule & schedule)
static Block ProcessBlock(Block block, const Inner_::KeySchedule & schedule)
{
block = Ip(block);