1
0
forked from um/kgg-dec

chore: reformat files

This commit is contained in:
鲁树人 2024-11-19 16:59:28 +09:00
parent 84585531d4
commit edbe9ae708
5 changed files with 108 additions and 111 deletions

View File

@ -3,38 +3,36 @@
#include "tc_tea.h" #include "tc_tea.h"
#include <algorithm> #include <algorithm>
#include <string>
#include <array> #include <array>
#include <string>
#include <vector> #include <vector>
const static std::string kEKeyV2Prefix = "UVFNdXNpYyBFbmNWMixLZXk6"; const static std::string kEKeyV2Prefix = "UVFNdXNpYyBFbmNWMixLZXk6";
const static std::array<uint8_t, 16> kEKeyV2Key1{ const static std::array<uint8_t, 16> kEKeyV2Key1{
0x33, 0x38, 0x36, 0x5A, 0x4A, 0x59, 0x21, 0x40, 0x33, 0x38, 0x36, 0x5A, 0x4A, 0x59, 0x21, 0x40, 0x23, 0x2A, 0x24, 0x25, 0x5E, 0x26, 0x29, 0x28,
0x23, 0x2A, 0x24, 0x25, 0x5E, 0x26, 0x29, 0x28,
}; };
const static std::array<uint8_t, 16> kEKeyV2Key2{ const static std::array<uint8_t, 16> kEKeyV2Key2{
0x2A, 0x2A, 0x23, 0x21, 0x28, 0x23, 0x24, 0x25, 0x2A, 0x2A, 0x23, 0x21, 0x28, 0x23, 0x24, 0x25, 0x26, 0x5E, 0x61, 0x31, 0x63, 0x5A, 0x2C, 0x54,
0x26, 0x5E, 0x61, 0x31, 0x63, 0x5A, 0x2C, 0x54,
}; };
template <typename T> std::span<T> ss2span(std::string_view sv) { template <typename T>
std::span<T> ss2span(std::string_view sv) {
auto* data = reinterpret_cast<const T*>(sv.data()); auto* data = reinterpret_cast<const T*>(sv.data());
return std::span<T>(const_cast<T*>(data), sv.size()); return std::span<T>(const_cast<T*>(data), sv.size());
} }
template <typename T> std::string_view span2ss(std::span<T> span) { template <typename T>
std::string_view span2ss(std::span<T> span) {
return std::string_view(reinterpret_cast<char*>(span.data()), span.size()); return std::string_view(reinterpret_cast<char*>(span.data()), span.size());
} }
void remove_trailing_zeros(std::vector<uint8_t>& vec) { void remove_trailing_zeros(std::vector<uint8_t>& vec) {
auto it = std::find_if(vec.rbegin(), vec.rend(), auto it = std::find_if(vec.rbegin(), vec.rend(), [](uint8_t value) { return value != 0; });
[](uint8_t value) { return value != 0; });
vec.erase(it.base(), vec.end()); vec.erase(it.base(), vec.end());
} }
std::vector<uint8_t> decrypt_ekey_v1(std::string_view ekey) { std::vector<uint8_t> decrypt_ekey_v1(std::string_view ekey) {
std::vector<uint8_t> result = std::vector<uint8_t> result = b64_decode(reinterpret_cast<const uint8_t*>(ekey.data()), ekey.size());
b64_decode(reinterpret_cast<const uint8_t *>(ekey.data()), ekey.size());
remove_trailing_zeros(result); remove_trailing_zeros(result);
uint32_t tea_key[4] = { uint32_t tea_key[4] = {

View File

@ -10,8 +10,11 @@ constexpr size_t kTeaBlockSize = 8;
constexpr size_t kFixedSaltLen = 2; constexpr size_t kFixedSaltLen = 2;
constexpr size_t kZeroPadLen = 7; constexpr size_t kZeroPadLen = 7;
inline void decrypt_round(uint8_t *p_plain, const uint8_t *p_cipher, inline void decrypt_round(uint8_t* p_plain,
uint64_t *iv1, uint64_t *iv2, const uint32_t *key) { const uint8_t* p_cipher,
uint64_t* iv1,
uint64_t* iv2,
const uint32_t* key) {
uint64_t iv1_next = Endian::be_u64_read(p_cipher); uint64_t iv1_next = Endian::be_u64_read(p_cipher);
uint64_t iv2_next = tc_tea_ecb_decrypt(iv1_next ^ *iv2, key); uint64_t iv2_next = tc_tea_ecb_decrypt(iv1_next ^ *iv2, key);
uint64_t plain = iv2_next ^ *iv1; uint64_t plain = iv2_next ^ *iv1;
@ -20,8 +23,7 @@ inline void decrypt_round(uint8_t *p_plain, const uint8_t *p_cipher,
Endian::be_u64_write(p_plain, plain); Endian::be_u64_write(p_plain, plain);
} }
std::vector<uint8_t> tc_tea_cbc_decrypt(std::span<uint8_t> cipher, std::vector<uint8_t> tc_tea_cbc_decrypt(std::span<uint8_t> cipher, const uint32_t* key) {
const uint32_t *key) {
// It needs to have at least 2 blocks long, due to the nature of the padding // It needs to have at least 2 blocks long, due to the nature of the padding
// scheme used. // scheme used.
if (cipher.size() % kTeaBlockSize != 0 || cipher.size() < kTeaBlockSize * 2) { if (cipher.size() % kTeaBlockSize != 0 || cipher.size() < kTeaBlockSize * 2) {
@ -50,8 +52,7 @@ std::vector<uint8_t> tc_tea_cbc_decrypt(std::span<uint8_t> cipher,
if (real_plain_len != copy_len) { if (real_plain_len != copy_len) {
// Decrypt the rest of the blocks // Decrypt the rest of the blocks
for (size_t i = cipher.size() - kTeaBlockSize * 3; i != 0; for (size_t i = cipher.size() - kTeaBlockSize * 3; i != 0; i -= kTeaBlockSize) {
i -= kTeaBlockSize) {
decrypt_round(p_output, in_cipher, &iv1, &iv2, key); decrypt_round(p_output, in_cipher, &iv1, &iv2, key);
in_cipher += kTeaBlockSize; in_cipher += kTeaBlockSize;
p_output += kTeaBlockSize; p_output += kTeaBlockSize;

View File

@ -6,11 +6,9 @@
#include <span> #include <span>
#include <vector> #include <vector>
std::vector<uint8_t> tc_tea_cbc_decrypt(std::span<uint8_t> cipher, std::vector<uint8_t> tc_tea_cbc_decrypt(std::span<uint8_t> cipher, const uint32_t* key);
const uint32_t *key);
inline std::vector<uint8_t> tc_tea_cbc_decrypt(std::span<uint8_t> cipher, inline std::vector<uint8_t> tc_tea_cbc_decrypt(std::span<uint8_t> cipher, const uint8_t* key) {
const uint8_t *key) {
uint32_t key_u32[4]; uint32_t key_u32[4];
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
key_u32[i] = Endian::be_u32_read(key + i * 4); key_u32[i] = Endian::be_u32_read(key + i * 4);