#include "memory.h"
#include "3des.h"
#include "stdio.h"
#include <stdlib.h>
/*********************************************************/
void F_func(bool In[32], const bool Ki[48]); // f 函数
void S_func(bool Out[32], const bool In[48]); // S 盒代替
void Transform(bool *Out, bool *In, const char *Table, int len); // 变换
void Xor(bool *InA, const bool *InB, int len); // 异或
void RotateL(bool *In, int len, int loop); // 循环左移
void ByteToBit(bool *Out, const char *In, int bits); // 字节组转换成位组
void BitToByte(char *Out, const bool *In, int bits); // 位组转换成字节组
bool SubKey[16][48]; // 16圈子密钥
/*********************************************************/
/************************加密*********************************/
int Encrypt(char *Msg, char *Key, char *Cipher, int length) //如果消息长度不是8的倍数,末位补0凑成8的倍数。
{
if (length <= 0) {
return -1;
}
int OutLength = (length % 8 == 0) ? length : ((1 + (length / 8)) * 8);
char keyarray1[8];
char keyarray2[8];
char keyarray3[8];
memcpy(&keyarray1, &Key[0], 8);
memcpy(&keyarray2, &Key[8], 8);
memcpy(&keyarray3, &Key[16], 8);
if (length % 8 == 0) {
int dst = length / 8;
char out[8];
char in[8];
int j;
for (j = 0; j < dst; j++) {
memcpy(&in[0], &Msg[8 * j], 8);
encrypt(in, keyarray1, out); ///////////////////
decrypt(out, keyarray2, in); //////////////////
encrypt(in, keyarray3, out); //////////////////
memcpy(&Cipher[8 * j], &out, 8);
}
return OutLength;
} else {
int ext = length / 8;
int dst = length % 8;
char * temp_in;
char * temp_add;
temp_in = (char *) malloc((ext + 1) * 8);
temp_add = (char *) malloc(8 - dst);
memcpy(&temp_in[0], &Msg[0], length);
memset(temp_add, 0, 8 - dst);
memcpy(&temp_in[length], &temp_add[0], 8 - dst);
int round = ext + 1;
char out[8];
char in[8];
int j;
for (j = 0; j < round; j++) {
memcpy(&in[0], &temp_in[8 * j], 8);
encrypt(in, keyarray1, out);
decrypt(out, keyarray2, in);
encrypt(in, keyarray3, out);
memcpy(&Cipher[8 * j], &out, 8);
}
return OutLength;
}
}
void encrypt(char In[8], const char Key[8], char Out[8]) {
Des_SetKey(Key);
static bool M[64], Tmp[32], *Li = &M[0], *Ri = &M[32];
ByteToBit(M, In, 64);
Transform(M, M, IP_Table, 64);
int i;
for (i = 0; i < 16; i++) {
memcpy(Tmp, Ri, 32);
F_func(Ri, SubKey[i]);
Xor(Ri, Li, 32);
memcpy(Li, Tmp, 32);
}
Transform(M, M, IPR_Table, 64);
BitToByte(Out, M, 64);
}
/************************解密*********************************/
int Decrypt(char *Msg, char *Key, char *Cipher, int length) //如果消息长度不是8的倍数,末位补0凑成8的倍数。
{
if (length <= 0) {
return -1;
}
int OutLength = (length % 8 == 0) ? length : ((1 + (length / 8)) * 8);
char keyarray1[8];
char keyarray2[8];
char keyarray3[8];
memcpy(&keyarray1, &Key[0], 8);
memcpy(&keyarray2, &Key[8], 8);
memcpy(&keyarray3, &Key[16], 8);
if (length % 8 == 0) {
int dst = length / 8;
char out[8];
char in[8];
int j;
for (j = 0; j < dst; j++) {
memcpy(&in[0], &Msg[8 * j], 8);
decrypt(in, keyarray3, out);
encrypt(out, keyarray2, in);
decrypt(in, keyarray1, out);
memcpy(&Cipher[8 * j], &out, 8);
}
return OutLength;
} else {
int ext = length / 8;
int dst = length % 8;
char * temp_in;
char * temp_add;
temp_in = (char *) malloc((ext + 1) * 8);
temp_add = (char *) malloc(8 - dst);
memcpy(&temp_in[0], &Msg[0], length);
memset(temp_add, 0, 8 - dst);
memcpy(&temp_in[length], &temp_add[0], 8 - dst);
int round = ext + 1;
char out[8];
char in[8];
int j;
for (j = 0; j < round; j++) {
memcpy(&in[0], &temp_in[8 * j], 8);
decrypt(in, keyarray3, out);
encrypt(out, keyarray2, in);
decrypt(in, keyarray1, out);
memcpy(&Cipher[8 * j], &out, 8);
}
return OutLength;
}
}
void decrypt(char In[8], const char Key[8], char Out[8]) {
Des_SetKey(Key);
static bool M[64], Tmp[32], *Li = &M[0], *Ri = &M[32];
ByteToBit(M, In, 64);
Transform(M, M, IP_Table, 64);
int i;
for (i = 15; i >= 0; i--) {
memcpy(Tmp, Li, 32);
F_func(Li, SubKey[i]);
Xor(Li, Ri, 32);
memcpy(Ri, Tmp, 32);
}
Transform(M, M, IPR_Table, 64);
BitToByte(Out, M, 64);
}
/********************设置密钥*****************************/
void Des_SetKey(const char Key[8]) {
static bool K[64], *KL = &K[0], *KR = &K[28];
ByteToBit(K, Key, 64);
Transform(K, K, PC1_Table, 56);
int i;
for (i = 0; i < 16; i++) {
RotateL(KL, 28, LOOP_Table[i]);
RotateL(KR, 28, LOOP_Table[i]);
Transform(SubKey[i], K, PC2_Table, 48);
}
}
/*********************************************************/
void F_func(bool In[32], const bool Ki[48]) {
static bool MR[48];
Transform(MR, In, E_Table, 48);
Xor(MR, Ki, 48);
S_func(In, MR);
Transform(In, In, P_Table, 32);
}
void S_func(bool Out[32], const bool In[48]) {
char i, j, k;
for (i = 0; i < 8; i++, In += 6, Out += 4) {
j = (In[0] << 1) + In[5];
k = (In[1] << 3) + (In[2] << 2) + (In[3] << 1) + In[4];
ByteToBit(Out, &S_Box[i][j][k], 4);
}
}
void Transform(bool *Out, bool *In, const char *Table, int len) {
static bool Tmp[256];
int i;
for (i = 0; i < len; i++)
Tmp[i] = In[Table[i] - 1];
memcpy(Out, Tmp, len);
}
void Xor(bool *InA, const bool *InB, int len) {
int i;
for (i = 0; i < len; i++)
InA[i] ^= InB[i];
}
void RotateL(bool *In, int len, int loop) {
static bool Tmp[256];
memcpy(Tmp, In, loop);
memcpy(In, In + loop, len - loop);
memcpy(In + len - loop, Tmp, loop);
}
void ByteToBit(bool *Out, const char *In, int bits) {
int i;
for (i = 0; i < bits; i++)
Out[i] = (In[i / 8] >> (i % 8)) & 1;
}
void BitToByte(char *Out, const bool *In, int bits) {
memset(Out, 0, (bits + 7) / 8);
int i;
for (i = 0; i < bits; i++)
Out[i / 8] |= In[i] << (i % 8);
}
/********************* end *********************************/
Android下3DES加密算法的JNI实现(完善版)
5星 · 超过95%的资源 需积分: 50 120 浏览量
2013-12-18
17:38:14
上传
评论 6
收藏 104KB RAR 举报
wtbee
- 粉丝: 19
- 资源: 36
最新资源
- IMG_20240423_191216.jpg
- 基于MATLAB实现分析黑体辐射光谱图时将通过迈克尔逊干涉仪得到干涉图经过傅里叶变换转换为光谱图的功能源码.zip
- DynamicArray02.java
- 数据可视化 matplotlib 实例.docx
- 数据可视化 matplotlib 实例.7z
- Python 使用 Matplotlib 绘制甘特图的实践.7z
- Python 使用 Matplotlib 绘制甘特图的实践.docx
- matplotlib 中的 cmap 参数介绍.docx
- matplotlib 中的 cmap 参数介绍.7z
- 基于matlab的毕业设计基于Matlab使用LQR实现车辆轨迹跟踪源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页