㈠ 什么是数据加密
考虑到用户可能试图旁路系统的情况,如物理地取走数据库,在通讯线路上窃听。对这样的威胁最有效的解决方法就是数据加密,即以加密格式存储和传输敏感数据。
数据加密的术语有:明文,即原始的或未加密的数据。通过加密算法对其进行加密,加密算法的输入信息为明文和密钥;密文,明文加密后的格式,是加密算法的输出信息。加密算法是公开的,而密钥则是不公开的。密文,不应为无密钥的用户理解,用于数据的存储以及传输。
例:明文为字符串:
AS KINGFISHERS CATCH FIRE
(为简便起见,假定所处理的数据字符仅为大写字母和空格符)。假定密钥为字符串:
ELIOT
加密算法为:
1) 将明文划分成多个密钥字符串长度大小的块(空格符以"+"表示)
AS+KI NGFIS HERS+ CATCH +FIRE
2) 用00~26范围的整数取代明文的每个字符,空格符=00,A=01,...,Z=26:
0119001109 1407060919 0805181900 0301200308 0006091805
3) 与步骤2一样对密钥的每个字符进行取代:
0512091520
4) 对明文的每个块,将其每个字符用对应的整数编码与密钥中相应位置的字符的整数编码的和模27后的值取代:
5) 将步骤4的结果中的整数编码再用其等价字符替换:
FDIZB SSOXL MQ+GT HMBRA ERRFY
如果给出密钥,该例的解密过程很简单。问题是对于一个恶意攻击者来说,在不知道密钥的情况下,利用相匹配的明文和密文获得密钥究竟有多困难?对于上面的简单例子,答案是相当容易的,不是一般的容易,但是,复杂的加密模式同样很容易设计出。理想的情况是采用的加密模式使得攻击者为了破解所付出的代价应远远超过其所获得的利益。实际上,该目的适用于所有的安全性措施。这种加密模式的可接受的最终目标是:即使是该模式的发明者也无法通过相匹配的明文和密文获得密钥,从而也无法破解密文。
1. 数据加密标准
传统加密方法有两种,替换和置换。上面的例子采用的就是替换的方法:使用密钥将明文中的每一个字符转换为密文中的一个字符。而置换仅将明文的字符按不同的顺序重新排列。单独使用这两种方法的任意一种都是不够安全的,但是将这两种方法结合起来就能提供相当高的安全程度。数据加密标准(Data Encryption Standard,简称DES)就采用了这种结合算法,它由IBM制定,并在1977年成为美国官方加密标准。
DES的工作原理为:将明文分割成许多64位大小的块,每个块用64位密钥进行加密,实际上,密钥由56位数据位和8位奇偶校验位组成,因此只有256个可能的密码而不是264个。每块先用初始置换方法进行加密,再连续进行16次复杂的替换,最后再对其施用初始置换的逆。第i步的替换并不是直接利用原始的密钥K,而是由K与i计算出的密钥Ki。
DES具有这样的特性,其解密算法与加密算法相同,除了密钥Ki的施加顺序相反以外。
2. 公开密钥加密
多年来,许多人都认为DES并不是真的很安全。事实上,即使不采用智能的方法,随着快速、高度并行的处理器的出现,强制破解DES也是可能的。"公开密钥"加密方法使得DES以及类似的传统加密技术过时了。公开密钥加密方法中,加密算法和加密密钥都是公开的,任何人都可将明文转换成密文。但是相应的解密密钥是保密的(公开密钥方法包括两个密钥,分别用于加密和解密),而且无法从加密密钥推导出,因此,即使是加密者若未被授权也无法执行相应的解密。
公开密钥加密思想最初是由Diffie和Hellman提出的,最着名的是Rivest、Shamir以及Adleman提出的,现在通常称为RSA(以三个发明者的首位字母命名)的方法,该方法基于下面的两个事实:
1) 已有确定一个数是不是质数的快速算法;
2) 尚未找到确定一个合数的质因子的快速算法。
RSA方法的工作原理如下:
1) 任意选取两个不同的大质数p和q,计算乘积r=p*q;
2) 任意选取一个大整数e,e与(p-1)*(q-1)互质,整数e用做加密密钥。注意:e的选取是很容易的,例如,所有大于p和q的质数都可用。
3) 确定解密密钥d:
d * e = 1 molo(p - 1)*(q - 1)
根据e、p和q可以容易地计算出d。
4) 公开整数r和e,但是不公开d;
5) 将明文P (假设P是一个小于r的整数)加密为密文C,计算方法为:
C = Pe molo r
6) 将密文C解密为明文P,计算方法为:
P = Cd molo r
然而只根据r和e(不是p和q)要计算出d是不可能的。因此,任何人都可对明文进行加密,但只有授权用户(知道d)才可对密文解密。
下面举一简单的例子对上述过程进行说明,显然我们只能选取很小的数字。
例:选取p=3, q=5,则r=15,(p-1)*(q-1)=8。选取e=11(大于p和q的质数),通过d * 11 = 1 molo 8,计算出d =3。
假定明文为整数13。则密文C为
C = Pe molo r
= 1311 molo 15
= 1,792,160,394,037 molo 15
= 7
复原明文P为:
P = Cd molo r
= 73 molo 15
= 343 molo 15
= 13
因为e和d互逆,公开密钥加密方法也允许采用这样的方式对加密信息进行"签名",以便接收方能确定签名不是伪造的。假设A和B希望通过公开密钥加密方法进行数据传输,A和B分别公开加密算法和相应的密钥,但不公开解密算法和相应的密钥。A和B的加密算法分别是ECA和ECB,解密算法分别是DCA和DCB,ECA和DCA互逆,ECB和DCB互逆。若A要向B发送明文P,不是简单地发送ECB(P),而是先对P施以其解密算法DCA,再用加密算法ECB对结果加密后发送出去。密文C为:
C = ECB(DCA(P))
B收到C后,先后施以其解密算法DCB和加密算法ECA,得到明文P:
ECA(DCB(C))
= ECA(DCB(ECB(DCA(P))))
= ECA(DCA(P)) /*DCB和ECB相互抵消*/
= P /*DCB和ECB相互抵消*/
这样B就确定报文确实是从A发出的,因为只有当加密过程利用了DCA算法,用ECA才能获得P,只有A才知道DCA算法,没有人,即使是B也不能伪造A的签名。
㈡ 用C语言来实现DES加密算法(很急)两天内
DES虽然不难但是挺繁复的,代码如下,关键点都有英文解释,仔细看。各个函数的功能都可以从函数名看出来。
#include "pch.h"
#include "misc.h"
#include "des.h"
NAMESPACE_BEGIN(CryptoPP)
/* Tables defined in the Data Encryption Standard documents
* Three of these tables, the initial permutation, the final
* permutation and the expansion operator, are regular enough that
* for speed, we hard-code them. They're here for reference only.
* Also, the S and P boxes are used by a separate program, gensp.c,
* to build the combined SP box, Spbox[]. They're also here just
* for reference.
*/
#ifdef notdef
/* initial permutation IP */
static byte ip[] = {
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
/* final permutation IP^-1 */
static byte fp[] = {
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
/* expansion operation matrix */
static byte ei[] = {
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
};
/* The (in)famous S-boxes */
static byte sbox[8][64] = {
/* S1 */
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
/* S2 */
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
/* S3 */
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
/* S4 */
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
/* S5 */
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
/* S6 */
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
/* S7 */
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
/* S8 */
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
};
/* 32-bit permutation function P used on the output of the S-boxes */
static byte p32i[] = {
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
};
#endif
/* permuted choice table (key) */
static const byte pc1[] = {
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
/* number left rotations of pc1 */
static const byte totrot[] = {
1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
};
/* permuted choice key (table) */
static const byte pc2[] = {
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
/* End of DES-defined tables */
/* bit 0 is left-most in byte */
static const int bytebit[] = {
0200,0100,040,020,010,04,02,01
};
/* Set key (initialize key schele array) */
DES::DES(const byte *key, CipherDir dir)
: k(32)
{
SecByteBlock buffer(56+56+8);
byte *const pc1m=buffer; /* place to modify pc1 into */
byte *const pcr=pc1m+56; /* place to rotate pc1 into */
byte *const ks=pcr+56;
register int i,j,l;
int m;
for (j=0; j<56; j++) { /* convert pc1 to bits of key */
l=pc1[j]-1; /* integer bit location */
m = l & 07; /* find bit */
pc1m[j]=(key[l>>3] & /* find which key byte l is in */
bytebit[m]) /* and which bit of that byte */
? 1 : 0; /* and store 1-bit result */
}
for (i=0; i<16; i++) { /* key chunk for each iteration */
memset(ks,0,8); /* Clear key schele */
for (j=0; j<56; j++) /* rotate pc1 the right amount */
pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
/* rotate left and right halves independently */
for (j=0; j<48; j++){ /* select bits indivially */
/* check bit that goes to ks[j] */
if (pcr[pc2[j]-1]){
/* mask it in if it's there */
l= j % 6;
ks[j/6] |= bytebit[l] >> 2;
}
}
/* Now convert to odd/even interleaved form for use in F */
k[2*i] = ((word32)ks[0] << 24)
| ((word32)ks[2] << 16)
| ((word32)ks[4] << 8)
| ((word32)ks[6]);
k[2*i+1] = ((word32)ks[1] << 24)
| ((word32)ks[3] << 16)
| ((word32)ks[5] << 8)
| ((word32)ks[7]);
}
if (dir==DECRYPTION) // reverse key schele order
for (i=0; i<16; i+=2)
{
std::swap(k[i], k[32-2-i]);
std::swap(k[i+1], k[32-1-i]);
}
}
/* End of C code common to both versions */
/* C code only in portable version */
// Richard Outerbridge's initial permutation algorithm
/*
inline void IPERM(word32 &left, word32 &right)
{
word32 work;
work = ((left >> 4) ^ right) & 0x0f0f0f0f;
right ^= work;
left ^= work << 4;
work = ((left >> 16) ^ right) & 0xffff;
right ^= work;
left ^= work << 16;
work = ((right >> 2) ^ left) & 0x33333333;
left ^= work;
right ^= (work << 2);
work = ((right >> 8) ^ left) & 0xff00ff;
left ^= work;
right ^= (work << 8);
right = rotl(right, 1);
work = (left ^ right) & 0xaaaaaaaa;
left ^= work;
right ^= work;
left = rotl(left, 1);
}
inline void FPERM(word32 &left, word32 &right)
{
word32 work;
right = rotr(right, 1);
work = (left ^ right) & 0xaaaaaaaa;
left ^= work;
right ^= work;
left = rotr(left, 1);
work = ((left >> 8) ^ right) & 0xff00ff;
right ^= work;
left ^= work << 8;
work = ((left >> 2) ^ right) & 0x33333333;
right ^= work;
left ^= work << 2;
work = ((right >> 16) ^ left) & 0xffff;
left ^= work;
right ^= work << 16;
work = ((right >> 4) ^ left) & 0x0f0f0f0f;
left ^= work;
right ^= work << 4;
}
*/
// Wei Dai's modification to Richard Outerbridge's initial permutation
// algorithm, this one is faster if you have access to rotate instructions
// (like in MSVC)
inline void IPERM(word32 &left, word32 &right)
{
word32 work;
right = rotl(right, 4U);
work = (left ^ right) & 0xf0f0f0f0;
left ^= work;
right = rotr(right^work, 20U);
work = (left ^ right) & 0xffff0000;
left ^= work;
right = rotr(right^work, 18U);
work = (left ^ right) & 0x33333333;
left ^= work;
right = rotr(right^work, 6U);
work = (left ^ right) & 0x00ff00ff;
left ^= work;
right = rotl(right^work, 9U);
work = (left ^ right) & 0xaaaaaaaa;
left = rotl(left^work, 1U);
right ^= work;
}
inline void FPERM(word32 &left, word32 &right)
{
word32 work;
right = rotr(right, 1U);
work = (left ^ right) & 0xaaaaaaaa;
right ^= work;
left = rotr(left^work, 9U);
work = (left ^ right) & 0x00ff00ff;
right ^= work;
left = rotl(left^work, 6U);
work = (left ^ right) & 0x33333333;
right ^= work;
left = rotl(left^work, 18U);
work = (left ^ right) & 0xffff0000;
right ^= work;
left = rotl(left^work, 20U);
work = (left ^ right) & 0xf0f0f0f0;
right ^= work;
left = rotr(left^work, 4U);
}
// Encrypt or decrypt a block of data in ECB mode
void DES::ProcessBlock(const byte *inBlock, byte * outBlock) const
{
word32 l,r,work;
#ifdef IS_LITTLE_ENDIAN
l = byteReverse(*(word32 *)inBlock);
r = byteReverse(*(word32 *)(inBlock+4));
#else
l = *(word32 *)inBlock;
r = *(word32 *)(inBlock+4);
#endif
IPERM(l,r);
const word32 *kptr=k;
for (unsigned i=0; i<8; i++)
{
work = rotr(r, 4U) ^ kptr[4*i+0];
l ^= Spbox[6][(work) & 0x3f]
^ Spbox[4][(work >> 8) & 0x3f]
^ Spbox[2][(work >> 16) & 0x3f]
^ Spbox[0][(work >> 24) & 0x3f];
work = r ^ kptr[4*i+1];
l ^= Spbox[7][(work) & 0x3f]
^ Spbox[5][(work >> 8) & 0x3f]
^ Spbox[3][(work >> 16) & 0x3f]
^ Spbox[1][(work >> 24) & 0x3f];
work = rotr(l, 4U) ^ kptr[4*i+2];
r ^= Spbox[6][(work) & 0x3f]
^ Spbox[4][(work >> 8) & 0x3f]
^ Spbox[2][(work >> 16) & 0x3f]
^ Spbox[0][(work >> 24) & 0x3f];
work = l ^ kptr[4*i+3];
r ^= Spbox[7][(work) & 0x3f]
^ Spbox[5][(work >> 8) & 0x3f]
^ Spbox[3][(work >> 16) & 0x3f]
^ Spbox[1][(work >> 24) & 0x3f];
}
FPERM(l,r);
#ifdef IS_LITTLE_ENDIAN
*(word32 *)outBlock = byteReverse(r);
*(word32 *)(outBlock+4) = byteReverse(l);
#else
*(word32 *)outBlock = r;
*(word32 *)(outBlock+4) = l;
#endif
}
void DES_EDE_Encryption::ProcessBlock(byte *inoutBlock) const
{
e.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
}
void DES_EDE_Encryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
e.ProcessBlock(inBlock, outBlock);
d.ProcessBlock(outBlock);
e.ProcessBlock(outBlock);
}
void DES_EDE_Decryption::ProcessBlock(byte *inoutBlock) const
{
d.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
}
void DES_EDE_Decryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
d.ProcessBlock(inBlock, outBlock);
e.ProcessBlock(outBlock);
d.ProcessBlock(outBlock);
}
void TripleDES_Encryption::ProcessBlock(byte *inoutBlock) const
{
e1.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
e2.ProcessBlock(inoutBlock);
}
void TripleDES_Encryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
e1.ProcessBlock(inBlock, outBlock);
d.ProcessBlock(outBlock);
e2.ProcessBlock(outBlock);
}
void TripleDES_Decryption::ProcessBlock(byte *inoutBlock) const
{
d1.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
d2.ProcessBlock(inoutBlock);
}
void TripleDES_Decryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
d1.ProcessBlock(inBlock, outBlock);
e.ProcessBlock(outBlock);
d2.ProcessBlock(outBlock);
}
㈢ 数据加密提纲
数据加密
考虑到用户可能试图旁路系统的情况,如物理地取走数据库,在通讯线路上窃听。对这样的威胁最有效的解决方法
就是数据加密,即以加密格式存储和传输敏感数据。
数据加密的术语有:明文,即原始的或未加密的数据。通过加密算法对其进行加密,加密算法的输入信息为明文和
密钥;密文,明文加密后的格式,是加密算法的输出信息。加密算法是公开的,而密钥则是不公开的。密文,不应为无
密钥的用户理解,用于数据的存储以及传输。
例:明文为字符串:
AS KINGFISHERS CATCH FIRE
(为简便起见,假定所处理的数据字符仅为大写字母和空格符)。假定密钥为字符串:
ELIOT
加密算法为:
1) 将明文划分成多个密钥字符串长度大小的块(空格符以"+"表示)
AS+KI NGFIS HERS+ CATCH +FIRE
2) 用00~26范围的整数取代明文的每个字符,空格符=00,A=01,...,Z=26:
0119001109 1407060919 0805181900 0301200308 0006091805
3) 与步骤2一样对密钥的每个字符进行取代:
0512091520
4) 对明文的每个块,将其每个字符用对应的整数编码与密钥中相应位置的字符的整数编码的和模27后的值取代:
5) 将步骤4的结果中的整数编码再用其等价字符替换:
FDIZB SSOXL MQ+GT HMBRA ERRFY
如果给出密钥,该例的解密过程很简单。问题是对于一个恶意攻击者来说,在不知道密钥的情况下,利用相匹配的
明文和密文获得密钥究竟有多困难?对于上面的简单例子,答案是相当容易的,不是一般的容易,但是,复杂的加密模
式同样很容易设计出。理想的情况是采用的加密模式使得攻击者为了破解所付出的代价应远远超过其所获得的利益。实
际上,该目的适用于所有的安全性措施。这种加密模式的可接受的最终目标是:即使是该模式的发明者也无法通过相匹
配的明文和密文获得密钥,从而也无法破解密文。
1. 数据加密标准
传统加密方法有两种,替换和置换。上面的例子采用的就是替换的方法:使用密钥将明文中的每一个字符转换为密
文中的一个字符。而置换仅将明文的字符按不同的顺序重新排列。单独使用这两种方法的任意一种都是不够安全的,但
是将这两种方法结合起来就能提供相当高的安全程度。数据加密标准(Data Encryption Standard,简称DES)就采用了
这种结合算法,它由IBM制定,并在1977年成为美国官方加密标准。
DES的工作原理为:将明文分割成许多64位大小的块,每个块用64位密钥进行加密,实际上,密钥由56位数据位和8
位奇偶校验位组成,因此只有256个可能的密码而不是264个。每块先用初始置换方法进行加密,再连续进行16次复杂的
替换,最后再对其施用初始置换的逆。第i步的替换并不是直接利用原始的密钥K,而是由K与i计算出的密钥Ki。
DES具有这样的特性,其解密算法与加密算法相同,除了密钥Ki的施加顺序相反以外。
2. 公开密钥加密
多年来,许多人都认为DES并不是真的很安全。事实上,即使不采用智能的方法,随着快速、高度并行的处理器的出
现,强制破解DES也是可能的。"公开密钥"加密方法使得DES以及类似的传统加密技术过时了。公开密钥加密方法中,加密
算法和加密密钥都是公开的,任何人都可将明文转换成密文。但是相应的解密密钥是保密的(公开密钥方法包括两个密钥,
分别用于加密和解密),而且无法从加密密钥推导出,因此,即使是加密者若未被授权也无法执行相应的解密。
公开密钥加密思想最初是由Diffie和Hellman提出的,最着名的是Rivest、Shamir以及Adleman提出的,现在通常称为
RSA(以三个发明者的首位字母命名)的方法,该方法基于下面的两个事实:
1) 已有确定一个数是不是质数的快速算法;
2) 尚未找到确定一个合数的质因子的快速算法。
RSA方法的工作原理如下:
1) 任意选取两个不同的大质数p和q,计算乘积r=p*q;
2) 任意选取一个大整数e,e与(p-1)*(q-1)互质,整数e用做加密密钥。注意:e的选取是很容易的,例如,所有大
于p和q的质数都可用。
3) 确定解密密钥d:
d * e = 1 molo(p - 1)*(q - 1)
根据e、p和q可以容易地计算出d。
4) 公开整数r和e,但是不公开d;
5) 将明文P (假设P是一个小于r的整数)加密为密文C,计算方法为:
C = Pe molo r
6) 将密文C解密为明文P,计算方法为:
P = Cd molo r
然而只根据r和e(不是p和q)要计算出d是不可能的。因此,任何人都可对明文进行加密,但只有授权用户(知道d)
才可对密文解密。
下面举一简单的例子对上述过程进行说明,显然我们只能选取很小的数字。
例:选取p=3, q=5,则r=15,(p-1)*(q-1)=8。选取e=11(大于p和q的质数),通过d * 11 = 1 molo 8,
计算出d =3。
假定明文为整数13。则密文C为
C = Pe molo r
= 1311 molo 15
= 1,792,160,394,037 molo 15
= 7
复原明文P为:
P = Cd molo r
= 73 molo 15
= 343 molo 15
= 13
因为e和d互逆,公开密钥加密方法也允许采用这样的方式对加密信息进行"签名",以便接收方能确定签名不是伪造的。
假设A和B希望通过公开密钥加密方法进行数据传输,A和B分别公开加密算法和相应的密钥,但不公开解密算法和相应的密钥。
A和B的加密算法分别是ECA和ECB,解密算法分别是DCA和DCB,ECA和DCA互逆,ECB和DCB互逆。
若A要向B发送明文P,不是简单地发送ECB(P),而是先对P施以其解密算法DCA,再用加密算法ECB对结果加密后发送出去。
密文C为:
C = ECB(DCA(P))
B收到C后,先后施以其解密算法DCB和加密算法ECA,得到明文P:
ECA(DCB(C))
= ECA(DCB(ECB(DCA(P))))
= ECA(DCA(P)) /*DCB和ECB相互抵消*/
= P /*DCB和ECB相互抵消*/
这样B就确定报文确实是从A发出的,因为只有当加密过程利用了DCA算法,用ECA才能获得P,只有A才知道DCA算法,没
有人,即使是B也不能伪造A的签名。
㈣ MD5 DES 的加密是什么意思
MD5的加密程序
package org.lxh.myzngt.util;
public class MD5Code {
/*
* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static
* final是表示了只读,切能在同一个进程空间内的多个 Instance间共享
*/
static final int S11 = 7;
static final int S12 = 12;
static final int S13 = 17;
static final int S14 = 22;
static final int S21 = 5;
static final int S22 = 9;
static final int S23 = 14;
static final int S24 = 20;
static final int S31 = 4;
static final int S32 = 11;
static final int S33 = 16;
static final int S34 = 23;
static final int S41 = 6;
static final int S42 = 10;
static final int S43 = 15;
static final int S44 = 21;
static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };
/*
* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 被定义到MD5_CTX结构中
*/
private long[] state = new long[4];// state (ABCD)
private long[] count = new long[2];// number of bits, molo 2^64 (lsb
// first)
private byte[] buffer = new byte[64]; // input buffer
/*
* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示.
*/
public String digestHexStr;
/*
* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.
*/
private byte[] digest = new byte[16];
/*
* getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串
* 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.
*/
public String getMD5ofStr(String inbuf) {
md5Init();
md5Update(inbuf.getBytes(), inbuf.length());
md5Final();
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 这是MD5这个类的标准构造函数,javaBean要求有一个public的并且没有参数的构造函数
public MD5Code() {
md5Init();
return;
}
/* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */
private void md5Init() {
count[0] = 0L;
count[1] = 0L;
// /* Load magic initialization constants.
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
return;
}
/*
* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是
* 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。
*/
private long F(long x, long y, long z) {
return (x & y) | ((~x) & z);
}
private long G(long x, long y, long z) {
return (x & z) | (y & (~z));
}
private long H(long x, long y, long z) {
return x ^ y ^ z;
}
private long I(long x, long y, long z) {
return y ^ (x | (~z));
}
/*
* FF,GG,HH和II将调用F,G,H,I进行近一步变换 FF, GG, HH, and II transformations for
* rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent
* recomputation.
*/
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
/*
* md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个
* 函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的
*/
private void md5Update(byte[] inbuf, int inputLen) {
int i, index, partLen;
byte[] block = new byte[64];
index = (int) (count[0] >>> 3) & 0x3F;
// /* Update number of bits */
if ((count[0] += (inputLen << 3)) < (inputLen << 3))
count[1]++;
count[1] += (inputLen >>> 29);
partLen = 64 - index;
// Transform as many times as possible.
if (inputLen >= partLen) {
md5Memcpy(buffer, inbuf, index, 0, partLen);
md5Transform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
md5Memcpy(block, inbuf, 0, i, 64);
md5Transform(block);
}
index = 0;
} else
i = 0;
// /* Buffer remaining input */
md5Memcpy(buffer, inbuf, index, i, inputLen - i);
}
/*
* md5Final整理和填写输出结果
*/
private void md5Final() {
byte[] bits = new byte[8];
int index, padLen;
// /* Save number of bits */
Encode(bits, count, 8);
// /* Pad out to 56 mod 64.
index = (int) (count[0] >>> 3) & 0x3f;
padLen = (index < 56) ? (56 - index) : (120 - index);
md5Update(PADDING, padLen);
// /* Append length (before padding) */
md5Update(bits, 8);
// /* Store state in digest */
Encode(digest, state, 16);
}
/*
* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的
* 字节拷贝到output的outpos位置开始
*/
private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
int len) {
int i;
for (i = 0; i < len; i++)
output[outpos + i] = input[inpos + i];
}
/*
* md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节
*/
private void md5Transform(byte block[]) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
/*
* Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途
*/
private void Encode(byte[] output, long[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (byte) (input[i] & 0xffL);
output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
}
}
/*
* Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
* 只合成低32bit,高32bit清零,以适应原始C实现的用途
*/
private void Decode(long[] output, byte[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
| (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
return;
}
/*
* b2iu是我写的一个把byte按照不考虑正负号的原则的”升位”程序,因为java没有unsigned运算
*/
public static long b2iu(byte b) {
return b < 0 ? b & 0x7F + 128 : b;
}
/*
* byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,
* 因为java中的byte的toString无法实现这一点,我们又没有C语言中的 sprintf(outbuf,"%02X",ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = Digit[(ib >>> 4) & 0X0F];
ob[1] = Digit[ib & 0X0F];
String s = new String(ob);
return s;
}
}
㈤ 如何为配置文件加密
在web.config或app.config文件里我们经常会存储一些敏感信息,比如connectionStrings或者appSettings,比如像下面的文件。
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="MyNwConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;"/>
</connectionStrings>
<appSettings>
<add key="User" value="myUsername"/>
<add key="Password" value="myPassword"/>
</appSettings>
</configuration>
using System;
using System.Configuration;
namespace WebConfigEncryptTest
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string user = ConfigurationManager.AppSettings.Get("User");
string password = ConfigurationManager.AppSettings.Get("Password");
string connectionString = ConfigurationManager.ConnectionStrings["MyNwConnectionString"].ConnectionString;
}
}
}
(一)加密文件可以使用的Provider
.NET为我们提供了一个工具aspnet_regiis.exe来对web.config文件中的敏感信息进行加密(app.config文件可以先改名为web.config,加密后再改回app.config)。你可以使用两个provider中的一个来进行加密:
System.Configuration.:在System.Configuration.dll中,使用Windows DPAPI(Data Protection API)来进行加密,密钥存在Windows Local Security Authority(LSA)中。注意:当使用时,加密文件所使用的帐号需要与运行web application的帐号相同,否则web application无法解密加密的内容。
System.Configuration.:在System.Configuration.dll中,使用RSA算法来进行加密(RSA算法是非对称加密,参见《对称加密与非对称加密 》),公钥存放在config文件当中,只有加密的计算机有密钥。通常是默认的缺省provider。
(二)加密文件的命令
加密web.config文件可以使用:
aspnet_regiis -pef section web-app-physical-dir
Encrypt the configuration section. Optional arguments:
[-prov provider] Use this provider to encrypt.
比如运行下面的命令就会分别对connectionStrings和appSettings中的信息进行加密:
aspnet_regiis.exe -pef "connectionStrings" "C:\myweb\HelloService"
aspnet_regiis.exe -pef "appSettings" "C:\myweb\HelloService"
加密后的web.config文件变成:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation targetFramework="4.0" />
</system.web>
<connectionStrings configProtectionProvider="">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>E2fO9C0TJVxImLYQZza+fCQdDbTpNh/kOKLRsK6zcFjkgtUCl6SnMViuu/2G1NVTxqXyEWYwyK6AiCZA+feeG/+f2EIimP7LJI+JRZVerI4MU6Ke3wxm2S/ATc73/W6eg9808f4//JB0kso0kGJ9i+==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>I1DWG11Iz/rq+NC9C/21B3Q22J9+//JW1oCvAGs5tHrZU5+vgvm0yCmSuCWZbXva+iv9J35EQqs58pq+hwVo1hg1dffpGCBykaXGl5VX3TIGc=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
<appSettings configProtectionProvider="">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>//SuBvV3D2kxhHaYGFaPuvYgsyOLf3+aYR3O/uh/+/iSANzAWoC+==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>5W2KhG/oETLUDptobcOM52x1qD/g9A0By/wcGXI+///w=</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
</configuration>
是默认的缺省provider,如果想使用,可以用-prov参数指明:
aspnet_regiis.exe -pef "connectionStrings" "C:\myweb\HelloService" -prov ""
aspnet_regiis.exe -pef "appSettings" "C:\myweb\HelloService" -prov ""
加密配置文件后,源程序不需要做任何改动。如果要修改或添加新的配置信息,需要先解密配置文件。不论使用哪种Provider,都只能在进行加密的计算机上对配置文件进行解密。
㈥ java des 加密 解密 密钥随机取得方法
java DES 加密 解密 生成随机密钥
举例说明:
//保存生成的key
public static void saveDesKey() {
try {
SecureRandom sr = new SecureRandom();
// 选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(sr);
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// 绝对路径
String fileName = "d:/DesKey.xml";
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密钥
Key key = kg.generateKey();
oos.writeObject(key);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取生成的key
public static Key getKey() {
Key kp = null;
try {
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);
// 绝对路径
String fileName = "d:/DesKey.xml";
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (Key) oos.readObject();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
return kp;
}
//加密开始
public static void encrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}
//解密开始
public static void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
}
//des加密主方法
public class DES {
public static void main(String[] args) throws Exception {
Encrypt.saveDesKey();
System.out.println("生成key");
Encrypt.getKey();
System.out.println("获取key");
Encrypt.encrypt("d:\\hello.txt", "d:\\encrypt.txt");
System.out.println("加密");
Encrypt.decrypt("d:\\encrypt.txt", "d:\\decrypt.txt");
System.out.println("解密");
}
以上方法亲测可用。
㈦ 苹果越狱后 文件夹加密ISIOCKED 我没有设置密码误锁了 什么文件都不开了怎么办
平刷一次就好
㈧ des加密算法(c/c++)
des.h文件:
#ifndefCRYPTOPP_DES_H
#defineCRYPTOPP_DES_H
#include"cryptlib.h"
#include"misc.h"
NAMESPACE_BEGIN(CryptoPP)
classDES:publicBlockTransformation
{
public:
DES(constbyte*userKey,CipherDir);
voidProcessBlock(constbyte*inBlock,byte*outBlock)const;
voidProcessBlock(byte*inoutBlock)const
{DES::ProcessBlock(inoutBlock,inoutBlock);}
enum{KEYLENGTH=8,BLOCKSIZE=8};
unsignedintBlockSize()const{returnBLOCKSIZE;}
protected:
staticconstword32Spbox[8][64];
SecBlock<word32>k;
};
classDESEncryption:publicDES
{
public:
DESEncryption(constbyte*userKey)
:DES(userKey,ENCRYPTION){}
};
classDESDecryption:publicDES
{
public:
DESDecryption(constbyte*userKey)
:DES(userKey,DECRYPTION){}
};
classDES_EDE_Encryption:publicBlockTransformation
{
public:
DES_EDE_Encryption(constbyte*userKey)
:e(userKey,ENCRYPTION),d(userKey+DES::KEYLENGTH,DECRYPTION){}
voidProcessBlock(constbyte*inBlock,byte*outBlock)const;
voidProcessBlock(byte*inoutBlock)const;
enum{KEYLENGTH=16,BLOCKSIZE=8};
unsignedintBlockSize()const{returnBLOCKSIZE;}
private:
DESe,d;
};
classDES_EDE_Decryption:publicBlockTransformation
{
public:
DES_EDE_Decryption(constbyte*userKey)
:d(userKey,DECRYPTION),e(userKey+DES::KEYLENGTH,ENCRYPTION){}
voidProcessBlock(constbyte*inBlock,byte*outBlock)const;
voidProcessBlock(byte*inoutBlock)const;
enum{KEYLENGTH=16,BLOCKSIZE=8};
unsignedintBlockSize()const{returnBLOCKSIZE;}
private:
DESd,e;
};
classTripleDES_Encryption:publicBlockTransformation
{
public:
TripleDES_Encryption(constbyte*userKey)
:e1(userKey,ENCRYPTION),d(userKey+DES::KEYLENGTH,DECRYPTION),
e2(userKey+2*DES::KEYLENGTH,ENCRYPTION){}
voidProcessBlock(constbyte*inBlock,byte*outBlock)const;
voidProcessBlock(byte*inoutBlock)const;
enum{KEYLENGTH=24,BLOCKSIZE=8};
unsignedintBlockSize()const{returnBLOCKSIZE;}
private:
DESe1,d,e2;
};
classTripleDES_Decryption:publicBlockTransformation
{
public:
TripleDES_Decryption(constbyte*userKey)
:d1(userKey+2*DES::KEYLENGTH,DECRYPTION),e(userKey+DES::KEYLENGTH,ENCRYPTION),
d2(userKey,DECRYPTION){}
voidProcessBlock(constbyte*inBlock,byte*outBlock)const;
voidProcessBlock(byte*inoutBlock)const;
enum{KEYLENGTH=24,BLOCKSIZE=8};
unsignedintBlockSize()const{returnBLOCKSIZE;}
private:
DESd1,e,d2;
};
NAMESPACE_END
#endif
des.cpp文件:
//des.cpp-modifiedbyWeiDaifrom:
/*
*
*circa1987,'s1977
*publicdomaincode.,but
*theactualencrypt/
*Outerbridge'sDEScodeasprintedinSchneier's"AppliedCryptography."
*
*Thiscodeisinthepublicdomain.Iwouldappreciatebugreportsand
*enhancements.
*
*PhilKarnKA9Q,[email protected],August1994.
*/
#include"pch.h"
#include"misc.h"
#include"des.h"
NAMESPACE_BEGIN(CryptoPP)
/*
*Threeofthesetables,theinitialpermutation,thefinal
*,areregularenoughthat
*forspeed,wehard-codethem.They'rehereforreferenceonly.
*Also,,gensp.c,
*tobuildthecombinedSPbox,Spbox[].They'realsoherejust
*forreference.
*/
#ifdefnotdef
/*initialpermutationIP*/
staticbyteip[]={
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7
};
/*finalpermutationIP^-1*/
staticbytefp[]={
40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25
};
/*expansionoperationmatrix*/
staticbyteei[]={
32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1
};
/*The(in)famousS-boxes*/
staticbytesbox[8][64]={
/*S1*/
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,
/*S2*/
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,
/*S3*/
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,
/*S4*/
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,
/*S5*/
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,
/*S6*/
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,
/*S7*/
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,
/*S8*/
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
};
/*32--boxes*/
staticbytep32i[]={
16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25
};
#endif
/*permutedchoicetable(key)*/
staticconstbytepc1[]={
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};
/*numberleftrotationsofpc1*/
staticconstbytetotrot[]={
1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
};
/*permutedchoicekey(table)*/
staticconstbytepc2[]={
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};
/*EndofDES-definedtables*/
/*bit0isleft-mostinbyte*/
staticconstintbytebit[]={
0200,0100,040,020,010,04,02,01
};
/*Setkey(initializekeyschelearray)*/
DES::DES(constbyte*key,CipherDirdir)
:k(32)
{
SecByteBlockbuffer(56+56+8);
byte*constpc1m=buffer;/*placetomodifypc1into*/
byte*constpcr=pc1m+56;/*placetorotatepc1into*/
byte*constks=pcr+56;
registerinti,j,l;
intm;
for(j=0;j<56;j++){/*convertpc1tobitsofkey*/
l=pc1[j]-1;/*integerbitlocation*/
m=l&07;/*findbit*/
pc1m[j]=(key[l>>3]&/*findwhichkeybytelisin*/
bytebit[m])/*andwhichbitofthatbyte*/
?1:0;/*andstore1-bitresult*/
}
for(i=0;i<16;i++){/*keychunkforeachiteration*/
memset(ks,0,8);/*Clearkeyschele*/
for(j=0;j<56;j++)/*rotatepc1therightamount*/
pcr[j]=pc1m[(l=j+totrot[i])<(j<28?28:56)?l:l-28];
/**/
for(j=0;j<48;j++){/*selectbitsindivially*/
/*checkbitthatgoestoks[j]*/
if(pcr[pc2[j]-1]){
/*maskitinifit'sthere*/
l=j%6;
ks[j/6]|=bytebit[l]>>2;
}
}
/*Nowconverttoodd/eveninterleavedformforuseinF*/
k[2*i]=((word32)ks[0]<<24)
|((word32)ks[2]<<16)
|((word32)ks[4]<<8)
|((word32)ks[6]);
k[2*i+1]=((word32)ks[1]<<24)
|((word32)ks[3]<<16)
|((word32)ks[5]<<8)
|((word32)ks[7]);
}
if(dir==DECRYPTION)//reversekeyscheleorder
for(i=0;i<16;i+=2)
{
std::swap(k[i],k[32-2-i]);
std::swap(k[i+1],k[32-1-i]);
}
}
/**/
/*Ccodeonlyinportableversion*/
//RichardOuterbridge'sinitialpermutationalgorithm
/*
inlinevoidIPERM(word32&left,word32&right)
{
word32work;
work=((left>>4)^right)&0x0f0f0f0f;
right^=work;
left^=work<<4;
work=((left>>16)^right)&0xffff;
right^=work;
left^=work<<16;
work=((right>>2)^left)&0x33333333;
left^=work;
right^=(work<<2);
work=((right>>8)^left)&0xff00ff;
left^=work;
right^=(work<<8);
right=rotl(right,1);
work=(left^right)&0xaaaaaaaa;
left^=work;
right^=work;
left=rotl(left,1);
}
inlinevoidFPERM(word32&left,word32&right)
{
word32work;
right=rotr(right,1);
work=(left^right)&0xaaaaaaaa;
left^=work;
right^=work;
left=rotr(left,1);
work=((left>>8)^right)&0xff00ff;
right^=work;
left^=work<<8;
work=((left>>2)^right)&0x33333333;
right^=work;
left^=work<<2;
work=((right>>16)^left)&0xffff;
left^=work;
right^=work<<16;
work=((right>>4)^left)&0x0f0f0f0f;
left^=work;
right^=work<<4;
}
*/
//WeiDai''sinitialpermutation
//algorithm,
//(likeinMSVC)
inlinevoidIPERM(word32&left,word32&right)
{
word32work;
right=rotl(right,4U);
work=(left^right)&0xf0f0f0f0;
left^=work;
right=rotr(right^work,20U);
work=(left^right)&0xffff0000;
left^=work;
right=rotr(right^work,18U);
work=(left^right)&0x33333333;
left^=work;
right=rotr(right^work,6U);
work=(left^right)&0x00ff00ff;
left^=work;
right=rotl(right^work,9U);
work=(left^right)&0xaaaaaaaa;
left=rotl(left^work,1U);
right^=work;
}
inlinevoidFPERM(word32&left,word32&right)
{
word32work;
right=rotr(right,1U);
work=(left^right)&0xaaaaaaaa;
right^=work;
left=rotr(left^work,9U);
work=(left^right)&0x00ff00ff;
right^=work;
left=rotl(left^work,6U);
work=(left^right)&0x33333333;
right^=work;
left=rotl(left^work,18U);
work=(left^right)&0xffff0000;
right^=work;
left=rotl(left^work,20U);
work=(left^right)&0xf0f0f0f0;
right^=work;
left=rotr(left^work,4U);
}
//
voidDES::ProcessBlock(constbyte*inBlock,byte*outBlock)const
{
word32l,r,work;
#ifdefIS_LITTLE_ENDIAN
l=byteReverse(*(word32*)inBlock);
r=byteReverse(*(word32*)(inBlock+4));
#else
l=*(word32*)inBlock;
r=*(word32*)(inBlock+4);
#endif
IPERM(l,r);
constword32*kptr=k;
for(unsignedi=0;i<8;i++)
{
work=rotr(r,4U)^kptr[4*i+0];
l^=Spbox[6][(work)&0x3f]
^Spbox[4][(work>>8)&0x3f]
^Spbox[2][(work>>16)&0x3f]
^Spbox[0][(work>>24)&0x3f];
work=r^kptr[4*i+1];
l^=Spbox[7][(work)&0x3f]
^Spbox[5][(work>>8)&0x3f]
^Spbox[3][(work>>16)&0x3f]
^Spbox[1][(work>>24)&0x3f];
work=rotr(l,4U)^kptr[4*i+2];
r^=Spbox[6][(work)&0x3f]
^Spbox[4][(work>>8)&0x3f]
^Spbox[2][(work>>16)&0x3f]
^Spbox[0][(work>>24)&0x3f];
work=l^kptr[4*i+3];
r^=Spbox[7][(work)&0x3f]
^Spbox[5][(work>>8)&0x3f]
^Spbox[3][(work>>16)&0x3f]
^Spbox[1][(work>>24)&0x3f];
}
FPERM(l,r);
#ifdefIS_LITTLE_ENDIAN
*(word32*)outBlock=byteReverse(r);
*(word32*)(outBlock+4)=byteReverse(l);
#else
*(word32*)outBlock=r;
*(word32*)(outBlock+4)=l;
#endif
}
voidDES_EDE_Encryption::ProcessBlock(byte*inoutBlock)const
{
e.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
}
voidDES_EDE_Encryption::ProcessBlock(constbyte*inBlock,byte*outBlock)const
{
e.ProcessBlock(inBlock,outBlock);
d.ProcessBlock(outBlock);
e.ProcessBlock(outBlock);
}
voidDES_EDE_Decryption::ProcessBlock(byte*inoutBlock)const
{
d.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
}
voidDES_EDE_Decryption::ProcessBlock(constbyte*inBlock,byte*outBlock)const
{
d.ProcessBlock(inBlock,outBlock);
e.ProcessBlock(outBlock);
d.ProcessBlock(outBlock);
}
voidTripleDES_Encryption::ProcessBlock(byte*inoutBlock)const
{
e1.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
e2.ProcessBlock(inoutBlock);
}
voidTripleDES_Encryption::ProcessBlock(constbyte*inBlock,byte*outBlock)const
{
e1.ProcessBlock(inBlock,outBlock);
d.ProcessBlock(outBlock);
e2.ProcessBlock(outBlock);
}
voidTripleDES_Decryption::ProcessBlock(byte*inoutBlock)const
{
d1.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
d2.ProcessBlock(inoutBlock);
}
voidTripleDES_Decryption::ProcessBlock(constbyte*inBlock,byte*outBlock)const
{
d1.ProcessBlock(inBlock,outBlock);
e.ProcessBlock(outBlock);
d2.ProcessBlock(outBlock);
}
NAMESPACE_END
程序运行如下:
㈨ 高分求java的RSA 和IDEA 加密解密算法
RSA算法非常简单,概述如下:
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足e<t并且e与t互素(就是最大公因数为1)
取d*e%t==1
这样最终得到三个数: n d e
设消息为数M (M <n)
设c=(M**d)%n就得到了加密后的消息c
设m=(c**e)%n则 m == M,从而完成对c的解密。
注:**表示次方,上面两式中的d和e可以互换。
在对称加密中:
n d两个数构成公钥,可以告诉别人;
n e两个数构成私钥,e自己保留,不让任何人知道。
给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。
别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。
rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解
从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法
求得d。
<二>实践
接下来我们来一个实践,看看实际的操作:
找两个素数:
p=47
q=59
这样
n=p*q=2773
t=(p-1)*(q-1)=2668
取e=63,满足e<t并且e和t互素
用perl简单穷举可以获得满主 e*d%t ==1的数d:
C:\Temp>perl -e "foreach $i (1..9999){ print($i),last if $i*63%2668==1 }"
847
即d=847
最终我们获得关键的
n=2773
d=847
e=63
取消息M=244我们看看
加密:
c=M**d%n = 244**847%2773
用perl的大数计算来算一下:
C:\Temp>perl -Mbigint -e "print 244**847%2773"
465
即用d对M加密后获得加密信息c=465
解密:
我们可以用e来对加密后的c进行解密,还原M:
m=c**e%n=465**63%2773 :
C:\Temp>perl -Mbigint -e "print 465**63%2773"
244
即用e对c解密后获得m=244 , 该值和原始信息M相等。
<三>字符串加密
把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了。
每次取字符串中的一个字符的ascii值作为M进行计算,其输出为加密后16进制
的数的字符串形式,按3字节表示,如01F
代码如下:
#!/usr/bin/perl -w
#RSA 计算过程学习程序编写的测试程序
#watercloud 2003-8-12
#
use strict;
use Math::BigInt;
my %RSA_CORE = (n=>2773,e=>63,d=>847); #p=47,q=59
my $N=new Math::BigInt($RSA_CORE{n});
my $E=new Math::BigInt($RSA_CORE{e});
my $D=new Math::BigInt($RSA_CORE{d});
print "N=$N D=$D E=$E\n";
sub RSA_ENCRYPT
{
my $r_mess = shift @_;
my ($c,$i,$M,$C,$cmess);
for($i=0;$i < length($$r_mess);$i++)
{
$c=ord(substr($$r_mess,$i,1));
$M=Math::BigInt->new($c);
$C=$M->(); $C->bmodpow($D,$N);
$c=sprintf "%03X",$C;
$cmess.=$c;
}
return \$cmess;
}
sub RSA_DECRYPT
{
my $r_mess = shift @_;
my ($c,$i,$M,$C,$dmess);
for($i=0;$i < length($$r_mess);$i+=3)
{
$c=substr($$r_mess,$i,3);
$c=hex($c);
$M=Math::BigInt->new($c);
$C=$M->(); $C->bmodpow($E,$N);
$c=chr($C);
$dmess.=$c;
}
return \$dmess;
}
my $mess="RSA 娃哈哈哈~~~";
$mess=$ARGV[0] if @ARGV >= 1;
print "原始串:",$mess,"\n";
my $r_cmess = RSA_ENCRYPT(\$mess);
print "加密串:",$$r_cmess,"\n";
my $r_dmess = RSA_DECRYPT($r_cmess);
print "解密串:",$$r_dmess,"\n";
#EOF
测试一下:
C:\Temp>perl rsa-test.pl
N=2773 D=847 E=63
原始串:RSA 娃哈哈哈~~~
加密串:
解密串:RSA 娃哈哈哈~~~
C:\Temp>perl rsa-test.pl 安全焦点(xfocus)
N=2773 D=847 E=63
原始串:安全焦点(xfocus)
加密串:
解密串:安全焦点(xfocus)
<四>提高
前面已经提到,rsa的安全来源于n足够大,我们测试中使用的n是非常小的,根本不能保障安全性,
我们可以通过RSAKit、RSATool之类的工具获得足够大的N 及D E。
通过工具,我们获得1024位的N及D E来测试一下:
n=EC3A85F5005D
4C2013433B383B
A50E114705D7E2
BC511951
d=0x10001
e=DD28C523C2995
47B77324E66AFF2
789BD782A592D2B
1965
设原始信息
M=
完成这么大数字的计算依赖于大数运算库,用perl来运算非常简单:
A) 用d对M进行加密如下:
c=M**d%n :
C:\Temp>perl -Mbigint -e " $x=Math::BigInt->bmodpow(0x11111111111122222222222233
333333333, 0x10001,
D55EDBC4F0
6E37108DD6
);print $x->as_hex"
b73d2576bd
47715caa6b
d59ea89b91
f1834580c3f6d90898
即用d对M加密后信息为:
c=b73d2576bd
47715caa6b
d59ea89b91
f1834580c3f6d90898
B) 用e对c进行解密如下:
m=c**e%n :
C:\Temp>perl -Mbigint -e " $x=Math::BigInt->bmodpow(0x17b287be418c69ecd7c39227ab
5aa1d99ef3
0cb4764414
, 0xE760A
3C29954C5D
7324E66AFF
2789BD782A
592D2B1965, CD15F90
4F017F9CCF
DD60438941
);print $x->as_hex"
(我的P4 1.6G的机器上计算了约5秒钟)
得到用e解密后的m= == M
C) RSA通常的实现
RSA简洁幽雅,但计算速度比较慢,通常加密中并不是直接使用RSA 来对所有的信息进行加密,
最常见的情况是随机产生一个对称加密的密钥,然后使用对称加密算法对信息加密,之后用
RSA对刚才的加密密钥进行加密。
最后需要说明的是,当前小于1024位的N已经被证明是不安全的
自己使用中不要使用小于1024位的RSA,最好使用2048位的。
----------------------------------------------------------
一个简单的RSA算法实现JAVA源代码:
filename:RSA.java
/*
* Created on Mar 3, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.math.BigInteger;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;
/**
* @author Steve
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class RSA {
/**
* BigInteger.ZERO
*/
private static final BigInteger ZERO = BigInteger.ZERO;
/**
* BigInteger.ONE
*/
private static final BigInteger ONE = BigInteger.ONE;
/**
* Pseudo BigInteger.TWO
*/
private static final BigInteger TWO = new BigInteger("2");
private BigInteger myKey;
private BigInteger myMod;
private int blockSize;
public RSA (BigInteger key, BigInteger n, int b) {
myKey = key;
myMod = n;
blockSize = b;
}
public void encodeFile (String filename) {
byte[] bytes = new byte[blockSize / 8 + 1];
byte[] temp;
int tempLen;
InputStream is = null;
FileWriter writer = null;
try {
is = new FileInputStream(filename);
writer = new FileWriter(filename + ".enc");
}
catch (FileNotFoundException e1){
System.out.println("File not found: " + filename);
}
catch (IOException e1){
System.out.println("File not found: " + filename + ".enc");
}
/**
* Write encoded message to 'filename'.enc
*/
try {
while ((tempLen = is.read(bytes, 1, blockSize / 8)) > 0) {
for (int i = tempLen + 1; i < bytes.length; ++i) {
bytes[i] = 0;
}
writer.write(encodeDecode(new BigInteger(bytes)) + " ");
}
}
catch (IOException e1) {
System.out.println("error writing to file");
}
/**
* Close input stream and file writer
*/
try {
is.close();
writer.close();
}
catch (IOException e1) {
System.out.println("Error closing file.");
}
}
public void decodeFile (String filename) {
FileReader reader = null;
OutputStream os = null;
try {
reader = new FileReader(filename);
os = new FileOutputStream(filename.replaceAll(".enc", ".dec"));
}
catch (FileNotFoundException e1) {
if (reader == null)
System.out.println("File not found: " + filename);
else
System.out.println("File not found: " + filename.replaceAll(".enc", "dec"));
}
BufferedReader br = new BufferedReader(reader);
int offset;
byte[] temp, toFile;
StringTokenizer st = null;
try {
while (br.ready()) {
st = new StringTokenizer(br.readLine());
while (st.hasMoreTokens()){
toFile = encodeDecode(new BigInteger(st.nextToken())).toByteArray();
System.out.println(toFile.length + " x " + (blockSize / 8));
if (toFile[0] == 0 && toFile.length != (blockSize / 8)) {
temp = new byte[blockSize / 8];
offset = temp.length - toFile.length;
for (int i = toFile.length - 1; (i <= 0) && ((i + offset) <= 0); --i) {
temp[i + offset] = toFile[i];
}
toFile = temp;
}
/*if (toFile.length != ((blockSize / 8) + 1)){
temp = new byte[(blockSize / 8) + 1];
System.out.println(toFile.length + " x " + temp.length);
for (int i = 1; i < temp.length; i++) {
temp[i] = toFile[i - 1];
}
toFile = temp;
}
else
System.out.println(toFile.length + " " + ((blockSize / 8) + 1));*/
os.write(toFile);
}
}
}
catch (IOException e1) {
System.out.println("Something went wrong");
}
/**
* close data streams
*/
try {
os.close();
reader.close();
}
catch (IOException e1) {
System.out.println("Error closing file.");
}
}
/**
* Performs <tt>base</tt>^<sup><tt>pow</tt></sup> within the molar
* domain of <tt>mod</tt>.
*
* @param base the base to be raised
* @param pow the power to which the base will be raisded
* @param mod the molar domain over which to perform this operation
* @return <tt>base</tt>^<sup><tt>pow</tt></sup> within the molar
* domain of <tt>mod</tt>.
*/
public BigInteger encodeDecode(BigInteger base) {
BigInteger a = ONE;
BigInteger s = base;
BigInteger n = myKey;
while (!n.equals(ZERO)) {
if(!n.mod(TWO).equals(ZERO))
a = a.multiply(s).mod(myMod);
s = s.pow(2).mod(myMod);
n = n.divide(TWO);
}
return a;
}
}
在这里提供两个版本的RSA算法JAVA实现的代码下载:
1. 来自于 http://www.javafr.com/code.aspx?ID=27020 的RSA算法实现源代码包:
http://zeal.newmenbase.net/attachment/JavaFR_RSA_Source.rar
2. 来自于 http://www.ferrara.linux.it/Members/lucabariani/RSA/implementazioneRsa/ 的实现:
http://zeal.newmenbase.net/attachment/sorgentiJava.tar.gz - 源代码包
http://zeal.newmenbase.net/attachment/algoritmoRSA.jar - 编译好的jar包
另外关于RSA算法的php实现请参见文章:
php下的RSA算法实现
关于使用VB实现RSA算法的源代码下载(此程序采用了psc1算法来实现快速的RSA加密):
http://zeal.newmenbase.net/attachment/vb_PSC1_RSA.rar
RSA加密的JavaScript实现: http://www.ohdave.com/rsa/