Ruby Ruby 中如何使用 DES 解密?

miserytan · 2018年03月30日 · 最后由 zfjoy520 回复于 2018年04月04日 · 3791 次阅读

java 加密后的结果为: D8B5E8E040EAFDB4E7D02A4D235A2462193D557C7DEE2D0E

package com.oa.base;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.w3c.dom.*;

import com.primeton.crimson.tree.ElementNode;
import com.primeton.eos.bizlets.util.BNXmlUtil;
import com.primeton.tp.core.api.BizContext;

public class Encrypt {

    private static final String keys="8b57d0d3";
    public static String myDecrypt(String message, String key) throws Exception {

        byte[] bytesrc = convertHexString(message);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("US-ASCII"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("US-ASCII"));
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte);
    }

    public static String myEncrypt(String message, String key) throws Exception {
        // message = java.net.URLEncoder.encode(message, "ascii");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("US-ASCII"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("US-ASCII"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        byte[] encryptbyte = cipher.doFinal(message.getBytes());
        return toHexString(encryptbyte).toUpperCase();

    }

    public static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }

        return digest;
    }

    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }

        return hexString.toString();
    }

    /**
     * 
     * @param doc type: Document, DOM;
     * @param param type: BizContext;
     * @return: int ,运算逻辑返回值,如果失败返回0,成功返回1 
     * @throws Exception 
     * <p>
     * ** bizlet 的显示名称 **
     * @bizlet_displayName Encrypt
     */
    public static int Encrypt(Document doc, BizContext param) throws Exception{
        Node userID = (Node)param.getParaObjectAt(0);
        String user = myDecrypt(BNXmlUtil.getNodeValue(userID), keys);
        BNXmlUtil.setNodeValue(userID, user);
        return 1;
    }

}

这个是 Java 方面的加密方式及解密方式, 如何在 ruby 中使用 DES 解密 Java 中加密的内容? ruby 解密之后的结果应该为 2018h03e29h11eng

我自己编写的 ruby 解密:

@string = params[:user_name]
@length = @string.length/2
@my_range = 0..@length
@my_array = []
for i in @my_range
  temp_string = @string[2 * i, 2]
  temp_integer = temp_string.to_i(16)
  @my_array << [temp_integer].pack("C").unpack("c")[0]
end
@my_array.delete(@my_array.last)

@my_array = [-40, -75, -24, -32, 64, -22, -3, -76, -25, -48, 42, 77, 35, 90, 36, 98, 25, 61, 85, 124, 125, -18, 45, 14]

@key = "8b57d0d3"
@cipher = OpenSSL::Cipher.new('DES-EDE3-CBC')
@des_key = @key.encode('US-ASCII')
@cipher.pkcs5_keyivgen(@key, @des_key)  
@cipher.decrypt  
@cipher.update(@my_array) + @cipher.final

可是我的解密就是解不出来,我不知道如何办了,请大神指教

convertHexStringtoHexString 这两个方法是干什么的,先验证这两个方法是不是对的

w7938940 回复

是对的,转换 16 进制

@huacnlee 大神帮忙看下,有没有什么解决办法

经常在交互过程中 key,iv 给的是一个字符串,使用中需要转 16 进制值,pack h*试一下。

@miserytan 拿去,不谢!^_^

require 'openssl'
# DES加密
def encrypt(context, key: '8b57d0d3')
  cipher = OpenSSL::Cipher::DES.new.encrypt.tap { |obj| obj.key = obj.iv = key }
  (cipher.update(context) + cipher.final).unpack1('H*').upcase
end
2.5.0 :001 > encrypt('2018h03e29h11eng') == 'D8B5E8E040EAFDB4E7D02A4D235A2462193D557C7DEE2D0E'
 => true
# DES解密
def decrypt(context, key: '8b57d0d3')
  cipher = OpenSSL::Cipher::DES.new.decrypt.tap { |obj| obj.key = obj.iv = key }
  cipher.update([context].pack('H*')) + cipher.final
end
2.5.0 :002 > decrypt('D8B5E8E040EAFDB4E7D02A4D235A2462193D557C7DEE2D0E') == '2018h03e29h11eng'
 => true
需要 登录 后方可回复, 如果你还没有账号请 注册新账号