/* * (C) Copyright 2012 * wangflord@allwinnertech.com * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; * */ /* A. 加密解密 1. 密钥的产生 1) 找出两个相异的大素数P和Q,令N=P×Q,M=(P-1)*(Q-1)。 2) 找出与M互素的大数E,且E 0) { if(power & 1) { ret = (ret * base_value) % divider; } power /= 2; base_value = (base_value * base_value) % divider; } return ret; } unsigned rsa_init(void) { unsigned k; unsigned product; unsigned m_value; m_value = M; k = 1; if(probe_gcd(m_value, E) == 1) //e,M互质 { do { product = M * k + 1; if(!(product % E)) { pblc_keys.public_key = E; pblc_keys.divider = N; prvt_keys.private_key = product/E; prvt_keys.divider = N; #ifdef DEBUG_MODE rsa_dump(); #endif return 0; } k ++; } while(1); } return -1; } void rsa_dump(void) { printf("base value\n"); printf("M = %d(%d * %d), N = %d(%d * %d)\n", M, P-1, Q-1, N, P, Q); printf("public key: \n"); printf("{e, n} = %d, %d\n", pblc_keys.public_key, pblc_keys.divider); printf("private key: \n"); printf("{d, n} = %d, %d\n", prvt_keys.private_key, prvt_keys.divider); } void rsa_encrypt( unsigned *input, unsigned int length, unsigned *output ) { unsigned int i; for(i=0;i