N个整数(数的大小为0-255)的序列,把它们加密为K个整数(数的大小为0-255).再将K个整数顺序随机打乱,使得可以从这乱序的K个整数中解码出原序列。设计加密解密算法,且要求K<=15*N.
如果是:
- N<=16,要求K<=16*N.
- N<=16,要求K<=10*N.
- N<=64,要求K<=15*N.
#include <iostream>
using namespace std;
void printArray(int* arr, int len) {
if (!arr) {
return;
}
for (int i = 0; i < len; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int cmp(const void *a, const void *b) {
int aa = (*(const int*) a) >> 4;
int bb = (*(const int*) b) >> 4;
return aa - bb;
}
int cmp1(const void *a, const void *b) {
int aa = (*(const int*) a) >> 2;
int bb = (*(const int*) b) >> 2;
return aa - bb;
}
void disorder(int* arr, int len) {
srand((int) time(NULL));
for (int i = 0; i < len; i++) {
int tmp = rand() % len;
swap(arr[i], arr[tmp]);
}
}
int* encrypt1(int* arr, int len, int k) {
int id = 0;
int* encryptArr = new int[k * len];
memset(encryptArr, 0, sizeof(int) * k * len);
for (int i = 0; i < len; ++i) {
int index = (i << 4);
int prev = arr[i] >> 4;
int last = arr[i] & 15;
for (int j = 0; j < k - 8; ++j, id++) {
encryptArr[id] = index | prev;
}
for (int j = 0; j < 8; ++j, id++) {
encryptArr[id] = index | last;
}
}
// printArray(encryptArr, k * len);
disorder(encryptArr, k * len);
return encryptArr;
}
int* encrypt2(int* arr, int len, int k) {
int id = 0;
int* encryptArr = new int[k * len];
memset(encryptArr, 0, sizeof(int) * k * len);
for (int i = 0; i < len; ++i) {
int index = (i << 2);
int data1 = arr[i] >> 6;
int data2 = (arr[i] >> 4) & 3;
int data3 = (arr[i] >> 2) & 3;
int data4 = arr[i] & 3;
encryptArr[id++] = index | data1;
for (int j = 0; j < 2; ++j, id++) {
encryptArr[id] = index | data2;
}
for (int j = 0; j < 4; ++j, id++) {
encryptArr[id] = index | data3;
}
for (int j = 0; j < 8; ++j, id++) {
encryptArr[id] = index | data4;
}
}
// printArray(encryptArr, k * len);
disorder(encryptArr, k * len);
return encryptArr;
}
int* decrypt1(int* encryptArr, int len, int k) {
qsort(encryptArr, k * len, sizeof(int), cmp);
// printArray(encryptArr, k * len);
int* decryptArr = new int[len];
memset(decryptArr, 0, sizeof(int) * len);
int prev = 0;
int last = 0;
int count1 = 0;
int count2 = 0;
int index = 0;
for (int i = 0; i < k * len; i += k) {
prev = encryptArr[i];
count1 = 1;
count2 = 0;
for (int j = i + 1; j < i + k; ++j) {
if (prev == encryptArr[j]) {
count1++;
} else {
last = encryptArr[j];
count2++;
}
}
if (count1 > k - 8) {
if (count1 == k) { //加密前prev,last相同
last = prev;
}
swap(last, prev);
} else if (count2 > k - 8) {
if (count2 == k) {
prev = last;
}
}
prev &= 15;
last &= 15;
prev <<= 4;
decryptArr[index++] = prev | last;
}
return decryptArr;
}
int* decrypt2(int* encryptArr, int len, int k) {
qsort(encryptArr, k * len, sizeof(int), cmp1);
// printArray(encryptArr, k * len);
int* decryptArr = new int[len];
memset(decryptArr, 0, sizeof(int) * len);
int* data = new int[4];
int* count = new int[4];
int index = 0;
int id = 1;
for (int i = 0; i < k * len; i += k) {
memset(data, -1, 4 * sizeof(int));
memset(count, 0, 4 * sizeof(int));
data[0] = encryptArr[i];
count[0] = 1;
id = 1;
for (int j = i + 1; j < i + k; ++j) {
if (encryptArr[j] == data[0]) {
count[0]++;
} else if (encryptArr[j] == data[1]) {
count[1]++;
} else if (encryptArr[j] == data[2]) {
count[2]++;
} else if (encryptArr[j] == data[3]) {
count[3]++;
} else if (id < 4) {
data[id] = encryptArr[j];
count[id]++;
id++;
}
}
int data1 = -1;
int data2 = -1;
int data3 = -1;
int data4 = -1;
for (int i = 0; i < 4; ++i) {
if (!count[i]) {
break;
}
switch (count[i]) {
case 1:
data1 = data[i] & 3;
break;
case 2:
data2 = data[i] & 3;
break;
case 4:
data3 = data[i] & 3;
break;
case 8:
data4 = data[i] & 3;
break;
case 3:
data1 = data2 = data[i] & 3;
break;
case 5:
data1 = data3 = data[i] & 3;
break;
case 9:
data1 = data4 = data[i] & 3;
break;
case 6:
data2 = data3 = data[i] & 3;
break;
case 10:
data2 = data4 = data[i] & 3;
break;
case 12:
data3 = data4 = data[i] & 3;
break;
case 7:
data1 = data2 = data3 = data[i] & 3;
break;
case 11:
data1 = data2 = data4 = data[i] & 3;
break;
case 13:
data1 = data3 = data4 = data[i] & 3;
break;
case 14:
data2 = data3 = data4 = data[i] & 3;
break;
}
}
decryptArr[index] |= data1 << 6;
decryptArr[index] |= data2 << 4;
decryptArr[index] |= data3 << 2;
decryptArr[index] |= data4;
index++;
}
return decryptArr;
}
int* encryptChoice(int* arr, int len, int n, int k) {
int* encryptArr = NULL;
if (n == 16) {
if (len > n) {
return NULL;
}
if (k == 16) {
k = 12;
encryptArr = encrypt1(arr, len, k);
} else if (k == 10) {
encryptArr = encrypt1(arr, len, k);
}
} else if (n == 64 && k == 15) {
encryptArr = encrypt2(arr, len, k);
}
if (encryptArr) {
cout << "n,k: " << n << "," << k << endl;
cout << "加密数组:" << endl;
printArray(encryptArr, k * len);
}
return encryptArr;
}
void decryptChoice(int* encryptArr, int len, int n, int k) {
int* decryptArr = NULL;
if (n == 16) {
//int 是32bit
if (len > n) {
return;
}
if (k == 16) {
k = 12;
decryptArr = decrypt1(encryptArr, len, k);
} else if (k == 10) {
decryptArr = decrypt1(encryptArr, len, k);
}
} else if (n == 64 && k == 15) {
decryptArr = decrypt2(encryptArr, len, k);
}
if (decryptArr) {
cout << "解密数组:" << endl;
printArray(decryptArr, len);
}
}
int main() {
int arr1[] =
{ 12, 20, 51, 4, 7, 48, 32, 13, 41, 35, 44, 67, 55, 34, 54, 63 };
int len = sizeof(arr1) / sizeof(int);
int k = 16;
int n = 16;
int* encryptArr = NULL;
encryptArr = encryptChoice(arr1, len, n, k);
decryptChoice(encryptArr, len, n, k);
k = 10;
encryptArr = encryptChoice(arr1, len, n, k);
decryptChoice(encryptArr, len, n, k);
k = 15;
n = 64;
encryptArr = encryptChoice(arr1, len, n, k);
decryptChoice(encryptArr, len, n, k);
return 0;
}