账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    JAVA的DES加密类,怎么用PHP实现加密,解密
    25
    0

    1、JAVA的DES加密类,怎么用PHP实现加密,解密。
    (1)加密的情况
    如果是密钥是123#Abcd,明文是hello world,输出3d518daea941120d72fe3488f5d27a64
    在本代码下是会出现data解密前是87897DDA5754EAAE69E7F063E8B0962A,明文是{"retcode":"2"}
    代码如下

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package com.cxlm.commonutil;
    
    import javax.crypto.Cipher;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    
    public class DESUTIL {
        private final String ALGORITHM = "DES/ECB/NoPadding";
        private final String KFNAME = "DES";
        private final DESUTIL.Triplet[] tripletGroup = new DESUTIL.Triplet[]{new DESUTIL.Triplet("3939393939393939", "3132333435363738", (DESUTIL.Triplet)null)};
    
        public DESUTIL() {
        }
    
        private String toPinBlock(String s) throws Exception {
            StringBuffer sb = new StringBuffer();
            int length = s.length();
            if(length > 16) {
                throw new Exception("");
            } else {
                if(length < 10) {
                    sb.append("0").append(Integer.toString(length)).append(s);
                } else {
                    sb.append(Integer.toString(length)).append(s);
                }
    
                for(int i = sb.toString().length(); i < 16; ++i) {
                    sb.append("F");
                }
    
                return sb.toString();
            }
        }
    
        private String toHex(String buffer) {
            StringBuffer sb = new StringBuffer(buffer.length() / 2);
    
            for(int i = 0; i < buffer.length(); i += 2) {
                sb.append((char)Integer.parseInt(buffer.substring(i, i + 2), 16));
            }
    
            return sb.toString();
        }
    
        private String toHex(byte[] buffer) {
            StringBuffer sb = new StringBuffer(2 * buffer.length);
            byte[] var6 = buffer;
            int var5 = buffer.length;
    
            for(int var4 = 0; var4 < var5; ++var4) {
                byte b = var6[var4];
                sb.append(((b & 240) == 0?"0":"") + Integer.toHexString(b & 255));
            }
    
            return sb.toString();
        }
    
        private byte[] fromHex(String s) {
            byte[] result = new byte[s.length() / 2];
            int i = 0;
    
            for(int j = 0; i < result.length; j += 2) {
                result[i] = (byte)Integer.parseInt(s.substring(j, j + 2), 16);
                ++i;
            }
    
            return result;
        }
    
        private byte[] Xor(byte[] block1, byte[] block2) {
            byte[] res = new byte[block1.length];
            if(block1.length == block2.length) {
                for(int i = 0; i < block1.length; ++i) {
                    res[i] = (byte)(block1[i] ^ block2[i]);
                }
            }
    
            return res;
        }
    
        private byte[] DesGenerate_ENCRYPT(String key, String plaintext) throws Exception {
            DESUTIL.Triplet triplet = new DESUTIL.Triplet(key, plaintext, (DESUTIL.Triplet)null);
            Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            byte[] ciphertext = (byte[])null;
            des.init(1, keyFactory.generateSecret(new DESKeySpec(triplet.key)));
            ciphertext = des.doFinal(triplet.plaintext);
            return ciphertext;
        }
    
        private String DesStringGenerate_ENCRYPT(String key, String plaintext) throws Exception {
            DESUTIL.Triplet triplet = new DESUTIL.Triplet(key, plaintext, (DESUTIL.Triplet)null);
            Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            byte[] ciphertext = (byte[])null;
            des.init(1, keyFactory.generateSecret(new DESKeySpec(triplet.key)));
            ciphertext = des.doFinal(triplet.plaintext);
            return this.toHex(ciphertext).toUpperCase();
        }
    
        private String getData(String version) {
            StringBuffer data = new StringBuffer(version.replace(".", ""));
            char[] numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
            int length = version.split("\\.").length + 2;
            int length2 = version.split("\\.").length + 5;
    
            for(int i = 0; i < length; ++i) {
                data.append(numbersAndLetters[length2 + i]);
            }
    
            return data.toString();
        }
    
        private byte[] DesGenerate_DECRYPT(String key, String plaintext) throws Exception {
            DESUTIL.Triplet triplet = new DESUTIL.Triplet(key, plaintext, (DESUTIL.Triplet)null);
            Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            byte[] ciphertext = (byte[])null;
            des.init(2, keyFactory.generateSecret(new DESKeySpec(triplet.key)));
            ciphertext = des.doFinal(triplet.plaintext);
            return ciphertext;
        }
    
        private String DesStringGenerate__DECRYPT(String key, String plaintext) throws Exception {
            DESUTIL.Triplet triplet = new DESUTIL.Triplet(key, plaintext, (DESUTIL.Triplet)null);
            Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            byte[] ciphertext = (byte[])null;
            des.init(2, keyFactory.generateSecret(new DESKeySpec(triplet.key)));
            ciphertext = des.doFinal(triplet.plaintext);
            return this.toHex(ciphertext).toUpperCase();
        }
    
        private void arraycopy(byte[] src, int srcPos, byte[] dest, int destPos, int length) {
            if(dest != null && src != null) {
                byte[] temp = new byte[length];
    
                int i;
                for(i = 0; i < length; ++i) {
                    temp[i] = src[srcPos + i];
                }
    
                for(i = 0; i < length; ++i) {
                    dest[destPos + i] = temp[i];
                }
            }
    
        }
    
        private byte[] ByteDataFormat(byte[] data) {
            int len = data.length;
            int padlen = 8 - len % 8;
            int newlen = len + padlen;
            byte[] newdata = new byte[newlen];
            this.arraycopy(data, 0, newdata, 0, len);
    
            for(int i = len; i < newlen; ++i) {
                newdata[i] = 0;
            }
    
            return newdata;
        }
    
        private byte[] getKey(byte[] data) {
            int len = data.length;
            byte[] newdata = new byte[8];
            if(len >= 8) {
                this.arraycopy(data, 0, newdata, 0, 8);
            } else {
                data = this.ByteDataFormat(data);
                this.arraycopy(data, 0, newdata, 0, 8);
            }
    
            return newdata;
        }
    
        public String encrypt(String key, String plaintext) {
            String r = "";
    
            try {
                if(plaintext == null || plaintext.equals("")) {
                    return "";
                }
    
                r = this.DesStringGenerate_ENCRYPT(this.toHex(this.getKey(key.getBytes())), this.toHex(this.ByteDataFormat(plaintext.getBytes("UTF-8"))));
            } catch (Exception var5) {
                ;
            }
    
            return r;
        }
    
        public String decrypt(String key, String cryptograph) {
            String r = "";
    
            try {
                if(cryptograph == null || cryptograph.equals("")) {
                    return "";
                }
    
                String text = this.DesStringGenerate__DECRYPT(this.toHex(this.getKey(key.getBytes())), cryptograph);
                String text2 = "";
                int index = text.indexOf("00");
                if(index == -1) {
                    text2 = text;
                } else if(index % 2 == 1) {
                    text2 = text.substring(0, index + 1);
                } else {
                    text2 = text.substring(0, index);
                }
    
                r = new String(this.fromHex(text2), "UTF-8");
            } catch (Exception var7) {
                ;
            }
    
            return r;
        }
    
        private class Triplet {
            private final byte[] key;
            private final byte[] plaintext;
    
            private Triplet(String key, String plaintext) {
                this.key = DESUTIL.this.fromHex(key);
                this.plaintext = DESUTIL.this.fromHex(plaintext);
            }
        }
    }
    

    (2)解决方案1,利用以下方法(失败)
    输出1bb0a435b0fcbfe2c48d896fa67b4e53
    代码如下

    /**
     * 使用DES对数据进行解密
     * @param string  $data   要加密的数据
     * @param string  $key    加密数据时候使用的密钥(64bit-->8byte密钥; 128bit-->16byte密钥; 256bit-->32byte密钥)
     * @param string  $iv     加密向量数据, 默认为: 0123456789123456
     * @return string   已解密的数据
     */
    function des_encrypt($data, $key, $iv='', $padding="\0") {
        $blocksize = 16;
        $pad = $blocksize - (strlen($data) % $blocksize);       //计算填充长度
        $data = $data . str_repeat($padding, $pad);     //使用\0进行填充
        return bin2hex(mcrypt_encrypt(MCRYPT_DES, $key, $data, MCRYPT_MODE_ECB, $iv));
    }
    
    /**
     * 使用DES对数据进行解密
     * @param string  $data   要解密的数据
     * @param string  $key    加密数据时候使用的密钥(64bit-->8byte密钥; 128bit-->16byte密钥; 256bit-->32byte密钥)
     * @param string  $iv     加密向量数据, 默认为: 0123456789123456
     * @return string   已解密的数据
     */
    function des_decrypt($data, $key, $iv='', $padding="\0") {
        //先进行解密, 最后对解密的数据进行填充去除
        return rtrim(mcrypt_decrypt(MCRYPT_DES, $key, hex2bin($data), MCRYPT_MODE_ECB, $iv), $padding);
    }
    echo des_encrypt('hello world', '123#Abcd').'</br>';
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 残城碎梦 普通会员 1楼

      在Java中,我们使用DES(Data Encryption Standard)加密算法进行加密。以下是一个使用PHP实现的DES加密类,以及使用PHP解密的方法:

      首先,我们需要在PHP中引入 OpenSSL 库,它提供了一个对 DES 对象的类,可以用于进行加密和解密操作。

      ```php require_once 'openssl.php';

      // 创建 DES 对象 $des = openssl_des_init('des', 'DES/CTR/NoPadding', 512); ```

      然后,我们可以使用以下方法进行加密:

      1. 加密: php $plaintext = 'Hello, World!'; $iv = openssl_random_pseudo_bytes(16); $ciphertext = openssl_encrypt($plaintext, 'DES', $des, OPENSSL_RAW_DATA, $iv); echo "Encrypted ciphertext: " . $ciphertext . "<br>";

      在这个例子中,我们首先创建了一个 DES 对象,然后初始化了密钥对和IV。然后,我们使用 openssl_encrypt 方法对 plaintext(明文)进行加密,传递的参数为明文,加密方式为 DES,密钥为 'DES/CTR/NoPadding',IV为一个长度为 16 的随机字节。最后,我们打印出加密后的 ciphertext。

      1. 解密: php $iv = openssl_random_pseudo_bytes(16); $plaintext = file_get_contents('encrypted.ciphertext'); $decrypted_ciphertext = openssl_decrypt($plaintext, 'DES', $des, OPENSSL_RAW_DATA, $iv); echo "Decrypted ciphertext: " . $decrypted_ciphertext . "<br>";

      在这个例子中,我们首先读取加密后的 ciphertext 文件内容,然后使用 file_get_contents 方法读取该文件。然后,我们使用 openssl_decrypt 方法对 ciphertext 进行解密,传递的参数为 ciphertext,解密方式为 DES,密钥为 'DES/CTR/NoPadding',IV为一个长度为 16 的随机字节。最后,我们打印出解密后的 plaintext。

      注意:在实际应用中,由于DES算法是基于固定的密钥对和IV,所以每次加密和解密都需要在相同的密钥对和IV下进行。如果需要使用不同的密钥对或 IV,需要进行替换。另外,DES算法对于某些输入数据可能不安全,因为它使用了一个不公开的密钥对和IV,需要确保在实际应用中使用了安全的密钥对和 IV。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部