Class: DSP::TEA
- Inherits:
-
Object
- Object
- DSP::TEA
- Defined in:
- lib/dsp.rb
Constant Summary collapse
- ROUND =
16- LOG_ROUNDS =
4- DELTA =
0x9e3779b9- SALT_LEN =
2- ZERO_LEN =
7
Class Method Summary collapse
- .convertByteToInt(content) ⇒ Object
- .convertIntToByte(content) ⇒ Object
- .decrypt(encryptContent, key) ⇒ Object
- .decryptData(pInBuf, pKey) ⇒ Object
- .decryptString(encryptedStr, key) ⇒ Object
- .getUnsignedInt(data) ⇒ Object
- .htonl(x) ⇒ Object
- .ntohl(x) ⇒ Object
Class Method Details
.convertByteToInt(content) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dsp.rb', line 52 def convertByteToInt(content) tmp = [] result = [] len = content.length raise "Invalid length of byte array, must be 4 times." if len % 4 != 0 for i in 0...(len / 4) tmp[0,4] = content[i*4, 4] result[i] = ntohl(tmp) end return result end |
.convertIntToByte(content) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/dsp.rb', line 66 def convertIntToByte(content) result = [] for i in (0...content.length) tmp = htonl(content[i]) # System.arraycopy(tmp, 0, result, i * 4, 4) result[i*4, 4] = tmp[0, 4] end return result end |
.decrypt(encryptContent, key) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dsp.rb', line 22 def decrypt(encryptContent, key) con = convertByteToInt(encryptContent) k = convertByteToInt(key) y = getUnsignedInt(con[0]) z = getUnsignedInt(con[1]) sum = 0 a = k[0] b = k[1] c = k[2] d = k[3] sum = getUnsignedInt(0x9e3779b9 << LOG_ROUNDS) delta = getUnsignedInt(0x9e3779b9) for i in (0...ROUND) z -= (y << 4) + c ^ y + sum ^ (y >> 5) + d z &= 0xffffffff y -= (z << 4) + a ^ z + sum ^ (z >> 5) + b y &= 0xffffffff sum -= delta sum &= 0xffffffff end con[0] = y | 0x0 con[1] = z | 0x0 return convertIntToByte(con) end |
.decryptData(pInBuf, pKey) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/dsp.rb', line 96 def decryptData( pInBuf, pKey) nBufPos = 0 dest_buf = [] zero_buf = [] cur_buf = [] nInBufLen = pInBuf.length return nil if nInBufLen % 8 != 0 || nInBufLen < 16 || pKey.length != 16 cur_buf[0,8] = pInBuf[0,8] dest_buf = decrypt(cur_buf, pKey) nPadLen = dest_buf[0] & 7 textLen = nInBufLen - 1 - nPadLen - 2 - 7 pOutBufLen = textLen pOutBuf = [] for i in (0...pOutBufLen) pOutBuf[i] = 48 end for i in (0...8) zero_buf[i] = 0 end iv_pre_crypt = zero_buf.clone iv_cur_crypt = cur_buf.clone nBufPos += 8 cur_buf[0, 8] = pInBuf[8, 8] dest_i = 1 dest_i += nPadLen i = 1 while i <= 2 if(dest_i < 8) dest_i += 1 i += 1 elsif(dest_i == 8) iv_pre_crypt = iv_cur_crypt.clone iv_cur_crypt = cur_buf.clone for j in (0...8) return nil if (nBufPos + j >= nInBufLen) dest_buf[j] ^= pInBuf[nBufPos + j] end dest_buf = decrypt(dest_buf, pKey) nBufPos += 8 cur_buf[0, 8] = pInBuf[16, 8] dest_i = 0 end end nPlainLen = pOutBufLen i = 0 while nPlainLen > 0 if(dest_i < 8) pOutBuf[i+=1] = dest_buf[dest_i] ^ iv_pre_crypt[dest_i] dest_i+=1 nPlainLen-=1 elsif(dest_i == 8) iv_pre_crypt = iv_cur_crypt.clone iv_cur_crypt = cur_buf.clone for j in (0...8) return nil if(nBufPos + j >= nInBufLen) dest_buf[j] ^= pInBuf[nBufPos + j] end dest_buf = decrypt(dest_buf, pKey) nBufPos += 8 dest_i = 0 end end i = 1 while i <= 7 if(dest_i < 8) if((dest_buf[dest_i] ^ iv_pre_crypt[dest_i]) != 1) dest_i+=1 i+=1 else return nil end elsif(dest_i == 8) iv_pre_crypt = iv_cur_crypt.clone iv_cur_crypt = cur_buf.clone for j in (0...8) return nil if(nBufPos + j >= nInBufLen) dest_buf[j] ^= pInBuf[j] end dest_buf = decrypt(dest_buf, pKey) nBufPos += 8 dest_i = 0 end end return pOutBuf end |
.decryptString(encryptedStr, key) ⇒ Object
187 188 189 190 191 192 |
# File 'lib/dsp.rb', line 187 def decryptString(encryptedStr , key) infoStr = Base64.decode64(encryptedStr).bytes bKey = key.bytes res = decryptData(infoStr, bKey) res.pack('c*') end |
.getUnsignedInt(data) ⇒ Object
48 49 50 |
# File 'lib/dsp.rb', line 48 def getUnsignedInt(data) return data & 0xffffffff end |
.htonl(x) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/dsp.rb', line 76 def htonl(x) res = [] for i in (0...4) res[i] = x >> 24 & 0xff x <<= 8 end return res end |
.ntohl(x) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/dsp.rb', line 86 def ntohl(x) res = 0 for i in (0...4) res <<= 8 res |= 0xff & x[i] end return res end |