This article describes the definition and usage of 3DES symmetric encryption algorithm implemented by Java. Share it for your reference, as follows:
1. Why 3DES appears
1. Return to Kirkhoff Principle
2. There are security issues
Two 3DES (Triple DES or DESede) benefits
1. Key length enhancement
2. Increase the number of iterations
Three 3DES related parameters
Four 3DES code implementation
package com.imooc.security.des;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESedeKeySpec;import org.apache.commons.codec.binary.Base64;public class Imooc3DES { private static String src = "cakin24 security 3des"; public static void main(String[] args) { jdk3DES(); } public static void jdk3DES() { try { //Generate KEY KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede"); //keyGenerator.init(168); keyGenerator.init(new SecureRandom()); //Default length SecretKey secretKey = keyGenerator.generateKey(); byte[] bytesKey = secretKey.getEncoded(); //KEY conversion DESedeKeySpec desedeKeySpec = new DESedeKeySpec(bytesKey); SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede"); Key convertSecretKey = factory.generateSecret(desedeKeySpec); // Encryption 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 : " + Base64.encodeBase64String(result)); //Decrypt cipher.init(Cipher.DECRYPT_MODE, convertSecretKey); result = cipher.doFinal(result); System.out.println("jdk 3des decrypt : " + new String(result)); } catch (Exception e) { e.printStackTrace(); } }}Five achievement
jdk 3des encrypt: 6t7A/RnarDZSl+MteZVBfxWnNZr0yjBw
jdk 3des decrypt : cakin24 security 3des
Note: org.apache.commons.codec.binary.Base64 package is introduced here, and you can click here to download this site .
PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:
Online DES encryption/decryption tools:
http://tools.VeVB.COM/password/des_encode
Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools.VeVB.COM/password/txt_encode
MD5 online encryption tool:
http://tools.VeVB.COM/password/CreateMD5Password
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools.VeVB.COM/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 encryption tool:
http://tools.VeVB.COM/password/sha_encode
For more information about Java related content, please check out the topics of this site: "Summary of Java Mathematical Operation Skills", "Tutorial on Java Data Structures and Algorithms", "Summary of Java Characters and String Operation Skills", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.