基于RSA与MD5的数字签名过程:
1:对明文m通过MD5得到消息摘要msg
2:对m与msg进行RSA加密得到密文c,cmsg
3:传递c,cmsg
4:接收方对接收到的c,cmsg进行解密得到m‘,msg’。
5:接手方对明文m‘通过MD5得到消息摘要msg’‘
6:判断msg‘与msg’‘是否相等
结果截图:
代码实现:
RSA.cpp
// RSA加密算法.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include "MD5.h"
using namespace std;
string prime1 = "217348148938414312829991530009662094969";
string prime2 = "226625456845085456897380341837778341773";
string N = "49256623547601819313443212631599995144332154326734557815185481020182065840037";
string r = "49256623547601819313443212631599995143888180720951058045458109148334625403296";
string d = "29148854456454436705046635358720157607717178159840646185376413638432727169453";
string e = "997";
void divide(string s, int a) {
int len = s.size();
int flag = 0;
vector<int> ans;
for (int i = 0; i < len; ++i) {
//
// 17 689592....
//
int temp = flag * 10 + s[i] - '0';
if (temp < a) {
flag = temp;
ans.push_back(0);
}
else {
flag = temp % a;
ans.push_back(temp / a);
}
}
//for (int i = 0; i < ans.size(); ++i) {
//cout << ans[i];
//}
//cout << endl;
}
void copydeque(deque<int>& x, deque<int>& y)
{
for (deque<int>::const_iterator it = x.begin(); it != x.end(); it++)
{
y.push_back(*it);
}
}
deque<int> dequeTobinary(deque<int>& x)
{
//int index = 0;
deque<int> ans;
deque<int> a;
deque<int> temp;
copydeque(x, a);
while (a.size() > 0 && a.front() != 0)
{
//二进制
deque<int>::const_iterator it = a.end();
it--;
if (*it % 2 == 0)
{
ans.push_front(0);
}
else {
ans.push_front(1);
}
//余数
int flag = 0;
for (it = a.begin(); it != a.end(); it++)
{
if ((*it + flag * 10) < 2)
{
flag = *it;
temp.push_back(0);
if (it + 1 == a.end())
{
//temp.push_back(0);
}
}
else
{
temp.push_back((*it + flag * 10) / 2);
flag = (*it + flag * 10) % 2;
}
}
a.clear();
while (temp.size() > 0 && temp.front() == 0)
{
temp.pop_front();
}
if (temp.size() > 0) {
copydeque(temp, a);
//printDeque(temp);
}
temp.clear();
}
return ans;
}
deque<int> dequeToAdd(deque<int>& x, deque<int>& y)
{
deque<int> ans;
deque<int> temp1;
deque<int> temp2;
//temp1长度>=temp2
if (x.size() > y.size())
{
copydeque(x, temp1);
copydeque(y, temp2);
}
else
{
copydeque(y, temp1);
copydeque(x, temp2);
}
if (temp2.size() == 0)
{
return temp1;
}
deque<int>::const_iterator it1 = temp1.end();
it1--;
deque<int>::const_iterator it2 = temp2.end();
it2--;
int flag = 0;
while (it2 != temp2.begin())
{
if ((*it1 + *it2 + flag) > 9)
{
ans.push_front((*it1 + *it2 + flag) % 10);
flag = 1;
}
else
{
ans.push_front(*it1 + *it2 + flag);
flag = 0;
}
it1--;
it2--;
}
//如果位数相同
if (it1 == temp1.begin())
{
if ((*it1 + *it2 + flag) > 9)
{
ans.push_front((*it1 + *it2 + flag) % 10);
flag = 1;
ans.push_front(flag);
}
else
{
ans.push_front(*it1 + *it2 + flag);
flag = 0;
}
}
//如果位数不相同
else
{
//处理temp2首位
if ((*it1 + *it2 + flag) > 9)
{
ans.push_front((*it1 + *it2 + flag) % 10);
flag = 1;
}
else
{
ans.push_front(*it1 + *it2 + flag);
flag = 0;
}
it1--;
//处理temp1剩下的位数
while (it1 != temp1.begin())
{
if ((*it1 + flag) > 9)
{
ans.push_front((*it1 + flag) % 10);
flag = 1;
}
else
{
ans.push_front(*it1 + flag);
flag = 0;
}
it1--;
}
//处理temp1首位
if ((*it1 + flag) > 9)
{
ans.push_front((*it1 + flag) % 10);
flag = 1;
ans.push_front(flag);
}
else
{
ans.push_front(*it1 + flag);
flag = 0;
}
}
return ans;
}
int compare(deque<int>& x, deque<int>& y)
{
if (x.size() > y.size())
{
return 1;
}
else if (x.size() < y.size())
{
return 0;
}
else {
deque<int>::const_iterator it1 = x.begin();
deque<int>::const_iterator it2 = y.begin();
while (it1 != x.end())
{
if (*it1 > *it2)
{
return 1;
}
else if (*it1 < *it2)
{
return 0;
}
it1++;
it2++;
}
return -1;
}
}
deque<int> dequeToSub(deque<int>& x, deque<int>& y)
{
//默认x>y
deque<int> ans;
deque<int>::const_iterator it1 = x.end();
deque<int>::const_iterator it2 = y.end();
it1--;
it2--;
int flag = 0;
while (it2 != y.begin())
{
if ((*it1 - flag) >= *it2)
{
ans.push_front((*it1 - flag) - *it2);
flag = 0;
}
else
{
ans.push_front((*it1 - flag + 10) - *it2);
flag = 1;
}
it2--;
it1--;
}
//处理y首位
if ((*it1 - flag) >= *it2)
{
ans.push_front((*it1 - flag) - *it2);
flag = 0;
}
else
{
ans.push_front((*it1 - flag + 10) - *it2);
flag = 1;
}
if (it1 == x.begin())
{
while (ans.front() == 0 && ans.size() > 1)
{
ans.pop_front();
}
return ans;
}
it1--;
while (it1 != x.begin())
{
if (*it1 - flag >= 0)
{
ans.push_front(*it1 - flag);
flag = 0;
}
else
{
ans.push_front(*it1 - flag + 10);
flag = 1;
}
it1--;
}
ans.push_front(*it1 - flag);
while (ans.front() == 0 && ans.size() > 1)
{
ans.pop_front();
}
return ans;
}
deque<int> dequeToMod(deque<int>& x, deque<int>& y)
{
deque<int> ans;
deque<int> temp;
copydeque(x, temp);
deque<int> temp2;
copydeque(y, temp2);
int len = temp.size() - y.size();
//x>y
if (compare(x, y) == 1)
{
if (len < 2)//10倍范围内
{
ans = dequeToSub(temp, y);
while (compare(ans, y) != 0)
{
temp.clear();
copydeque(ans, temp);
ans.clear();
ans = dequeToSub(temp, y);
}
return ans;
}
else
{
while (len >= 2)
{
int a = len - 1;
while (a > 0)
{
temp2.push_back(0);
a--;
}
deque<int> temp3 = dequeToSub(temp, temp2);
temp.clear();
copydeque(temp3, temp);
temp3.clear();
len = temp.size() - y.size();
temp2.clear();
copydeque(y, temp2);
}
ans = dequeToMod(temp, y);
return ans;
}
}
else if (compare(x, y) == 0)
{
copydeque(x, ans);
}
else
{
ans.push_back(0);
}
return ans;
}
deque<int> quick_multiply(deque<int>& x, deque<int>& y, deque<int>& z) {
deque<int> ans;
deque<int> res;
copydeque(x, res);
deque<int> b = dequeTobinary(y);
while (b.size() > 0)
{
deque<int>::const_iterator it = b.end();
it--;
if (*it == 1)
{
ans = dequeToAdd(ans, res);
ans = dequeToMod(ans, z);
}
res = dequeToAdd(res, res);
res = dequeToMod(res, z);
b.pop_back();
}
return ans;
}
deque<int> quick_power(deque<int>& x, deque<int>& y, deque<int>& z) {
deque<int>ans;
ans.push_back(1);
deque<int>res;
copydeque(x, res);
deque<int> b = dequeTobinary(y);
while (b.size() > 0)
{
deque<int>::const_iterator it = b.end();
it--;
if (*it == 1)
{
ans = quick_multiply(ans, res, z);
}
res = quick_multiply(res, res, z);
b.pop_back();
}
return ans;
}
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it;
}
cout << endl;
}
deque<int> lock(char ch) {
int temp = ch;
deque<int> d1;
while (temp != 0) {
d1.push_front(temp % 10);
temp = temp / 10;
}
//deque<int> d2 = { 9,9,7 };
deque<int> d2;
for (int i = 0; i < e.size(); ++i) {
d2.push_back(e[i] - '0');
}
deque<int> d3;
for (int i = 0; i < N.size(); ++i) {
d3.push_back(N[i] - '0');
}
deque<int> d4 = quick_power(d1, d2, d3);
//cout << "**********" << endl;
//for (int i = 0; i < d4.size(); ++i)
// cout << d4[i] << " ";
//cout << "\n************" << endl;
return d4;
}
deque<int> unlock(deque<int>& d4) {
deque<int> d5;
for (int i = 0; i < d.size(); ++i) {
d5.push_back(d[i] - '0');
}
deque<int> d6;
for (int i = 0; i < N.size(); ++i) {
d6.push_back(N[i] - '0');
}
deque<int> d7 = quick_power(d4, d5, d6);
//printDeque(d7);
return d7;
}
vector< deque<int> > RSA(string msg) {
vector< deque<int> > ans;
for (int i = 0; i < msg.size(); ++i) {
deque<int> temp = lock(msg[i]);
ans.push_back(temp);
}
return ans;
}
vector<deque<int> > un_RSA(vector<deque<int> >& ans) {
vector<deque<int> > result;
for (int i = 0; i < ans.size(); ++i) {
deque<int> temp;
temp = unlock(ans[i]);
result.push_back(temp);
}
return result;
}
int main()
{
//明文
string msg = "math";
//(N,e)为公钥 (N,d)为私钥
cout << "明文为:";
for (int i = 0; i < msg.size(); ++i)
cout << msg[i] << " ";
cout << endl;
md5 MD5;
MD5.update(msg);
cout << "消息摘要:";
PrintMD5("math", MD5);
/// <summary>
/// RSA密钥呼唤
/// </summary>
/// <returns></returns>
///
swap(e,d);
cout << "开始对消息摘要进行RSA加密……" << endl;
//msg = "7e676e9e663beb40fd133f5ee24487c2";
msg = MD5.toString();
vector<deque<int> > v1 = RSA(msg);
cout << "密文为:" << endl;
for (int i = 0; i < v1.size(); ++i) {
cout << "***" << "i" << "*** : ";
printDeque(v1[i]);
}
cout << "开始对密文进行RSA解密……" << endl;
vector<deque<int> > v2 = un_RSA(v1);
cout << "解密后为:" << endl;
for (int i = 0; i < v2.size(); ++i) {
cout << "***" << "i" << "*** : ";
printDeque(v2[i]);
}
cout << "解密完成!!!" << endl;
return 0;
}
MD5.h
#pragma once
#include<iostream>
#include<fstream>
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
#define RotateLeft(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + ac; \
(a) = RotateLeft ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + ac; \
(a) = RotateLeft ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + ac; \
(a) = RotateLeft ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + ac; \
(a) = RotateLeft ((a), (s)); \
(a) += (b); \
}
using namespace std;
typedef unsigned char uchar;
typedef unsigned long ulong;
class md5 {
public:
md5();
md5(const void* input, size_t length);
md5(const string& str);//处理字符串
md5(ifstream& in);//处理txt文件
void update(const void* input, size_t length);
void update(const string& str);//处理字符串
void update(ifstream& in);//处理txt文件
const uchar* digest();
string toString();
void reset();
private:
void update(const uchar* input, size_t length);
void final();
void transform(const uchar block[64]);
void encode(const ulong* input, uchar* output, size_t length);
void decode(const uchar* input, ulong* output, size_t length);
string bytesToHexString(const uchar* input, size_t length);
md5(const md5&);
md5& operator=(const md5&);
ulong state[4]; /* state (ABCD) */
ulong count[2]; /* number of bits, modulo 2^64 (low-order word first) */
uchar buffer[64]; /* input buffer */
uchar _digest[16]; /* message digest */
bool over; /* calculate finished ? */
static const uchar padding[64]; /* padding for calculate */
static const char hex[16];
static const size_t BUFFER_SIZE = 1024;
};
const uchar md5::padding[64] = { 0x80 };
const char md5::hex[16] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
md5::md5() {
reset();
}
md5::md5(const void* input, size_t length) {
reset();
update(input, length);
}
md5::md5(const string& str) {
reset();
update(str);
}
md5::md5(ifstream& in) {
reset();
update(in);
}
const uchar* md5::digest() {
if (!over) {
over = true;
final();
}
return _digest;
}
void md5::reset() {
over = false;
/* reset number of bits. */
count[0] = count[1] = 0;
/* Load magic initialization constants. */
state[0] = 0x67452301;
state[1] = 0xefcdab89;
state[2] = 0x98badcfe;
state[3] = 0x10325476;
}
void md5::update(const void* input, size_t length) {
update((const uchar*)input, length);
}
void md5::update(const string& str) {
update((const uchar*)str.c_str(), str.length());
}
void md5::update(ifstream& in) {
if (!in)
return;
std::streamsize length;
char buffer[BUFFER_SIZE];
while (!in.eof()) {
in.read(buffer, BUFFER_SIZE);
length = in.gcount();
if (length > 0)
update(buffer, length);
}
in.close();
}
void md5::update(const uchar* input, size_t length) {
ulong i, index, partLen;
over = false;
/* Compute number of bytes mod 64 */
index = (ulong)((count[0] >> 3) & 0x3f);
/* update number of bits */
if ((count[0] += ((ulong)length << 3)) < ((ulong)length << 3))
count[1]++;
count[1] += ((ulong)length >> 29);
partLen = 64 - index;
/* transform as many times as possible. */
if (length >= partLen) {
memcpy(&buffer[index], input, partLen);
transform(buffer);
for (i = partLen; i + 63 < length; i += 64)
transform(&input[i]);
index = 0;
}
else {
i = 0;
}
/* Buffer remaining input */
memcpy(&buffer[index], &input[i], length - i);
}
void md5::final() {
uchar bits[8];
ulong oldState[4];
ulong oldCount[2];
ulong index, padLen;
/* Save current state and count. */
memcpy(oldState, state, 16);
memcpy(oldCount, count, 8);
/* Save number of bits */
encode(count, bits, 8);
/* Pad out to 56 mod 64. */
index = (ulong)((count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
update(padding, padLen);
/* Append length (before padding) */
update(bits, 8);
/* Store state in digest */
encode(state, _digest, 16);
/* Restore current state and count. */
memcpy(state, oldState, 16);
memcpy(count, oldCount, 8);
}
void md5::transform(const uchar block[64]) {
ulong a = state[0], b = state[1], c = state[2], d = state[3], x[16];
decode(block, x, 64);
/* Round 1 */
FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
void md5::encode(const ulong* input, uchar* output, size_t length) {
for (size_t i = 0, j = 0; j < length; i++, j += 4) {
output[j] = (uchar)(input[i] & 0xff);
output[j + 1] = (uchar)((input[i] >> 8) & 0xff);
output[j + 2] = (uchar)((input[i] >> 16) & 0xff);
output[j + 3] = (uchar)((input[i] >> 24) & 0xff);
}
}
void md5::decode(const uchar* input, ulong* output, size_t length) {
for (size_t i = 0, j = 0; j < length; i++, j += 4) {
output[i] = ((ulong)input[j]) | (((ulong)input[j + 1]) << 8) |
(((ulong)input[j + 2]) << 16) | (((ulong)input[j + 3]) << 24);
}
}
string md5::bytesToHexString(const uchar* input, size_t length) {
string str;
str.reserve(length << 1);
for (size_t i = 0; i < length; i++) {
int t = input[i];
int a = t / 16;
int b = t % 16;
str.append(1, hex[a]);
str.append(1, hex[b]);
}
return str;
}
string md5::toString() {
return bytesToHexString(digest(), 16);
}
void PrintMD5(const string& str, md5& md5) {
cout << "md5(\"" << str << "\") = " << md5.toString() << endl;
}
string FileDigest(const string& file) {
ifstream in(file.c_str(), ios::binary);
if (!in)
return "";
md5 md5;
streamsize length;
char buffer[1024];
while (!in.eof()) {
in.read(buffer, 1024);
length = in.gcount();
if (length > 0)
md5.update(buffer, length);
}
in.close();
return md5.toString();
}