openssl 3des java 解密

import org.apache.commons.io.FileUtils;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: jun.lei@counect.com
 * Date: 2018-03-12
 * Time: 16:39
 */
public class OpenSSLDes3Utils {
    final static byte[] magic = "Salted__".getBytes(StandardCharsets.US_ASCII);
    public static byte[] decode(File des3File,String password) throws Exception {
        final byte[] pass = password.getBytes(StandardCharsets.US_ASCII);
        BASE64Decoder base64Decoder = new BASE64Decoder();
        String base64Str = FileUtils.readFileToString(des3File,Charset.forName("utf-8"));
        final byte[] base64Bytes = base64Decoder.decodeBuffer(base64Str);
        final byte[] shouldBeMagic = Arrays.copyOfRange(base64Bytes, 0,
                magic.length);
        if (!Arrays.equals(shouldBeMagic, magic)) {
            System.out.println("Bad magic number");
            return null;
        }
    
        final byte[] salt = Arrays.copyOfRange(base64Bytes, magic.length,
                magic.length + 8);
    
        final byte[] passAndSalt = concat(pass, salt);
        byte[] hash = new byte[0];
        byte[] keyAndIv = new byte[0];
        for (int i = 0; i < 2; i++) {
            final byte[] data = concat(hash, passAndSalt);
            final MessageDigest md = MessageDigest.getInstance("MD5");
            hash = md.digest(data);
            keyAndIv = concat(keyAndIv, hash);
        }
    
        final byte[] keyValue = Arrays.copyOfRange(keyAndIv, 0, 24);
        final byte[] iv = Arrays.copyOfRange(keyAndIv, 24, 32);
        return STD3Des.des3DecodeCBC(keyValue,iv,Arrays.copyOfRange(base64Bytes,16,base64Bytes.length));
    }
    
    private static byte[] concat(final byte[] a, final byte[] b) {
        final byte[] c = new byte[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }
    
    public static void main(String[] args) {
        try {
            byte[] tarGzFileBytes = OpenSSLDes3Utils.decode(new File
                    ("C:\\Users\\junle\\Documents\\ms118000102_000001229t_2018-03-09T15-44" +
                    "-47.dat.tar.gz.des"),"111111");
            FileUtils.writeByteArrayToFile(new File("C:\\Users\\junle\\Documents\\ms118000102_000001229t_2018-03-09T15-44-47.dat.tar.gz"),tarGzFileBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


import org.apache.commons.io.FileUtils;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.File;
import java.nio.charset.Charset;
import java.security.Key;

/**
 *
 * @author Administrator
 */
public class STD3Des {
    public static final String KEY_ALGORITHM = "DESede";
    public static final String MD5 = "MD5";
    public static final String CIPHER_ALGORITHM = "DESede/CBC/PKCS5Padding";
    public static final String DEFAULT_CHARSET = "UTF-8";
    public static void main(String[] args) throws Exception {

//        byte[] key = ("f510b8737344cddbca1c8564").getBytes();
//		// byte[] keyiv = {0x66,0x6f,0x61,0x6f,0x63,0x75,0x65,0x6e};
//		byte[] keyiv = {'f','o','a','o','c','u','e','n'};
//
//        System.out.println("\r\nkey.length:" + key.length);
//        byte[] data = "中国ABCabc1234".getBytes("UTF-8");
//
//        System.out.println("ECB加密解密");
//        byte[] str3 = des3EncodeECB(key, data);
//        byte[] str4 = ees3DecodeECB(key, str3);
//        System.out.println(new BASE64Encoder().encode(str3));
//        System.out.println(new String(str4, "UTF-8"));
//
//        System.out.println("<=============>");
//
//       System.out.println("CBC加密解密");
//       byte[] str5 = des3EncodeCBC(key, keyiv, data);
//       byte[] str6 = des3DecodeCBC(key, keyiv, str5);
//       System.out.println(new BASE64Encoder().encode(str5));
//       System.out.println(new String(str6, "UTF-8"));
        BASE64Decoder base64Decoder = new BASE64Decoder();
        String base64Str = FileUtils.readFileToString(new File("C:\\Users\\junle\\Documents\\ms118000102_000001229t_2018-03-09T15-44-47.dat.tar.gz.des"), Charset.forName("utf-8"));
        byte[] base64Bytes = base64Decoder.decodeBuffer(base64Str);
        FileUtils.writeByteArrayToFile(new File
                ("C:\\Users\\junle\\Documents\\ms118000102_000001229t_2018-03-09T15-44-47.dat.encoded"),base64Bytes);
//        MessageDigest md5 = MessageDigest.getInstance(MD5);
//        final byte[] keyBytes = md5.digest("111111".getBytes(DEFAULT_CHARSET));
//        byte[] decoded = des3EncodeCBC(keyBytes,null,base64Bytes);
//        FileUtils.writeByteArrayToFile(new File("C:\\Users\\junle\\Documents\\ms118000102_000001229t_2018-03-09T15-44-47.dat.tar.gz"),decoded);
    }

    /**
     * ECB加密,不要IV
     *
     * @param key 密钥
     * @param data 明文
     * @return Base64编码的密文
     * @throws Exception
     */
    public static byte[] des3EncodeECB(byte[] key, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data);

        return bOut;
    }

    /**
     * ECB解密,不要IV
     *
     * @param key 密钥
     * @param data Base64编码的密文
     * @return 明文
     * @throws Exception
     */
    public static byte[] ees3DecodeECB(byte[] key, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

        cipher.init(Cipher.DECRYPT_MODE, deskey);

        byte[] bOut = cipher.doFinal(data);

        return bOut;

    }

    /**
     * CBC加密
     *
     * @param key 密钥
     * @param keyiv IV
     * @param data 明文
     * @return Base64编码的密文
     * @throws Exception
     */
    public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);

        return bOut;
    }

    /**
     * CBC解密
     *
     * @param key 密钥
     * @param keyiv IV
     * @param data Base64编码的密文
     * @return 明文
     * @throws Exception
     */
    public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);

        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

        byte[] bOut = cipher.doFinal(data);

        return bOut;

    }

}

转载于:https://my.oschina.net/u/732556/blog/1788560

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值