文档章节

openssl 3des java 解密

rockingMan
 rockingMan
发布于 2018/04/02 13:47
字数 731
阅读 76
收藏 0
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;

    }

}

© 著作权归作者所有

rockingMan
粉丝 11
博文 15
码字总数 3347
作品 0
合肥
后端工程师
私信 提问
com.sun.crypto.provider.SunJCE

Could not instantiate bean class [com.lz.monitor.alert.service.ServiceImp]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com.sun.crypto.provi......

随智阔
2014/03/06
1K
0
Java DESede用C++ Openssl实现

最近在看一个项目的代码 开发语言:C++ 开发环境:VS2005 但有一个很别扭的地方,就是这个项目与外界的加密算法采用DESede,但其实现是采用jni调用java vm里面的函数。 查了一下,可以用Ope...

suit
2012/10/26
2.1K
0
对称加密算法DES、3DES原理和实现方式

1、对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法,技术成熟。在对称加密算法中,数据发信方将明文(原始数据)和加密密钥(mi yue)一起经过特殊加密算法处理后,使其变成复杂的...

zchuanzhao
2015/11/04
6.1K
0
Java加密技术(二)——对称加密算法DES&AES

接下来我们介绍对称加密算法,最常用的莫过于DES数据加密算法。 DES DES-Data Encryption Standard,即数据加密算法。是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个:Key、...

mrliuze
2015/05/27
308
0
Android外部文件加解密及应用实践

有这样的应用场景,当我们把一些重要文件放到asset文件夹中时,把.apk解压是可以直接拿到这个文件的,一些涉及到重要信息的文件我们并不想被反编译拿去,这个时候需要先对文件进行加密,然后...

C6C
2018/05/08
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Navicat 快捷键

操作 结果 ctrl+q 打开查询窗口 ctrl+/ 注释sql语句 ctrl+shift +/ 解除注释 ctrl+r 运行查询窗口的sql语句 ctrl+shift+r 只运行选中的sql语句 F6 打开一个mysql命令行窗口 ctrl+l 删除一行 ...

低至一折起
50分钟前
4
0
PyTorch入门笔记一

张量 引入pytorch,生成一个随机的5x3张量 >>> from __future__ import print_function>>> import torch>>> x = torch.rand(5, 3)>>> print(x)tensor([[0.5555, 0.7301, 0.5655],......

仪山湖
今天
5
0
OSChina 周二乱弹 —— 开发语言和语言开发的能一样么

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @花间小酌:#今日歌曲推荐# 分享The Score的单曲《Revolution》 《Revolution》- The Score 手机党少年们想听歌,请使劲儿戳(这里) @批判派...

小小编辑
今天
2.6K
19
oracle ORA-39700: database must be opened with UPGRADE option

ORA-01092: ORACLE instance terminated. Disconnection forced ORA-00704: bootstrap process failure ORA-39700: database must be opened with UPGRADE option 进程 ID: 3650 会话 ID: 29......

Tank_shu
今天
3
0
分布式协调服务zookeeper

ps.本文为《从Paxos到Zookeeper 分布式一致性原理与实践》笔记之一 ZooKeeper ZooKeeper曾是Apache Hadoop的一个子项目,是一个典型的分布式数据一致性的解决方案,分布式应用程序可以基于它...

ls_cherish
今天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部