java encryption and decryption algorithm --DES

  • ECB
sun.misc.BASE64Decoder Import; 

Import in javax.crypto *;. 
Import javax.crypto.spec.DESKeySpec; 
Import java.io.IOException; 
Import java.security.InvalidKeyException; 
Import java.security.Key; 
Import java.security.NoSuchAlgorithmException ; 
Import java.security.spec.InvalidKeySpecException; 

public class the encrypt { 

    public static byte [] initSecretKey () throws NoSuchAlgorithmException { 
        // the specified key generation algorithm is 
        the KeyGenerator kg = KeyGenerator.getInstance ( "des"); 
        // initialize keys generator, the secret key is determined to have a size 
        kg.init (56 is); 
        // generates a secret key 
        a SecretKey kg.generateKey SecretKey = (); 
        return secretkey.getEncoded (); 
    }

    static byte public [] the encrypt (byte [] Key, the src String) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, a NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { 
        // initialize keys JDK des conversion keys 
        DESKeySpec DESKeySpec = new new DESKeySpec (Key); 
        // Create examples of plant keys 
        the SecretKeyFactory SecretKeyFactory = SecretKeyFactory.getInstance ( "des"); 
        // generates a secret key 
        a secretKey secretKey = SecretKeyFactory.generateSecret (DESKeySpec); 

        / ** 
         * practical encryption cipher 
         * / 
        // Create cipher object 
        cipher cipher = cipher .getInstance ( "des / ecb / PKCS5Padding"); 
        // initialize the Cipher 
        cipher.init (Cipher.ENCRYPT_MODE, secretKey);
        byte[] data = src.getBytes();
        //加密
        byte[] encryptedData = cipher.doFinal(data);

    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException {
        //byte[] secretKey = initSecretKey();
        //或者代码中约定key
        String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
        byte[] secretKey = new BASE64Decoder().decodeBuffer(key);
        String str = "abc";
        byte[] encryptedData = encrypt(secretKey, str);
        DecrypteData the decrypt = String (secretKey, encryptedData); 
        Cipher cipher = Cipher.getInstance ( "des / ecb / PKCS5Padding");
    }

    the decrypt static String public (byte [] Key, byte [] encryptedData) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, a NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { 
        // initialize keys JDK des conversion keys 
        DESKeySpec DESKeySpec = new new DESKeySpec (Key); 
        // Create examples of plant keys 
        the SecretKeyFactory SecretKeyFactory = SecretKeyFactory.getInstance ( "des"); 
        // generates a secret key 
        a secretKey secretKey = SecretKeyFactory.generateSecret (DESKeySpec); 

        / ** 
         * decryption practice Cipher 
         * / 
        // Create Cipher object 
        // initialization Cipher 
        cipher.init (Cipher.DECRYPT_MODE, secretKey); 
        // encryption
        byte[] dencryptedData = cipher.doFinal(encryptedData);
        return new String(dencryptedData);
    }
}
  • CBC

The ECB difference, encryption and decryption must use an initialization vector, and consistent initialization vector

sun.misc.BASE64Decoder Import; 

Import in javax.crypto *;. 
Import javax.crypto.spec.DESKeySpec; 
Import javax.crypto.spec.IvParameterSpec; 
Import java.io.IOException; 
Import java.security.InvalidAlgorithmParameterException; 
Import the java.security .InvalidKeyException; 
Import java.security.Key; 
Import java.security.NoSuchAlgorithmException; 
Import java.security.spec.InvalidKeySpecException; 

public class the encrypt { 

    public static byte [] initSecretKey () throws NoSuchAlgorithmException { 
        // designated secret key algorithm generator 
        KeyGenerator = KeyGenerator.getInstance kg ( "des"); 
        // initialize secret key generator, the secret key is determined to have a size 
        kg.init (56); 
        // generates a secret key
        SecretKey = kg.generateKey a SecretKey (); 
        return secretkey.getEncoded (); 
    } 

    public static byte [] the encrypt (byte [] Key, the src String, byte [] keyIv) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, a NoSuchPaddingException, BadPaddingException, {IllegalBlockSizeException 
        // jdk initialization keys secret key conversion des 
        DESKeySpec DESKeySpec = new new DESKeySpec (Key); 
        // create keys factory instance 
        the SecretKeyFactory SecretKeyFactory = SecretKeyFactory.getInstance ( "des"); 
        // Private keys 
        secretKey secretKey = secretKeyFactory.generateSecret ( DESKeySpec); 

        / ** 
         * encryption practice the Cipher 
         * / 
        // create Cipher object
        Cipher cipher = Cipher.getInstance("des/cbc/PKCS5Padding");
        //创建初始化向量
        IvParameterSpec iv = new IvParameterSpec(keyIv);
        //初始化Cipher
        cipher.init(Cipher.ENCRYPT_MODE,secretKey,iv);
        byte[] data = src.getBytes();
        //加密
        byte[] encryptedData = cipher.doFinal(data);

    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException, InvalidAlgorithmParameterException {
        //byte[] secretKey = initSecretKey();
        //或者代码中约定key
        String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
        byte[] secretKey = new BASE64Decoder().decodeBuffer(key);
        byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};
        String str = "abc";
        byte[] encryptedData = encrypt(secretKey, str,iv);
        String decrypteData = decrypt(secretKey, encryptedData,iv);
    }

    public static String decrypt(byte[] key,byte[] encryptedData,byte[] keyIv) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
        //jdk初始化秘钥转化des秘钥
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例
        SecretKeyFactory = SecretKeyFactory.getInstance SecretKeyFactory ( "des"); 
        //生成秘钥
        SecretKey secretKey = SecretKeyFactory.generateSecret (DESKeySpec); 

        / ** 
         * decryption practice Cipher 
         * / 
        // create Cipher object 
        Cipher cipher = Cipher.getInstance ( "des / cbc / PKCS5Padding "); 
        // initialization vector created 
        IvParameterSpec IV = new new IvParameterSpec (keyIv); 
        // initialize the Cipher 
        cipher.init (Cipher.DECRYPT_MODE, secretKey, IV); 
        // encrypted 
        byte [] = dencryptedData Cipher.doFinal (encryptedData); 
        return String new new (dencryptedData); 
    } 
}

DES and 3DES similar to the same operation mode and fill mode, encryption and decryption are differences:

  • jdk initialization keys transformation des keys

  OF:

DESKeySpec desKeySpec = new DESKeySpec(key);

  3DES:

DESedeKeySpec spec = new DESedeKeySpec(key);
  • Create a secret key factory instance  

  OF:

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");

  3DES:

SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");

  

Guess you like

Origin www.cnblogs.com/ivy-xu/p/12297194.html