Answers for "criot descrip java/oracle"

0

criot descrip java/oracle

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;

/**
 * @author zhuyuan 2019/6/1 10:47
 */
public class DESUtils {
    private static final String SECRET_KEY_FACTORY_ALGORITHM = "DES";
    /**
     * Encryption algorithm: DES
           * Encryption mode: CBC, the encryption mode of each group is strongly associated with the encryption of the previous group.
           * Insufficient digits filling mode: Don't auto fill, fill is realized by program, use 0 to fill.
     */
    private static final String CIPHER_ALGORITHM = "DES/CBC/NoPadding";
    /**
           * Key
     */
    private static final String DEFAULT_SECRET_KEY = "12345678";
    /**
           * Offset 
     */
    private static final byte[] SECRET_KEY_IV = {0, 0, 0, 0, 0, 0, 0, 0};
    /**
           * Group length
     */
    private final static int BLOCK_SIZE = 8;

    /**
           * Encryption
     *
     * @param sourceData
     * @return
     */
    public static String encrypt(String sourceData) {
        return encrypt(sourceData, DEFAULT_SECRET_KEY);
    }

    /**
           * Encryption
     *
     * @param sourceData
     * @param secretKey
     * @return
     */
    public static String encrypt(String sourceData, String secretKey) {
        return bytesToHexStr(encrypt(sourceData.getBytes(), secretKey));
    }

    /**
           * Encryption
     *
     * @param sourceData byte[]
     * @param secretKey  String
     * @return byte[]
     */
    public static byte[] encrypt(byte[] sourceData, String secretKey) {
        try {
            DESKeySpec desKey = new DESKeySpec(secretKey.getBytes());
            // Create a key factory and use it to convert DESKeySpec to
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_ALGORITHM);
            Key key = keyFactory.generateSecret(desKey);
            // Cipher object actually completes the encryption operation
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            // CBC iteration offset
            IvParameterSpec ips = new IvParameterSpec(SECRET_KEY_IV);
            // Initialize the Cipher object with the key
            cipher.init(Cipher.ENCRYPT_MODE, key, ips);
            // Now, get the data and encrypt it
            // Formally execute the encryption operation
            return cipher.doFinal(addPadding(sourceData));
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
	}
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
Posted by: Guest on July-05-2021

Browse Popular Code Answers by Language