0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

バイト列の一部に数字を埋め込む、埋め込まれている数字を取り出す

0
Posted at

バイト列の一部に埋め込む

import base64
import struct

arr = bytearray(base64.b64decode('base64文字列'))
arr2 = bytearray(struct.pack("<I", 1111))  <-- 埋め込みたい数字

arr[10] = arr2[0]
arr[11] = arr2[1]
arr[12] = arr2[2]
arr[13] = arr2[3]

base64.b64encode(arr)
  • base64.b64decode の戻りは str
  • str だと位置を指定した変更はできないので、bytearray に変換して一部分の変更をしてやる

埋め込まれたものを取り出す

import sys
import base64
import struct

print struct.unpack('<i', base64.b64decode(args[1])[10:14])[0] <--- args[1] は数字が埋め込まれたバイナリ文字列の base64 文字列
  • str の一部分取り出しは str[0:4] とかで可能
  • struct.unpack の戻りは tuple なので、最後に [0] をつけてやる
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?