Class: NeotelisPay::Utils::Des

Inherits:
Object
  • Object
show all
Defined in:
lib/neotelis_pay/utils/des.rb

Constant Summary collapse

ALG =
'DES-ECB'

Class Method Summary collapse

Class Method Details

.decode(str, key) ⇒ Object

解密



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/neotelis_pay/utils/des.rb', line 22

def self.decode(str, key)
  #key取固定前8位
  use_key = key[0,8]
  #
  decode_str = [str].pack('H*')
  des = OpenSSL::Cipher::Cipher.new(ALG)
  des.decrypt
  des.key = use_key
  result = des.update(decode_str) + des.final
  dec_str = result.force_encoding("GBK")
  utf8_str = dec_str.encode("UTF-8")
  return utf8_str
end

.des_param_list(service_type) ⇒ Object

获取需要加密的字段



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/neotelis_pay/utils/des.rb', line 37

def self.des_param_list(service_type)
  des_key_list = []
  case service_type
    when NeotelisPay::SERVICE_TYPE_SIGN_APPLY
      des_key_list << 'userToken'
      des_key_list << 'payerName'
      des_key_list << 'certNo'
      des_key_list << 'bankCardNo'
    when NeotelisPay::SERVICE_TYPE_SIGN_CONFIRM
      des_key_list << 'token'
      des_key_list << 'verifyCode'
    when NeotelisPay::SERVICE_TYPE_PREPAY
      des_key_list << 'userToken'
      des_key_list << 'payerName'
      des_key_list << 'certNo'
      des_key_list << 'bankCardNo'
    when NeotelisPay::SERVICE_TYPE_PAY_CONFIRM
      des_key_list << 'token'
      des_key_list << 'verifyCode'
    when NeotelisPay::SERVICE_TYPE_GATEWAY_PAY
      des_key_list << ''
  end
  return des_key_list
end

.encode(str, key) ⇒ Object

加密



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/neotelis_pay/utils/des.rb', line 9

def self.encode(str, key)
  #key取固定前8位
  use_key = key[0,8]
  #使用GBK编码
  use_str = str.encode("GBK")
  cipher = OpenSSL::Cipher.new(ALG)
  cipher.encrypt
  cipher.key = use_key
  result = cipher.update(use_str) + cipher.final
  return result.unpack('H*')
end