Questions tagged [bencoding]

Bencoding, pronounced "B-encoding", is the encoding used by BitTorrent.

Introduction

Bencoding, pronounced "B-encoding", is the encoding used by BitTorrent, a peer-to-peer file sharing protocol. It supports the encoding of byte strings, integers, lists, and dictionaries (key-value pairs).

From Wikipedia (http://en.wikipedia.org/w/index.php?title=Bencode&oldid=644035222):

Bencode uses ASCII characters as delimiters and digits.

  • An integer is encoded as i<integer encoded in base ten ASCII>e. Leading zeros are not allowed (although the number zero is still
    represented as "0"). Negative values are encoded by prefixing the
    number with a minus sign. The number 42 would thus be encoded as
    i42e, 0 as i0e, and -42 as i-42e. Negative zero is not permitted.
  • A byte string (a sequence of bytes, not necessarily characters) is encoded as <length>:<contents>. The length is encoded in base 10,
    like integers, but must be non-negative (zero is allowed); the
    contents are just the bytes that make up the string. The string
    "spam" would be encoded as 4:spam. The specification does not deal
    with encoding of characters outside the ASCII set; to mitigate this,
    some BitTorrent applications explicitly communicate the encoding
    (most commonly UTF-8) in various non-standard ways. This is identical to how netstrings work, except that netstrings additionally append a
    comma suffix after the byte sequence.
  • A list of values is encoded as l<contents>e . The contents consist of the bencoded elements of the list, in order, concatenated. A list consisting of the string "spam" and the number 42 would be encoded as: l4:spami42ee. Note the absence of separators between elements.
  • A dictionary is encoded as d<contents>e. The elements of the dictionary are encoded each key immediately followed by its value.
    All keys must be byte strings and must appear in lexicographical
    order. A dictionary that associates the values 42 and "spam" with the keys "foo" and "bar", respectively (in other words, {"bar": "spam", "foo": 42}), would be encoded as follows: d3:bar4:spam3:fooi42ee. (This might be easier to read by inserting some spaces: d 3:bar 4:spam 3:foo i42e e.)

There are no restrictions on what kind of values may be stored in lists and dictionaries; they may (and usually do) contain other lists and dictionaries. This allows for arbitrarily complex data structures to be encoded.

Links

16 questions
4
votes
1 answer

Decode (BEncode) torrent files

Hello I'm making a Console app in VS15 using C#. How can I decode torrent files? To get the Name, Size and Date of the torrent file? I want to donwload a torrent file from a server and then decode it to see the name, size and date. So far i can…
Loc Dai Le
  • 1,661
  • 4
  • 35
  • 70
3
votes
2 answers

How to decode bencoded torrent data

I'm trying to extract size and name from a torrent file with decoding the content of a torrent file with bencode. I did pip install bencode then I tested with one of the line of a torrent file as you can see there. import bencode blabla =…
Bouh10
  • 297
  • 2
  • 6
  • 18
2
votes
1 answer

Why is hashing the info dict turning out wrong?

I have been trying for ages to get this hashing thing for BitTorrent to work in Java but it always becomes wrong. I have narrowed it down to a few lines of code where I'm 99% sure the problem is: Bencode bencode = new…
Jesper
  • 472
  • 1
  • 4
  • 14
2
votes
1 answer

How to import bencode cleanly?

I'm trying to use bdecode from the bencode library that is to say this: def bdecode(x): try: r, l = decode_func[x[0]](x, 0) except (IndexError, KeyError, ValueError): raise BTFailure("not a valid bencoded string") if l !=…
Bouh10
  • 297
  • 2
  • 6
  • 18
2
votes
1 answer

How to bencode non-ascii strings and non-integer numbers?

According to the bencoding specification: Bencoded strings are encoded as follows: :, or key:value Note that there is no constant beginning delimiter, and no ending delimiter. Example: 4:spam…
user784540
1
vote
2 answers

Attempts to parse bencode / torrent file in R

I wish I could parse torrent files automatically via R. I tried to use R-bencode package: library('bencode') test_torrent <- readLines('/home/user/Downloads/some_file.torrent', encoding = "UTF-8") decoded_torrent <-…
masha-pl
  • 49
  • 5
1
vote
0 answers

Node.js: Invalid data: Missing delimiter ":" [0x3a]

I'm trying to read a bencoded file, so I'm executing the following code: 'use strict'; const fs = require('fs'); const bencode = require('bencode'); let buffer = fs.readFileSync('file.torrent'); const torrent =…
1
vote
2 answers

Having issues with BitTorrent Protocol

I'm trying to make just a simple BitTorrent tracker for a school project. It's totally hacked together right now but I can't find where I'm going wrong. I'm wondering if I have a misunderstanding of what the server response should be. I am using…
jawknee530
  • 319
  • 4
  • 12
1
vote
0 answers

cURL makes invalid bencoding when downloading torrents from torcache

The title says it all. I realize that a similar question has been asked at https://askubuntu.com/questions/307566/wget-and-curl-somehow-modifying-bencode-file-when-downloading/310507#310507 but I don't think the same solution works, because I have…
1
vote
1 answer

KRPC query message dict mysterious prefix

My torrent client DHT implementation frequently receives KRPC dict messages with 4 extra bytes before the message dictionary. Here is an example: d1\xba\x04 Following that, is a bencoded dict, d, the contents, and then e, per the specification. It…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
1
vote
1 answer

Bencode in Python with a Debian?

I'm trying to put bencode on my debian in order to decode some bittorrent data but i don't figure-out how to do it from link like this for exemple: https://pypi.python.org/pypi/bencode/1.0 I downloaded the package which correspond to my version…
flo
  • 65
  • 5
0
votes
2 answers

receiving unreadable text while trying to bencode a "find_node" query

Im using the bencodeNET trying to send a find_node query and receive an answer using a bootstrapping node. it seems like the request works well and I do get a response on wireshark and c#. the problem is when decoding (using bencoding ofc) I get…
raz
  • 29
  • 7
0
votes
1 answer

I want to get the information of a torrent file in a desired format through python

I am writing code to parse tracker information in torrent file using python. import bencoder import sys target = './'+sys.argv[1] with open(target, 'rb') as torrent_file: torrent = bencoder.decode(torrent_file.read()) i=0 while True: …
Sliced_ice
  • 37
  • 4
0
votes
1 answer

[ERROR]:Unable to set value to keychain Error Domain=com.samsoffes.sskeychain Code=-50 "errSecParam" UserInfo={NSLocalizedDescription=errSecParam}

I am using appcelerator titanium and bencoding. What could be the error with the following code? Error attached below var securely = require('bencoding.securely'); var SecureProperties = securely.createProperties({ secret :…
0
votes
1 answer

Bencoded string length in java

I am a bit confused with bencoding. According to the specification when I bencode string I need to use the following format: length:string String spam becomes 4:spam My question: 4 is qty of symbols of bencoded string, or qty of utf-8 bytes? For…
user5115411
1
2