三重des实现十六进制输入

时间:2014-11-09 19:43:40

标签: java encryption tripledes

使用代码执行三重DES:

public class TripleDES {

    private static sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
    private static sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

    public static void main(String[] args) {

        try {
            try {
                Cipher c = Cipher.getInstance("DESede");
            } catch (Exception e) {
                System.err.println("Installing SunJCE provider.");
                Provider sunjce = new com.sun.crypto.provider.SunJCE();
                Security.addProvider(sunjce);
            }

            File keyFile = new File("D:/3DES/keygen.txt");

            /*
             * writeKey(generateKey(),keyFile);
             * System.out.println("After writing to file");
             */

            SecretKey rKey = readKey(keyFile);
            System.out.println("Secret key :" + rKey);
            String encryptedMssg = encrypt(rKey, "afda");
            System.out.println("Encrypted mssg :" + encryptedMssg);

            String decryptedMssg = decrypt(rKey, encryptedMssg);
            System.out.println("Decrypted mssg :" + decryptedMssg);

        } catch (Exception e) {
            System.err.println(e);
            System.err.println("Usage: java " + TripleDES.class.getName()
                    + " -d|-e|-g <keyfile>");
        }
    }

    public static SecretKey generateKey() throws NoSuchAlgorithmException {
        KeyGenerator keygen = KeyGenerator.getInstance("DESede");
        System.out.println(keygen.generateKey());
        return keygen.generateKey();
    }

    public static void writeKey(SecretKey key, File f) throws IOException,
            NoSuchAlgorithmException, InvalidKeySpecException {
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        DESedeKeySpec keyspec = (DESedeKeySpec) keyfactory.getKeySpec(key,
                DESedeKeySpec.class);
        byte[] rawkey = keyspec.getKey();
        FileOutputStream out = new FileOutputStream(f);
        out.write(rawkey);
        out.close();
    }

    public static SecretKey readKey(File f) throws IOException,
            NoSuchAlgorithmException, InvalidKeyException,
            InvalidKeySpecException {
        DataInputStream in = new DataInputStream(new FileInputStream(f));
        byte[] rawkey = new byte[(int) f.length()];
        in.readFully(rawkey);
        in.close();
        DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyfactory.generateSecret(keyspec);

        return key;
    }

    public static SecretKey readKey(byte[] rawkey) throws IOException,
            NoSuchAlgorithmException, InvalidKeyException,
            InvalidKeySpecException {
        // DataInputStream in = new DataInputStream(new FileInputStream(f));
        // byte[] rawkey = new byte[(int)f.length()];
        // in.readFully(rawkey);
        // in.close();
        DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyfactory.generateSecret(keyspec);

        return key;
    }

    public static String encrypt(SecretKey key, String clearData)
            throws NoSuchAlgorithmException, InvalidKeyException,
            NoSuchPaddingException, IOException, Exception {
        System.out.println("inside encrypt");
        System.out.println("KEYYYYY::" + key);
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cleartext = clearData.getBytes("ASCII");
        byte[] ciphertext = cipher.doFinal(cleartext);

        return encoder.encode(ciphertext);
    }

    public static String decrypt(SecretKey key, String encryptedData)
            throws NoSuchAlgorithmException, InvalidKeyException, IOException,
            IllegalBlockSizeException, NoSuchPaddingException,
            BadPaddingException, Exception {
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] dec = decoder.decodeBuffer(encryptedData);
        byte[] clearbytes = cipher.doFinal(dec);

        return new String(clearbytes, "ASCII");
    }


}

使用此代码我不应该进行加密然后解密然后再加密? 它正在生成加密的字符串,但它与在线计算器不匹配,问题区域在哪里。

问题区域是什么?

1 个答案:

答案 0 :(得分:1)

加密算法"DESede"使用密钥C进行加密,使用密钥B进行解密,使用密钥A进行加密。这些密钥取自提供给DESedeKeySpec的数据。所以DESedeKeySpec应包含密钥C,B和A:3 * 8字节= 24字节。

较新的Java版本还支持16字节的DES EDE密钥。在这种情况下,加密,解密和加密由用于加密的密钥A,用于解密的密钥B和用于再次加密的A执行。这通常称为DES ABA密钥。

换句话说,DESede确实在算法内部使用了三个键。你不必自己编程,你的代码似乎执行得很好。