Bruno Piovan

BEncoding and Torrent files with .Net

I wrote some code to handle bencoding/decoding in C#, the code is based on the specification. I created also a helper class to open/save torrent files and also compute its info_hash.

I wrote this project in C#, but the sample below is in visual basic. I don't think C# developers would have problems to understand it, but if you have any question just let me know.

Sub Main()
    'opens a torrent file and display its info_hash
    Dim torrentFile As BDictionary = Torrent.ParseTorrentFile("C:\Users\Bruno\Desktop\test.torrent")
    Console.Write("Existing torrent's info_hash: ")
    Console.WriteLine(Torrent.ComputeInfoHash(torrentFile).ToString)

    'creates a new torrent file
    Dim newtorrent As New BDictionary()
    newtorrent.Add("created by", "Bruno Piovan")

    'adds some integers
    newtorrent.Add("Ticks", Environment.TickCount)
    newtorrent.Add("Negative", Decimal.MinValue)

    'creates an info dictionary and adds it
    Dim info As New BDictionary
    newtorrent.Add("info", info)

    'generates some random bytes
    Dim sha As SHA512 = SHA512.Create
    Dim bytes() As Byte = sha.ComputeHash(Encoding.UTF8.GetBytes(Environment.TickCount.ToString))

    'adds the random bytes to the info dictionary
    info.Add("bytes", bytes)

    'adds a list
    Dim list As New BList
    For Each p As Process In Process.GetProcesses
        list.Add(p.ProcessName)
    Next
    info.Add("Processes", list)

    'computes the info_hash of the new torrent
    Console.Write("New torrent's info_hash:      ")
    Console.WriteLine(Torrent.ComputeInfoHash(newtorrent).ToString)

    'saves the torrent
    Torrent.SaveTorrent(newtorrent, "C:\Users\Bruno\Desktop\new-torrent.torrent")
End Sub

If you use this code, let me know if you find any bug or wrong implementation.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 14, 2009 7:36 PM

Michaeli said:

Hello, i like your project, but i fail to use it.

I have a torrent file which has more than one tracker and i do not know how to extract ALL trackers from it using your torrentparser.

Maybe you can give me some infos on that matter.

Best regards,

Michaeli

# August 28, 2009 6:26 PM

Bruno Piovan said:

@Michaeli can you send me the torrent file? Thanks!

# September 11, 2009 12:36 PM

jk said:

Thanks for posting this. Can you give me an example of how to retrieve tracker URL from a torrent file via C#? im a c# newbie.

Thanks

# June 2, 2010 4:54 AM

rtyecript said:

I really liked the article, and the very cool blog

# August 25, 2011 2:36 AM

Chanel Outlet said:

<p>

Thanks for sharing your thoughts with us.I enjoyed well while reading your article.<a href="www.authentic-chanel-outlet.com/" target="_blank" title="Chanel Outlet Online"><u><strong>Chanel Outlet Online</strong></u></a><br />

&nbsp;</p>

# September 16, 2011 10:18 AM

qwfy5287 said:

I find a bug.

Chinese characters will be garbled

# March 3, 2012 8:09 AM

DrulkDalaWritk said:

# May 26, 2012 8:13 PM

DrulkDalaWritk said:

<a href=http://www.hrsholdingsltd.com>Mulberry  Outlet </a>  qtuw

# May 28, 2012 8:58 PM

DrulkDalaWritk said:

# June 11, 2012 3:54 AM

louis vuitton bags said:

Do you adore handbags? In this short article you will find the finest Coach, Gucci, Prada and other custom handbags for 2012. Styles will always be altering and 2012 is not any different. This year the very best creative designers have develop some really incredible stuff. Many of those developers are integrating innovative technologies with the most recent outfits to make totally remarkable <a href=http://www.wowcheaplouisvuitton.com>louis vuitton bags</a>.

# June 11, 2012 4:37 AM

Steve said:

How can I use this? I found this from your planetsourcecode reader and want the torrent WRITER instead, where do you input your file hash/list of files etc etc

# August 19, 2012 2:06 PM

Steve said:

ha, nevermind figured it out, had to convert all the .cs files to vb class files with Csharp to vb.net converter to use in vb.net, not sure if you can use them without converting or compiling into a dll, but it works great now - great code thanks a lot.

# August 19, 2012 4:56 PM

icons pack said:

