對稱密鑰密碼算法,也叫做單鑰密碼算法或私鑰密碼算法,發(fā)送方和接收方共同擁有相同的密鑰,發(fā)送方使用這個密鑰將明文數(shù)據(jù)加密成密文,然后發(fā)送給接收方,接收方收到密文后使用這個密鑰將密文解密成明文讀取。因?yàn)檫@個密鑰既用來進(jìn)行加密數(shù)據(jù),也用來進(jìn)行解密數(shù)據(jù),所以叫做對稱密鑰,它是一種加密大量數(shù)據(jù)的加密方法。
對稱密鑰密碼體制有很多種加密算法,常用的對稱加密算法包括DES、3DES、AES、RC4、SM1(國產(chǎn))、SM4(國產(chǎn))等。這類算法的長處是加密速度快,便于硬件實(shí)現(xiàn)和大規(guī)模生產(chǎn),但由于對稱密鑰算法的加密密鑰和解密密鑰時(shí)是相同的單個密鑰,以是這類加密算法需要保障密鑰安全。所以,在使用對稱密鑰算法加密通信前,必要有安全信道來傳遞密鑰。
DES是一種分組數(shù)據(jù)加密技術(shù)(先將數(shù)據(jù)分成固定長度的小數(shù)據(jù)塊,之后進(jìn)行加密),速度較快,適用于大量數(shù)據(jù)加密,DES是應(yīng)用最廣泛的對稱密碼算法(由于計(jì)算能力的快速進(jìn)展,DES已不在被認(rèn)為是安全的);
DES 加解密算法詳解:
1 ) In Action
· 有明文M(64位) = 0123456789ABCDEF,即 M(64位) = 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
· L(32位) = 0000 0001 0010 0011 0100 0101 0110 0111
R(32位) = 1000 1001 1010 1011 1100 1101 1110 1111
· 有密鑰K(64位) = 133457799BBCDFF1,即 K(64位) = 00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001
· 其中末尾紅色標(biāo)注為奇偶校驗(yàn)位,即實(shí)際密鑰為56位。
2 )第一步:生成 16 個子鑰 (48 位 )
· 對 K 使用 PC-1(8×7)
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
· 從而,由 K(64 位 ) = 00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001
· 得到 K+(56位) = 1111000 0110011 0010101 0101111 0101010 1011001 1001111 0001111
· 進(jìn)而, C0(28位) = 1111000 0110011 0010101 0101111
D0(28位) = 0101010 1011001 1001111 0001111
· C1和D1分別為C0和D0左移1位?!?C3和D3分別為C2和D2左移2位 …
· 從而得到 ** C1D1 ~ C16D16 :
· C1 = 1110000110011001010101011111
D1 = 1010101011001100111100011110
· C2 = 1100001100110010101010111111
D2 = 0101010110011001111000111101
· C3 = 0000110011001010101011111111
D3 = 0101011001100111100011110101
· C4 = 0011001100101010101111111100
D4 = 0101100110011110001111010101
· …
…
· C15 = 1111100001100110010101010111
D15 = 1010101010110011001111000111
· C16 = 1111000011001100101010101111
D16 = 0101010101100110011110001111
· Kn(48 位 ) = PC-2( CnDn(56 位 ) )
· PC-2(8×6)
· 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
· 最終得到所有16個子鑰,每個48位:
K1 = 000110 110000 001011 101111 111111 000111 000001 110010
K2 = 011110 011010 111011 011001 110110 111100 100111 100101
K3 = 010101 011111 110010 001010 010000 101100 111110 011001
K4 = 011100 101010 110111 010110 110110 110011 010100 011101
K5 = 011111 001110 110000 000111 111010 110101 001110 101000
K6 = 011000 111010 010100 111110 010100 000111 101100 101111
K7 = 111011 001000 010010 110111 111101 100001 100010 111100
K8 = 111101 111000 101000 111010 110000 010011 101111 111011
K9 = 111000 001101 101111 101011 111011 011110 011110 000001
K10 = 101100 011111 001101 000111 101110 100100 011001 001111
K11 = 001000 010101 111111 010011 110111 101101 001110 000110
K12 = 011101 010111 000111 110101 100101 000110 011111 101001
K13 = 100101 111100 010111 010001 111110 101011 101001 000001
K14 = 010111 110100 001110 110111 111100 101110 011100 111010
K15 = 101111 111001 000110 001101 001111 010011 111100 001010
K16 = 110010 110011 110110 001011 000011 100001 011111 110101
3 )第二步:用子鑰對 64 位數(shù)據(jù)加密
· 對明文 M 使用 IP(8×8)
· 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
· 由于M(64位) =0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
· 對M運(yùn)用IP,故有 IP(64位) = 1100 1100 0000 0000 1100 1100 1111 1111 1111 0000 1010 1010 1111 0000 1010 1010
· IP(64位) = L0(32位) + R0(32位)
· 故
· L0 (32位) = 1100 1100 0000 0000 1100 1100 1111 1111
R0 (32位) = 1111 0000 1010 1010 1111 0000 1010 1010
· 從L0和R0開始,循環(huán)16次,得出L1R1到L16R16,依據(jù)遞推公式:
· Ln = R(n-1)
Rn = L(n-1) + f (R(n-1),Kn)
L1=R0
R1=L0異或F(R0,K1)
· 其中除了Kn為48位,其他變量及函數(shù)均為32位。
· 其中+號表示異或XOR運(yùn)算,函數(shù)f 從一個32位的數(shù)據(jù)塊R(n-1)和一個48位子鑰Kn得到一個新的32位數(shù)據(jù)塊。
· 先將32位R(n-1)按照下表擴(kuò)展到48位,在進(jìn)行異或運(yùn)算
· 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,
· 得到48位數(shù),將48位數(shù)順序分成8份,6位一份,作為輸入,通過第6位中的1和6作為行數(shù)2-5作為列數(shù)在S盒中進(jìn)行置換最后形成32位的f (R(n-1),Kn)
· 101000 行10 列0100à4 à1101 à 13
· S[1]:
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,
· S[2]:
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,
· S[3]
· ….
· S[8]
· 到此為止,我們得到了16對32位的數(shù)據(jù)塊,即 L1R1, L2R2, L3R3, …, L16R16
· 最后一對L16R16就是我們需要的。
· 繼續(xù)對R16L16(64位)運(yùn)用一次重排列: IP-1(8×8)
· 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
· 即在 L16(32位) = 0100 0011 0100 0010 0011 0010 0011 0100
R16(32位) = 0000 1010 0100 1100 1101 1001 1001 0101
R16L16(64位) = 00001010 01001100 11011001 10010101 01000011 01000010 00110010 00110100
· 時(shí),對R16L16運(yùn)用IP-1,得 IP-1(64位) = 10000101 11101000 00010011 01010100 00001111 00001010 10110100 00000101 = 85E813540F0AB405
· 從而,經(jīng)過以上步驟,最終從明文 M = 0123456789ABCDEF 得到密文 C = IP-1 = 85E813540F0AB405
· 以上為加密過程,要解密,依次反向計(jì)算即可。
/*
* 用jdk實(shí)現(xiàn)DES:
* */
public static void jdkDES(String src){
try{
// 生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance('DES');
keyGenerator.init(56);
// 產(chǎn)生密鑰
SecretKey secretKey = keyGenerator.generateKey();
// 獲取密鑰
byte[] bytesKey = secretKey.getEncoded();
// KEY轉(zhuǎn)換
DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance('DES');
Key convertSecretKey = factory.generateSecret(desKeySpec);
// 加密(加解密方式:..工作模式/填充方式)
Cipher cipher = Cipher.getInstance('DES/ECB/PKCS5Padding');
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println('jdk des encrypt:' + Hex.encodeHexString(result));
// 解密
cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
result = cipher.doFinal(result);
System.out.println('jdk des decrypt:' + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
3DES是一種基于DES的加密算法,使用3個不同密匙對同一個分組數(shù)據(jù)塊進(jìn)行3次加密,如此以使得密文強(qiáng)度更高。
/*
* 用jdk實(shí)現(xiàn)3DES:
* */
public static void jdk3DES(String src) {
try {
// 生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance('DESede');
// 必須長度是:112或168
// keyGenerator.init(168);
keyGenerator.init(new SecureRandom());
// 產(chǎn)生密鑰
SecretKey secretKey = keyGenerator.generateKey();
// 獲取密鑰
byte[] bytesKey = secretKey.getEncoded();
// KEY轉(zhuǎn)換
DESedeKeySpec desKeySpec = new DESedeKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance('DESede');
Key convertSecretKey = factory.generateSecret(desKeySpec);
// 加密
Cipher cipher = Cipher.getInstance('DESede/ECB/PKCS5Padding');
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println('jdk 3des encrypt:' + Hex.encodeHexString(result));
// 解密
cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
result = cipher.doFinal(result);
System.out.println('jdk 3des decrypt:' + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
相較于DES和3DES算法而言,AES算法有著更高的速度和資源使用效率,安全級別也較之更高了,被稱為下一代加密標(biāo)準(zhǔn)。AES將是未來最主要,最常用的對稱密碼算法;
/*
* 用jdk實(shí)現(xiàn)AES:
* */
public static void jdkAES(String src){
try{
// 生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance('AES');
keyGenerator.init(128);
// 產(chǎn)生密鑰
SecretKey secretKey = keyGenerator.generateKey();
// 獲取密鑰
byte[] keyBytes = secretKey.getEncoded();
// KEY轉(zhuǎn)換
Key key = new SecretKeySpec(keyBytes, 'AES');
// 加密
Cipher cipher = Cipher.getInstance('AES/ECB/PKCS5Padding');
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println('jdk aes encrypt:' + Hex.encodeHexString(result));
// 解密
cipher.init(Cipher.DECRYPT_MODE, key);
result = cipher.doFinal(result);
System.out.println('jdk aes decrypt:' + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}