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();
        }
      }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 更新:MD5加密是单向的,只能加密不能解密(破解除外)。标题可能会引起读者误解,已经改正,感谢Li_Cheng同学...
    葛高召阅读 6,634评论 0 4
  • 为了防止我们的数据泄露,我们往往会对数据进行加密,特别是敏感数据,我们要求的安全性更高。下面将介绍几种常用的加密算...
    Chauncey_Chen阅读 8,000评论 0 20
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,793评论 1 32
  • 今天晚上妈妈带我和姐姐去电影院看了一部电影。这部电影的名字就叫《红海行动》,这是一部保卫世界和平的电影,刚...
    45cbff51831c阅读 3,122评论 1 1
  • 我意识到瞬变中提到的"大象"就是感受。在人的行为中大象扮演即为重要的角色。而我以前却不了解感受,不了解情绪。现在慢...
    心_声阅读 1,462评论 0 0