desalgorithm.java

来自「200多个自己编的java程序,大家可以学一下.」· Java 代码 · 共 54 行

JAVA
54
字号
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class DESAlgorithm {

    private static String algorithm = "DESede";


    public static SecretKey genDESKey(byte[] keyByte) throws Exception {
        SecretKey key = new SecretKeySpec(keyByte, algorithm);
        return key;
    }


    public static byte[] desEncrypt(SecretKey key, String crypt)
            throws Exception {
        return desEncrypt(key, crypt.getBytes());
    }


    public static byte[] desEncrypt(SecretKey key, byte[] crypt)
            throws Exception {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(crypt);
    }


    public static byte[] desDecrypt(SecretKey key, String crypt)
            throws Exception {
        return desDecrypt(key, crypt.getBytes());
    }


    public static byte[] desDecrypt(SecretKey key, byte[] crypt)
            throws Exception {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(crypt);
    }


    public static String getAlgorithm() {
        return algorithm;
    }


    public static void setAlgorithm(String algorithm) {
        DESAlgorithm.algorithm = algorithm;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?