Class: Encryption

Inherits:
Object
  • Object
show all
Defined in:
lib/haruzira_sdk/HzEncryption.rb

Instance Method Summary collapse

Constructor Details

#initializeEncryption

Returns a new instance of Encryption.



21
22
23
24
25
# File 'lib/haruzira_sdk/HzEncryption.rb', line 21

def initialize()
    #region 変数・定数
    @BLOCK_SIZE = 16
    #endregion
end

Instance Method Details

#cipherDecryption(decType, byteDecrypt, strDecKey) ⇒ Object

<summary> 文字列の複合化(AES-CBC) </summary> <param name=“decType”>複合化のアルゴリズム</param> <param name=“byteDecrypt”>複合対象文字列</param> <param name=“strDecKey”>複合キー文字列</param> <returns>OK:複合化された文字列,NG:“”</returns>



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/haruzira_sdk/HzEncryption.rb', line 90

def cipherDecryption(decType, byteDecrypt, strDecKey)
    dec_data = ""

    begin
        dec = OpenSSL::Cipher.new("AES-128-CBC")
        dec.decrypt
        dec_key = createKey(strDecKey)
        dec.key = dec_key
        dec.iv = reverseString(dec_key)
        dec_data = dec.update(byteDecrypt)
        dec_data << dec.final

    rescue Exception => ex
        p ex
        dec_data = ""
    ensure
        return dec_data
    end
        
end

#cipherEncryption(strMsg, encType, strEncKey) ⇒ Object

<summary> 文字列の暗号化(AES-CBC)</summary> <param name=“strMsg”>暗号化を行う文字列</param> <param name=“encType”>暗号化のアルゴリズム</param> <param name=“strEncKey”>暗号化キー文字列</param> <returns>暗号化されたbyte配列</returns>



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/haruzira_sdk/HzEncryption.rb', line 62

def cipherEncryption(strMsg, encType, strEncKey)
    enc_data = ""

    begin
        enc = OpenSSL::Cipher.new("AES-128-CBC")
        enc.encrypt
        enc_key = createKey(strEncKey)
        enc.key = enc_key
        enc.iv = reverseString(enc_key)
        enc.padding = 1
        enc_data = enc.update(strMsg)
        enc_data << enc.final

    rescue Exception => ex
        p ex
        enc_data = ""
    ensure
        return enc_data.unpack("C*")
    end
end

#createKey(k) ⇒ Object

<summary> キーの生成(AES-CBC)</summary> <param name=“strMsg”>s生成を行うキー文字列</param> <returns>生成されたキー文字列</returns>



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/haruzira_sdk/HzEncryption.rb', line 33

def createKey(k)
    retKey = k
    lack = retKey.bytesize % 16
    #puts lack
    if(lack != 0)
        i = 0
        j = 0
        while i < (16 - lack)
            retKey = retKey + j.to_s
            i += 1
            if(j < 9)
                j += 1
            else
                j = 0
            end
        end
    end

    #puts retKey
    return retKey
end

#encodeToBase64String(buff) ⇒ Object

<summary> Base64にエンコードする</summary> <param name=“buff”>エンコード対象文字列バイト配列</param> <returns>エンコード結果</returns>



138
139
140
141
142
# File 'lib/haruzira_sdk/HzEncryption.rb', line 138

def encodeToBase64String(buff)
    strBase64 = Base64.encode64(buff.pack("C*"))

    return strBase64
end

#reverseString(strBuff) ⇒ Object

<summary> 文字列を反転する</summary> <param name=“strBuff”>反転対象文字列</param> <returns>反転結果</returns>



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/haruzira_sdk/HzEncryption.rb', line 116

def reverseString(strBuff)
    rvsStr = strBuff.reverse
    i = 0
    retBuf = []
    (rvsStr.bytes).each do |b|
        retBuf.push(b ^ HZ_XOR_VALUE)
        if(i > 15)
            break
        else
            i += 1
        end

    end

    return retBuf.pack("C*")
end