ruby des解密

后端用随手百度来代码写了个des加密,能跑而不知其所以然,ruby适配其进行解密,话不多说,直接上代码:
ruby代码如下

require 'openssl'

# DES加密
def encrypt(context, key: 'key1')
  cipher = OpenSSL::Cipher::DES.new(:ECB).encrypt.tap { |obj| obj.key = key }
  cipher.padding = 5
  encrypt = (cipher.update(context) + cipher.final()).unpack('H*')
  encrypt[0]
end

# DES解密
def decrypt(context, key: 'key1')
  cipher = OpenSSL::Cipher::DES.new(:ECB).decrypt.tap { |obj| obj.key =  key}
  cipher.padding = 5
  cipher.update([context].pack('H*')) + cipher.final
end

orgStr = "ABCD1234"

puts "原文 #{orgStr}"
enStr = encrypt(orgStr)
puts "密文 #{enStr}"#99efa7e595c3034410dd790d60424fb9
deStr = decrypt(enStr)
puts "解密文 #{deStr}"

顺便附上java代码方便对照

import javax.crypto.Cipher;
import java.io.PrintStream;
import java.security.Key;
import java.security.Security;
import javax.crypto.spec.SecretKeySpec;
import com.sun.crypto.provider.SunJCE;

public class DesUtils {
    private static String strDefaultKey = "key1";
      private Cipher encryptCipher = null;
      private Cipher decryptCipher = null;
      
      public static String byteArr2HexStr(byte[] arrB)
        throws Exception
      {
        int iLen = arrB.length;
        
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++)
        {
          int intTmp = arrB[i];
          while (intTmp < 0) {
            intTmp += 256;
          }
          if (intTmp < 16) {
            sb.append("0");
          }
          sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
      }
      
      public static byte[] hexStr2ByteArr(String strIn)
        throws Exception
      {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
        
        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i += 2)
        {
          String strTmp = new String(arrB, i, 2);
          arrOut[(i / 2)] = ((byte)Integer.parseInt(strTmp, 16));
        }
        return arrOut;
      }
      
      public DesUtils()
        throws Exception
      {
        this(strDefaultKey);
      }
      
      public DesUtils(String strKey)
        throws Exception
      {
        Security.addProvider(new SunJCE());
        Key key = getKey(strKey.getBytes());
        
        this.encryptCipher = Cipher.getInstance("DES");
        this.encryptCipher.init(1, key);
        
        this.decryptCipher = Cipher.getInstance("DES");
        this.decryptCipher.init(2, key);
      }
      
      public byte[] encrypt(byte[] arrB)
        throws Exception
      {
        return this.encryptCipher.doFinal(arrB);
      }
      
      public String encrypt(String strIn)
        throws Exception
      {
        return byteArr2HexStr(encrypt(strIn.getBytes()));
      }
      
      public byte[] decrypt(byte[] arrB)
        throws Exception
      {
        return this.decryptCipher.doFinal(arrB);
      }
      
      public String decrypt(String strIn)
        throws Exception
      {
        return new String(decrypt(hexStr2ByteArr(strIn)));
      }
      
      private Key getKey(byte[] arrBTmp)
        throws Exception
      {
        byte[] arrB = new byte[8];
        for (int i = 0; (i < arrBTmp.length) && (i < arrB.length); i++) {
          arrB[i] = arrBTmp[i];
        }
        Key key = new SecretKeySpec(arrB, "DES");
        
        return key;
      }
      
      public static void main(String[] args)
      {
        try
        {
          String str = "ABCD1234";
          DesUtils des = new DesUtils(strDefaultKey);
          System.out.println("原文" + str);
          String enStr = des.encrypt(str);
          System.out.println("密文" + enStr);//99efa7e595c3034410dd790d60424fb9
          String deStr = des.decrypt(enStr);
          System.out.println("解密文" + deStr);
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }

}

你可能感兴趣的:(ruby des解密)