如何在Ruby中使用(' DES / CBC / PKCS5Padding'模式)实现DES加密?

时间:2014-11-20 07:50:06

标签: ruby encryption des

我收到了来自第三方公司的DES加密内容。我无法用红宝石解密它。但它适用于我的java代码。


这是java代码:

public class Main {

public static void main(String[] args) {
    try {
        System.out.println(encrypt("12345678", "abc", "12345678"));
        //System.out.println(encrypt("12345678", "ABC", "12345678"));


        System.out.println(decrypt("12345678", "9YR6ZPdZufM=", "12345678"));
        //System.out.println(decrypt("12345678", "6rtTnrF34mPkJ5SO3RiaaQ==", "12345678"));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String encrypt(String key, String str, String ivString) throws Exception {
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Key secretKey = keyFactory.generateSecret(desKeySpec);

    IvParameterSpec iv = new IvParameterSpec(ivString.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    byte[] bytes = cipher.doFinal(str.getBytes());
    dumpHex(bytes);

    return Base64.encode(bytes);
}

public static void dumpHex(byte[] bytes) {
    for (byte b : bytes) {
        System.out.println(String.format("%02x",b&0xff));
    }
    System.out.println(bytesToHex(bytes));
}

final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

public static String decrypt(String key, String str, String ivString) throws Exception {
    byte[] data = Base64.decode(str);
    dumpHex(data);
    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    Key secretKey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec(ivString.getBytes());
    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    byte[] decryptedBytes = cipher.doFinal(data);
    return new String(decryptedBytes, "gb2312");
}

}

我在ruby中找不到相同的DES模式('DES / CBC / PKCS5Padding')。当我尝试加密相同的字符串时,我得到了不同的结果。


这是我的ruby源代码:

class Des
  require 'openssl'
  require 'base64'
  ALG = 'DES-EDE3-CBC'
  KEY = "12345678"
  DES_KEY = "12345678"

  def encode(str)
    des = OpenSSL::Cipher::Cipher.new(ALG)
    des.pkcs5_keyivgen(KEY, DES_KEY)
    des.encrypt
    cipher = des.update(str) + des.final
    pp cipher.length
    pp cipher.unpack('H*')[0]
    return Base64.encode64(cipher)
  end

  def decode(str)
    str = Base64.decode64(str)
    pp str.unpack('H*')[0]
    des = OpenSSL::Cipher::Cipher.new(ALG)
    des.pkcs5_keyivgen(KEY, DES_KEY)
    des.decrypt
    des.update(str) + des.final
  end

  def KEY()
    pp KEY.length
    KEY
  end

  def DES_KEY()
    pp DES_KEY.length
    DES_KEY
  end
end

我用java加密'abc',得到“9YR6ZPdZufM =”。但是在ruby版本中得到“5SHCjTOrygg =”。它让我很困惑。

1 个答案:

答案 0 :(得分:0)

我在ruby代码中更改了一些配置,它可以工作。但我不知道为什么......

def DES
    ALG = 'DES-EDE-CBC'
    KEY = "1234567812345678"
    DES_KEY = "12345678"