Share 3des c++ source code

ngoctrinh411

New member
## 3DES C ++ Mã nguồn

### Giới thiệu

3DES (Triple DES) là một mật mã khối đối xứng, đây là một cải tiến so với mật mã DES (tiêu chuẩn mã hóa dữ liệu) ban đầu.Nó sử dụng ba khóa riêng biệt để mã hóa dữ liệu, giúp nó an toàn hơn DES.

### Mã nguồn C ++

Sau đây là mã nguồn C ++ cho thuật toán 3DES:

`` `C ++
#include <Istream>
#include <String>

sử dụng không gian tên STD;

// Lịch trình chính 3DES
Key_schedule dài không dấu [3] [16];

// chức năng tròn 3des
dài không dấu f (dài không dấu x, dài không dấu k) {
// Mở rộng khối đầu vào lên 48 bit
x = ((x << 8) | (x >> 56)) ^ ((x << 16) | (x >> 48)) ^ ((x << 24) | (x >> 32)) ^((x << 32) | (x >> 24)) ^ ((x << 40) | (x >> 16));

// XOR với phím tròn
x ^= k;

// Áp dụng các hộp S
x = (x >> 4) ^ (x << 28) ^ (x >> 14) ^ (x << 18) ^ (x >> 23) ^ (x << 10) ^ (x >> 7)^ (x << 1);

// XOR với phím tròn một lần nữa
x ^= k;

trả lại x;
}

// Chức năng mã hóa 3DES
Mã hóa dài dài không dấu (Bản hợp pháp dài dài không dấu, Key dài dài không dấu, Key dài dài không dấu2, Key dài dài không dấu3) {
// Vòng 1
plaintext = f (plaintext, key1);

// Vòng 2
plaintext = f (plaintext, key2);

// Vòng thứ 3
plaintext = f (plaintext, key3);

// Vòng 4
plaintext = f (plaintext, key1);

// Vòng 5
plaintext = f (plaintext, key2);

// Vòng thứ 6
plaintext = f (plaintext, key3);

trả lại bản rõ;
}

// Chức năng giải mã 3des
giải mã dài không dấu (bản mã dài không dấu, Key dài không dấu dài, Key2 dài không dấu, Key dài dài không dấu3) {
// Vòng thứ 6
CIPHERTEXT = F (CIPHERTEXT, KEY3);

// Vòng 5
CIPHERTEXT = F (CIPHERTEXT, KEY2);

// Vòng 4
CIPHERTEXT = F (CIPHERTEXT, KEY1);

// Vòng thứ 3
CIPHERTEXT = F (CIPHERTEXT, KEY3);

// Vòng 2
CIPHERTEXT = F (CIPHERTEXT, KEY2);

// Vòng 1
CIPHERTEXT = F (CIPHERTEXT, KEY1);

trả lại bản mã;
}

int main () {
// Tạo khóa ngẫu nhiên
không dấu dài key1 = rand () % (1 << 64);
Key2 dài không dấu2 = rand () % (1 << 64);
Key3 dài không dấu3 = rand () % (1 << 64);

// Mã hóa một thông báo rõ ràng
Không dấu dài bản rõ dài = 0x1234567890ABCDEF;
bản mã dài không dấu dài = mã hóa (plaintext, key1, key2, key3);

// giải mã mật mã
không dấu dài giải mã decrypted_plaintext = decrypt (Mật mã, key1, key2, key3);

// In kết quả
cout << "PlainText:" << Plaintext << endl;
cout << "Mật mã:" << COGHERTEXT << ENDL;
Cout << "Bản rõ được giải mã:" << Decrypted_
=======================================
## 3DES C++ Source Code

### Introduction

3DES (Triple DES) is a symmetric-key block cipher, which is an improvement over the original DES (Data Encryption Standard) cipher. It uses three separate keys to encrypt the data, which makes it more secure than DES.

### C++ Source Code

The following is the C++ source code for the 3DES algorithm:

```c++
#include <iostream>
#include <string>

using namespace std;

// 3DES key schedule
unsigned long long key_schedule[3][16];

// 3DES round function
unsigned long long f(unsigned long long x, unsigned long long k) {
// Expand the input block to 48 bits
x = ((x << 8) | (x >> 56)) ^ ((x << 16) | (x >> 48)) ^ ((x << 24) | (x >> 32)) ^ ((x << 32) | (x >> 24)) ^ ((x << 40) | (x >> 16));

// XOR with the round key
x ^= k;

// Apply the S-boxes
x = (x >> 4) ^ (x << 28) ^ (x >> 14) ^ (x << 18) ^ (x >> 23) ^ (x << 10) ^ (x >> 7) ^ (x << 1);

// XOR with the round key again
x ^= k;

return x;
}

// 3DES encryption function
unsigned long long encrypt(unsigned long long plaintext, unsigned long long key1, unsigned long long key2, unsigned long long key3) {
// 1st round
plaintext = f(plaintext, key1);

// 2nd round
plaintext = f(plaintext, key2);

// 3rd round
plaintext = f(plaintext, key3);

// 4th round
plaintext = f(plaintext, key1);

// 5th round
plaintext = f(plaintext, key2);

// 6th round
plaintext = f(plaintext, key3);

return plaintext;
}

// 3DES decryption function
unsigned long long decrypt(unsigned long long ciphertext, unsigned long long key1, unsigned long long key2, unsigned long long key3) {
// 6th round
ciphertext = f(ciphertext, key3);

// 5th round
ciphertext = f(ciphertext, key2);

// 4th round
ciphertext = f(ciphertext, key1);

// 3rd round
ciphertext = f(ciphertext, key3);

// 2nd round
ciphertext = f(ciphertext, key2);

// 1st round
ciphertext = f(ciphertext, key1);

return ciphertext;
}

int main() {
// Generate a random key
unsigned long long key1 = rand() % (1 << 64);
unsigned long long key2 = rand() % (1 << 64);
unsigned long long key3 = rand() % (1 << 64);

// Encrypt a plaintext message
unsigned long long plaintext = 0x1234567890abcdef;
unsigned long long ciphertext = encrypt(plaintext, key1, key2, key3);

// Decrypt the ciphertext
unsigned long long decrypted_plaintext = decrypt(ciphertext, key1, key2, key3);

// Print the results
cout << "Plaintext: " << plaintext << endl;
cout << "Ciphertext: " << ciphertext << endl;
cout << "Decrypted plaintext: " << decrypted_
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top