42 std::vector<uint32_t> L =
expand(key);
43 S.resize(2 * (
r + 1));
44 const uint32_t P = 0xB7E15163;
45 const uint32_t Q = 0x9E3779B9;
47 for (
int i = 1; i < 2 * (
r + 1); ++i) {
50 uint32_t
A = 0,
B = 0;
52 for (
int k = 0; k < 3 * std::max(
b, 2 * (
r + 1)); ++k) {
55 i = (i + 1) % (2 * (
r + 1));
60 std::vector<uint32_t>
RC6::expand(
const std::vector<uint8_t> &key) {
61 std::vector<uint32_t> L(
b / 4);
62 for (
size_t i = 0; i < key.size(); i++) {
63 L[i / 4] |= (
static_cast<uint32_t
>(key[i]) << (8 * (i % 4)));
69 return (val << shift) | (val >> (
w - shift));
73 return (val >> shift) | (val << (
w - shift));
77 uint32_t
A = plaintext[0],
B = plaintext[1];
80 for (
int i = 1; i <=
r; ++i) {
89 uint32_t
A = ciphertext[0],
B = ciphertext[1];
90 for (
int i =
r; i >= 1; --i) {
100 std::vector<uint8_t>
RC6::encrypt(
const std::vector<uint8_t> &plaintext) {
101 if (plaintext.size() % 8 != 0) {
102 throw std::invalid_argument(
103 "Plaintext length must be a multiple of 8 bytes");
105 std::vector<uint8_t> ciphertext;
106 ciphertext.reserve(plaintext.size());
107 for (
size_t i = 0; i < plaintext.size(); i += 8) {
109 uint32_t encrypted_block[2];
110 for (
int j = 0; j < 8; ++j) {
112 (
static_cast<uint32_t
>(plaintext[i + j]) << (8 * (j % 4)));
115 for (
int j = 0; j < 2; ++j) {
116 ciphertext.push_back((encrypted_block[j] >> 24) & 0xFF);
117 ciphertext.push_back((encrypted_block[j] >> 16) & 0xFF);
118 ciphertext.push_back((encrypted_block[j] >> 8) & 0xFF);
119 ciphertext.push_back(encrypted_block[j] & 0xFF);
125 std::vector<uint8_t>
RC6::decrypt(
const std::vector<uint8_t> &ciphertext) {
126 if (ciphertext.size() % 8 != 0) {
127 throw std::invalid_argument(
128 "Ciphertext length must be a multiple of 8 bytes");
130 std::vector<uint8_t> plaintext;
131 plaintext.reserve(ciphertext.size());
132 for (
size_t i = 0; i < ciphertext.size(); i += 8) {
134 uint32_t decrypted_block[2];
135 for (
int j = 0; j < 2; ++j) {
137 (
static_cast<uint32_t
>(ciphertext[i + 4 * j]) << 24) |
138 (
static_cast<uint32_t
>(ciphertext[i + 4 * j + 1]) << 16) |
139 (
static_cast<uint32_t
>(ciphertext[i + 4 * j + 2]) << 8) |
140 static_cast<uint32_t
>(ciphertext[i + 4 * j + 3]);
143 for (
int j = 0; j < 8; ++j) {
144 plaintext.push_back((decrypted_block[j / 4] >> (8 * (j % 4))) &
std::vector< uint8_t > encrypt(const std::vector< uint8_t > &plaintext)
Encrypts plaintext using RC6 algorithm.
std::vector< uint32_t > expand(const std::vector< uint8_t > &key)
Performs key expansion.
std::vector< uint8_t > decrypt(const std::vector< uint8_t > &ciphertext)
Decrypts ciphertext using RC6 algorithm.
void key_schedule(const std::vector< uint8_t > &key)
Generates the key schedule from the given key.
std::vector< uint32_t > S
uint32_t rotl(uint32_t val, int shift)
Performs left rotation.
void decrypt_block(const uint32_t ciphertext[2], uint32_t plaintext[2])
Decrypts a single block of ciphertext.
void encrypt_block(const uint32_t plaintext[2], uint32_t ciphertext[2])
Encrypts a single block of plaintext.
RC6(const std::vector< uint8_t > &key)
Constructor for RC6 class.
uint32_t rotr(uint32_t val, int shift)
Performs right rotation.