对称密钥密码算法,也叫做单钥密码算法或私钥密码算法,发送方和接收方共同拥有相同的密钥,发送方使用这个密钥将明文数据加密成密文,然后发送给接收方,接收方收到密文后使用这个密钥将密文解密成明文读取。因为这个密钥既用来进行加密数据,也用来进行解密数据,所以叫做对称密钥,它是一种加密大量数据的加密方法。 
对称密钥密码体制有很多种加密算法,常用的对称加密算法包括DES、3DES、AES、RC4、SM1(国产)、SM4(国产)等。这类算法的长处是加密速度快,便于硬件实现和大规模生产,但由于对称密钥算法的加密密钥和解密密钥时是相同的单个密钥,以是这类加密算法需要保障密钥安全。所以,在使用对称密钥算法加密通信前,必要有安全信道来传递密钥。 DES是一种分组数据加密技术(先将数据分成固定长度的小数据块,之后进行加密),速度较快,适用于大量数据加密,DES是应用最广泛的对称密码算法(由于计算能力的快速进展,DES已不在被认为是安全的);
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 · 其中末尾红色标注为奇偶校验位,即实际密钥为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 · 进而, 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 位数据加密 · 对明文 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运用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开始,循环16次,得出L1R1到L16R16,依据递推公式: · Ln = R(n-1) Rn = L(n-1) + f (R(n-1),Kn) L1=R0 R1=L0异或F(R0,K1) · 其中除了Kn为48位,其他变量及函数均为32位。 · 其中+号表示异或XOR运算,函数f 从一个32位的数据块R(n-1)和一个48位子钥Kn得到一个新的32位数据块。 · 先将32位R(n-1)按照下表扩展到48位,在进行异或运算 · 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位数,将48位数顺序分成8份,6位一份,作为输入,通过第6位中的1和6作为行数2-5作为列数在S盒中进行置换最后形成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位的数据块,即 L1R1, L2R2, L3R3, …, L16R16 · 最后一对L16R16就是我们需要的。 · 继续对R16L16(64位)运用一次重排列: 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 · 时,对R16L16运用IP-1,得 IP-1(64位) = 10000101 11101000 00010011 01010100 00001111 00001010 10110100 00000101 = 85E813540F0AB405 · 从而,经过以上步骤,最终从明文 M = 0123456789ABCDEF 得到密文 C = IP-1 = 85E813540F0AB405 · 以上为加密过程,要解密,依次反向计算即可。 /* * 用jdk实现DES: * */ public static void jdkDES(String src){ try{ // 生成KEY KeyGenerator keyGenerator = KeyGenerator.getInstance('DES'); keyGenerator.init(56); // 产生密钥 SecretKey secretKey = keyGenerator.generateKey(); // 获取密钥 byte[] bytesKey = secretKey.getEncoded(); // KEY转换 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个不同密匙对同一个分组数据块进行3次加密,如此以使得密文强度更高。
/* * 用jdk实现3DES: * */ public static void jdk3DES(String src) { try { // 生成KEY KeyGenerator keyGenerator = KeyGenerator.getInstance('DESede'); // 必须长度是:112或168 // keyGenerator.init(168); keyGenerator.init(new SecureRandom()); // 产生密钥 SecretKey secretKey = keyGenerator.generateKey(); // 获取密钥 byte[] bytesKey = secretKey.getEncoded(); // KEY转换 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算法有着更高的速度和资源使用效率,安全级别也较之更高了,被称为下一代加密标准。AES将是未来最主要,最常用的对称密码算法; /* * 用jdk实现AES: * */ public static void jdkAES(String src){ try{ // 生成KEY KeyGenerator keyGenerator = KeyGenerator.getInstance('AES'); keyGenerator.init(128); // 产生密钥 SecretKey secretKey = keyGenerator.generateKey(); // 获取密钥 byte[] keyBytes = secretKey.getEncoded(); // KEY转换 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(); } }
|