<a href=projects-dynamicbizinc.com/.../viewtopic.php I congratulate, what excellent answer.</a>

# November 2, 2012 8:36 PM

icons library said:

[url=ramzes-sport.ru/.../viewtopic.php] In my opinion you are mistaken. Let's discuss. Write to me in PM, we will talk.[/url]

# November 4, 2012 11:30 AM

Jimmyoh3vs said:

dyopl<a href=> randy moss jersey </a>

scysz<a href=> sean lee jersey </a>

lnmxv<a href=> darren mcfadden jersey </a>

vueyb<a href=> andy dalton jersey </a>

zdhfl<a href=> darren sproles jersey </a>

# November 4, 2012 7:40 PM

cheap north face coats said:

Woah! I'm really enjoying the template/theme of this website. It's simple, yet effective. A lot of times it's very hard to get that "perfect balance" between usability and visual appearance. I must say you have done a awesome job with this. In addition, the blog loads very quick for me on Chrome. Outstanding Blog!

# November 30, 2012 2:00 PM

shuttedrstocck.cfom said:

By WebOsPublisher

Nettie by artistbyday San Fransico

   logo eastern star

   /* Minimal Theme designed by Artur Kim (arturkim.com) for Tumblr | version 1.5 */

   /* RESET */

   html, body, div, span, applet, object, iframe,

   h1, h2, h3, h4, h5, h6, p, blockquote, pre,

   a, abbr, acronym, address, big, cite, code,

   del, dfn, em, font, img, ins, kbd, q, s, samp,

   small, strike, strong, sub, sup, tt, var,

   b, u, i, center,

   dl, dt, dd, ol, ul, li,

   fieldset, form, label, legend,

   table, caption, tbody, tfoot, thead, tr, th, td

       margin: 0; padding: 0; border: 0; outline: 0; font-size: 100%; vertical-align: baseline; background: transparent;

   body  line-height: 1;

   ol, ul  list-style: none;

   blockquote, q  quotes: none;

   blockquote:before, blockquote:after, q:before, q:after  content: ''; content: none;

   :focus  outline: 0;

   del  text-decoration: line-through;

   table  border-collapse: collapse; border-spacing: 0;

   /* GLOBAL ELEMENTS */

   body  background: #fff;

   body, input, textarea  color: #000; font: 14px/20px "helvetica neue",helvetica,arial,sans-serif;

   p, ul, ol, dd, pre  margin-bottom: 20px;

   pre, code  font: 12px Monaco,monospace; line-height: 20px;

   blockquote  border-left: 5px solid #ddd; color: #555; font-style: italic; margin-bottom: 20px; padding-left: 10px;

   table  border: 1px solid #ccc; border-width: 1px 1px 0 1px; font-size: 14px; line-height: 20px; margin: 0 0 22px 0; text-align: left;

   caption  text-align: left;

   tr  border-bottom: 1px solid #ccc;

   th, td  padding: .7em 1.25em;

   hr  background-color: #ccc; border: 0; color: #555; height: 1px; margin-bottom: 20px;

   a:link, a:visited  color: #555;

   a:focus, a:hover, a:active  color: #dd3333;

   h1, h2, h3, h4, h5, h6   font-weight:normal; clear:both;

   img  background: #eee;

   a img:focus, a img:hover, a img:active  background: #dd3333;

   /* DEFAULT CSS */

   #header .menu

       border-bottom: 1px solid #ccc;

       margin: 0 auto 3px;

       overflow: hidden;

       padding: 20px 0 10px;

       width: 900px;

       #header #pages

           float: left;

           width: 580px;

           #header #pages ul

               float: left;

               margin-bottom: 0;

               #header #pages li

                   float: left;

                   margin-right: 10px;

                   padding: 4px 10px 3px 0;

                   #header #pages a, #header #pages a:visited

                       color: #555;

                       font-size: 16px;

                       text-decoration: none;

                   #header #pages a:focus, #header #pages a:hover, #header #pages a:active

                       color: #dd3333;

       #header #search

           float: right;

           #header #search input

               border: 1px solid #ccc;

               line-height: 20px;

               padding: 4px;

           #header #search input.search-button

               background: #eee;

               color: #555;

               padding: 3px;

               width: 60px;

   #header #branding

       border-top: 1px solid #ccc;

       border-bottom: 1px solid #ccc;

       margin: 0 auto 3px;

       padding: 40px 0;

       width: 900px;

       #header h1 a

           color: #111;

           font: 80px Georgia,serif;

           font-weight: normal;

           line-height: 100px;

           text-decoration: none;

       #header h1 a:focus, #header h1 a:hover, #header h1 a:active

           color: #dd3333;

       #header img

           max-width: 900px;

   #main

       border-top: 1px solid #ccc;

       border-bottom: 1px solid #ccc;

       clear: both;

       margin: 0 auto 3px;

       overflow: hidden;

       width: 900px;

       #container

           border-right: 1px solid #ccc;

           float: left;

           min-height: 400px;

           width: 580px;

           .post-meta

               border-bottom: 1px solid #ccc;

               color: #555;

               overflow: hidden;

               padding: 9px 0;

               .post-meta .type

                   float: left;

                   .post-meta .type a

                       border-bottom: 1px solid #ccc;

                       font-weight: bold;

                       padding: 0 10px 10px;

                       text-decoration: none;

                   .post-type-text .post-meta .type a

                       border-bottom: 1px solid #555555;

                       color: #555555;

                   .post-type-photo .post-meta .type a

                       border-bottom: 1px solid #555555;

                       color: #555555;

                   .post-type-photoset .post-meta .type a

                       border-bottom: 1px solid #555555;

                       color: #555555;

                   .post-type-quote .post-meta .type a

                       border-bottom: 1px solid #555555;

                       color: #555555;

                   .post-type-link .post-meta .type a

                       border-bottom: 1px solid #555555;

                       color: #555555;

                   .post-type-chat .post-meta .type a

                       border-bottom: 1px solid #555555;

                       color: #555555;

                   .post-type-video .post-meta .type a

                       border-bottom: 1px solid #555555;

                       color: #555555;

                   .post-type-audio .post-meta .type a

                       border-bottom: 1px solid #555555;

                       color: #555555;

                   .post-meta .type a:focus, .post-meta .type a:hover, .post-meta .type a:active

                       margin-left: 5px;

               .post-meta .date, .post-meta .comments, .post-meta .note-count

                   float: left;

                   padding: 0 10px;

           .post-content

               padding: 30px 30px 60px;

           .post-content img

               max-width: 520px;

           .post-content div:last-child, .post-content div:last-child p,

           .post-content div:last-child ul, .post-content div:last-child ol,

           .post-content div:last-child blockquote

               margin-bottom: 0;

               .post-content h3

                   font: 20px Georgia,serif;

                   margin-bottom: 20px;

                   .post-content h3 a, .post-content h3 a:visited

                       color: #111;

                       text-decoration: none;

                   .post-content h3 a:focus, .post-content h3 a:hover, .post-content h3:active

                       color: #dd3333;

               .post-content img

                   margin-bottom: 20px;

                   padding: 10px;

               .post-type-text .post-content ul

                   padding-left: 30px;

                   .post-type-text .post-content ul ul

                       margin-bottom: 0;

                   .post-type-text .post-content ul li

                       list-style-type: disc;

                       .post-type-text .post-content ul ul li

                           list-style-type: circle;

               .post-type-text .post-content ol

                   padding-left: 30px;

                   .post-type-text .post-content ol ol

                       margin-bottom: 0;

                   .post-type-text .post-content ol li

                       list-style-type: upper-latin;

                       .post-type-text .post-content ol ol li

                           list-style-type: lower-latin;

               .post-type-photoset .post-content .html_photoset

                   background: #eee;

                   margin-bottom: 20px;

                   padding: 10px;

               .post-type-link .post-content .link-wrap

                   margin-bottom: 20px;

               .post-type-chat .post-content .label

                   color: #333;

                   font-weight: bold;

               .post-type-video .post-content .video-wrap

                   background: #f0f0f0;

                   margin-bottom: 20px;

                   padding: 10px;

               .post-type-audio .post-content .caption

                   margin-top: 10px;

               .tags

                   font-size: 13px;

               .tags a

                   text-decoration: none;

               .tags a:after

                   content: ",";

               .tags a:last-child:after

                   content: "";

           #disqus_thread

               border-top: 1px solid #ccc;

               padding: 10px 30px 0;

               #disqus_thread h3

                   color: #111;

                   font: 18px Georgia,serif;

                   margin: 20px 0 10px;

               #dsq-content .dsq-subscribe-menu

                   font-size: 12px;

           #post-notes

               border-top: 1px solid #ccc;

               padding: 30px;

               #post-notes ol li

                   border-bottom: 1px dotted #ccc;

                   padding: 10px 0;

                   #post-notes ol li img

                       background: none;

                       padding-right: 3px;

                       vertical-align: middle;

           .pagination

               border-top: 1px solid #ccc;

               overflow: hidden;

               padding: 20px 0;

               .pagination .previous-page

                   float: left;

               .pagination .next-page

                   float: right;

                   margin-right: 30px;

                   .pagination a

                       font: 20px Georgia,serif;

                       text-decoration: none;

       #sidebar

           float: left;

           padding-bottom: 30px;

           width: 319px;

           #sidebar h3

               border-bottom: 1px dotted #ccc;

               color: #999;

               font-size: 14px;

               font-weight: bold;

               margin: 0 0 20px;

               padding: 9px 20px;

               text-transform: uppercase;

               #sidebar h3 a, #sidebar h3 a:visited

                   color: #999;

                   text-decoration: none;

               #sidebar h3 a:focus, #sidebar h3 a:hover, #sidebar h3 a:active

                   color: #dd3333;

           #sidebar ul

               margin-bottom: 0;

           .widget

               border-bottom: 1px solid #ccc;

               color: #333;

               font-size: 13px;

               padding: 0 20px 20px 20px;

               .widget h3

                   font: 20px Georgia,serif;

                   margin-bottom: 20px;

                   .widget h3 a, .widget h3 a:visited

                       color: #000;

                       text-decoration: none;

                   .widget h3 a:focus, .widget h3 a:hover, .widget h3 a:active

                       color: #dd3333;

               .widget img

                   padding: 5px;

                   max-width: 250px;

               .widget p:last-child, .widget ul:last-child, .widget ol:last-child, .widget blockquote:last-child

                   margin-bottom: 0;

           #about

               overflow: hidden;

               #about img

                   background: #fff;

                   border: 1px solid #ccc;

                   float: left;

                   margin: 0 10px 0 0;

                   padding: 3px;

           .like_post

               border-bottom: 1px dotted #ccc;

               margin-bottom: 20px;

               padding-bottom: 20px;

           .like_post:last-child

               border: none;

               margin: 0;

               padding: 0;

               .like_post ul

                   padding-left: 20px;

                   .like_post ul ul

                       margin-bottom: 0;

                   .like_post ul li

                       list-style-type: disc;

                       .like_post ul ul li

                           list-style-type: circle;

               .like_post ol

                   padding-left: 20px;

                   .like_post ol ol

                       margin-bottom: 0;

                   .like_post ol li

                       list-style-type: upper-latin;

                       .like_post ol ol li

                           list-style-type: lower-latin;

               .like_post p:last-child, .like_post ul:last-child, .like_post ol:last-child, .like_post blockquote:last-child

                   margin-bottom: 0;

           #tweets

               margin-bottom: 10px;

               #tweets .content

                   margin-bottom: 10px;

                   #tweets a

                       text-decoration: none;

           #following_container

               overflow: hidden;

               padding-bottom: 20px;

               #following_container li

                   display: inline;

                   padding: 0;

                   margin: 0;

   #footer

       border-top: 1px solid #ccc;

       clear: both;

       margin: 0 auto;

       overflow: hidden;

       width: 900px;

       #colophon

           color: #555;

           float: right;

           font-size: 12px;

           padding: 10px 0 20px;

           #footer a

               border-bottom: 1px dotted #555;

               text-decoration: none;

                           Home

                           RSS

                   logo eastern star

                               Picture

                               January 09, 2012                            

               Recent Posts

                   clip art of fire or flame mailman in shorts clip art neo babylonian art how to change my computer icon

           Powered by Tumblr. Minimal Theme designed by Artur Kim.

# December 10, 2012 6:34 PM

nrrikmm@gmail.com said:

My current  red bottom heels rbh2013188.overblog.com are the most useful! may possibly beach sand and then choclate we will by no means finish inviting the whole bunch the instant you have them an individual fall in love!

# March 8, 2013 5:06 AM

ckrhvspb@gmail.com said:

If your own photostream is made up of photos who - whether or not good or maybe not : triggered any spirited comments' twine, we just want to see it all!

# March 28, 2013 11:46 AM

ezkbal@gmail.com said:

I care for such info a lot.

# April 11, 2013 2:33 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)