TTKMusicPlayer  4.1.3.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
aeswrapper.cpp
Go to the documentation of this file.
1 #include "aeswrapper.h"
2 #include "aes.h"
3 
4 static constexpr int DATA_CACHE_SIZE = 1024;
5 
6 QByteArray QAlgorithm::Aes::encryptECB(const QByteArray &in, const QByteArray &key, bool hex)
7 {
8  AES_KEY aes;
9  if(AES_set_encrypt_key((const unsigned char *)key.data(), 128, &aes) < 0)
10  {
11  return {};
12  }
13 
14  const int remd = in.size() % AES_BLOCK_SIZE;
15  const int number = (remd == 0) ? AES_BLOCK_SIZE : (AES_BLOCK_SIZE - remd);
16 
17  QByteArray buffer(in);
18  for(int i = 0; i < number; ++i)
19  {
20  buffer.append(number);
21  }
22  QByteArray result(buffer.size(), '0');
23 
24  for(int i = 0; i < buffer.length() / AES_BLOCK_SIZE; ++i)
25  {
26  AES_ecb_encrypt((const unsigned char*)buffer.data() + AES_BLOCK_SIZE * i,
27  (unsigned char*)result.data() + AES_BLOCK_SIZE * i,
28  &aes,
29  AES_ENCRYPT);
30  }
31 
32  return hex ? result.toHex() : result.toBase64();
33 }
34 
35 QByteArray QAlgorithm::Aes::decryptECB(const QByteArray &in, const QByteArray &key, bool hex)
36 {
37  AES_KEY aes;
38  if(AES_set_decrypt_key((const unsigned char *)key.data(), 128, &aes) < 0)
39  {
40  return {};
41  }
42 
43  const QByteArray &buffer = hex ? QByteArray::fromHex(in) : QByteArray::fromBase64(in);
44  QByteArray result(buffer.length(), '0');
45 
46  for(int i = 0; i < buffer.length() / AES_BLOCK_SIZE; ++i)
47  {
48  AES_ecb_encrypt((const unsigned char*)buffer.data() + AES_BLOCK_SIZE * i,
49  (unsigned char*)result.data() + AES_BLOCK_SIZE * i,
50  &aes,
51  AES_DECRYPT);
52  }
53 
54  const char padding = result.at(result.size() - 1);
55  return result.left(result.size() - padding);
56 }
57 
58 QByteArray QAlgorithm::Aes::encryptCBC(const QByteArray &in, const QByteArray &key, const QByteArray &iv, bool hex)
59 {
60  AES_KEY aes;
61  if(AES_set_encrypt_key((const unsigned char *)key.data(), 128, &aes) < 0)
62  {
63  return {};
64  }
65 
66  const int length = in.length();
67  const int total = (length / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
68  const int number = (length % AES_BLOCK_SIZE > 0) ? total - length : AES_BLOCK_SIZE;
69 
70  char buffer[DATA_CACHE_SIZE] = {0};
71  char *input = new char[total + 1]();
72 
73  memset(input, number, total);
74  memcpy(input, in.data(), length);
75 
76  AES_cbc_encrypt((const unsigned char *)input, (unsigned char *)buffer, total, &aes, (unsigned char *)iv.data(), AES_ENCRYPT);
77  delete[] input;
78 
79  const QByteArray result((const char *)buffer, total);
80  return hex ? result.toHex() : result.toBase64();
81 }
82 
83 QByteArray QAlgorithm::Aes::decryptCBC(const QByteArray &in, const QByteArray &key, const QByteArray &iv, bool hex)
84 {
85  AES_KEY aes;
86  if(AES_set_decrypt_key((const unsigned char *)key.data(), 128, &aes) < 0)
87  {
88  return {};
89  }
90 
91  char buffer[DATA_CACHE_SIZE] = {0};
92  const QByteArray &input = hex ? QByteArray::fromHex(in) : QByteArray::fromBase64(in);
93 
94  AES_cbc_encrypt((const unsigned char *)input.data(), (unsigned char *)buffer, input.length(), &aes, (unsigned char *)iv.data(), AES_DECRYPT);
95 
96  QByteArray result(buffer);
97  const char padding = result.at(result.size() - 1);
98  return result.left(result.size() - padding);
99 }
static constexpr int DATA_CACHE_SIZE
Definition: aeswrapper.cpp:4
int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
Expand the cipher key into the decryption key schedule.
Definition: aes_core.cpp:731
int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
Expand the cipher key into the encryption key schedule.
Definition: aes_core.cpp:630
static constexpr wchar_t key[]
QByteArray encryptECB(const QByteArray &in, const QByteArray &key, bool hex=false)
Definition: aeswrapper.cpp:6
#define AES_DECRYPT
Definition: aes.h:20
QByteArray decryptECB(const QByteArray &in, const QByteArray &key, bool hex=false)
Definition: aeswrapper.cpp:35
void AES_ecb_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key, const int enc)
Definition: aes_ecb.cpp:15
QByteArray decryptCBC(const QByteArray &in, const QByteArray &key, const QByteArray &iv, bool hex=false)
Definition: aeswrapper.cpp:83
QByteArray encryptCBC(const QByteArray &in, const QByteArray &key, const QByteArray &iv, bool hex=false)
Definition: aeswrapper.cpp:58
Definition: aes.h:30
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, const int enc)
Definition: aes_cbc.cpp:4
#define AES_ENCRYPT
Definition: aes.h:19
#define AES_BLOCK_SIZE
Definition: aes.h:27