Random Numbers in Python 3
by admin on Mar.30, 2009, under Python
Generating random numbers in Python is pretty easy and uses the Mersenne Twister core generator. This great for quickly generating random numbers, but is not suitable for all purposes, particularly those involving cryptographic functions.
import random
print(random.randint(0, 100)) #generates a random number between 0 and 100 (inclusive)
Python 3 Dijkstra’s Algorithm
by admin on Mar.30, 2009, under Python
The Dijkstra algorithm is a graph search algorithm designed to solve the single source shortest path problem (that is, two find the shortest path between two points with respect to other nodes in a graph). This algorithm can also be found on Wikipedia.
import heapq
from collections import defaultdict
class Edge(object):
def __init__(self, start, end, weight):
self.start, self.end, self.weight = start, end, weight
# For heapq.
def __cmp__(self, other): return cmp(self.weight, other.weight)
class Graph(object):
def __init__(self):
# The adjacency list.
self.adj = defaultdict(list)
def add_e(self, start, end, weight = 0):
self.adj[start].append(Edge(start, end, weight))
self.adj[end].append(Edge(end, start, weight))
def s_path(self, src):
"""
Returns the distance to every vertex from the source and the
array representing, at index i, the node visited before
visiting node i. This is in the form (dist, previous).
"""
dist, visited, previous, queue = {src: 0}, {}, {}, []
heapq.heappush(queue, (dist[src],src))
while len(queue) > 0:
distance, current = heapq.heappop(queue)
if current in visited:
continue
visited[current] = True
for edge in self.adj[current]:
relaxed = dist[current] + edge.weight
end = edge.end
if end not in dist or relaxed < dist[end]:
previous[end], dist[end] = current, relaxed
heapq.heappush(queue, (dist[end],end))
return dist, previous
Usage
g = Graph()
g.add_e(1,2,4)
..etc etc
# Find a shortest path from vertex 'a' (1) to 'j' (10).
dist, prev = g.s_path(1)
# Trace the path back using the prev array.
path, current, end = [], 10, 10
while current in prev:
path.insert(0, prev[current])
current = prev[current]
print path
print dist[end]
Explanation: the add_e function adds an edge (like an imaginary line) between two nodes, the first argument is the start node, the second argument is the finish node and the third argument is the weight of said edge.
Finding the shortest path: s_path function takes one argument that is the starting node which you wish to find the distance from. The end node is specified in the current and end variables.
The output of this algorithm is the list object (path) which shows the hops between each node (apart from the final node) and dist acts as a dictionary for the distance/weight travelled (dist[end], accessing the sum of all weights).
Python Image Obfuscator
by admin on Feb.07, 2009, under Python
You need Python and PIL installed for this to function correctly. Tabbing may not be correct.
Basically this uses a random number generator to generate the cartesian coordinates of a pixel, then generates three random numbers (one for red, one for green and one for blue) to use as a replacement rgb value. Images can be decrypted/encrypted relatively quickly.
class images:
def reverseit(self, filename):
import re, Image, string
f = open("stored.txt", "r")
content = f.readlines()
f.close()
f = open("stored.txt", "r")
for lines in content:
line = f.readline()
regex = re.compile(r"d{1,3},")
results = re.findall(regex, line)
x = string.replace(results[0], ",", "")
y = string.replace(results[1], ",", "")
red = string.replace(results[2], ",", "")
green = string.replace(results[3], ",", "")
blue = string.replace(results[4], ",", "")
image = Image.open(filename)
loaded = image.load()
loaded[string.atoi(x),string.atoi(y)] =
(string.atoi(red, 10), string.atoi(green, 10),
string.atoi(blue, 10))
f.close()
image.show()
def dotherand(self, loopwhile, filename):
import Image, random
image = Image.open(filename)
loaded = image.load()
loop = 0
newfile = open("stored.txt", "wt")
while loop <> loopwhile:
red = random.randrange(0, 255)
green = random.randrange(0, 255)
blue = random.randrange(0, 255)
x = random.randrange(0, image.size[0])
y = random.randrange(0, image.size[1])
a = loaded[x, y]
b = str(a[0])
c = str(a[1])
d = str(a[2])
towrite = str(x) + "," + str(y) + "," + b + ","
+ c + "," + d + ","
newfile.write(towrite)
loaded[x, y] = (red, green, blue)
loop = loop + 1
newfile.close()
image.show()
def prompt(self):
import sys, string, Image
input = raw_input("Action: ")
if input == "crypt":
filename = raw_input("Filename: ")
number3 = raw_input("Crypt how many pixels: ")
images.dotherand(string.atoi(number3), filename)
images.prompt()
if input == "decrypt":
filename = raw_input("Filename: ")
images.reverseit(filename)
images.prompt()
if input == "exit":
sys.exit(-1)
if input == "clearfile":
f = open("stored.txt", "wt")
f.close()
images = images()
images.prompt()
>
Reuploaded
by admin on Feb.07, 2009, under Python
Reuploaded for those looking for it, no Python 3 (000) port yet…
Download here. Note that it is not completely finished yet but has some cool features like base64/js encoding, as well as a built in proxy finder and user agent spoofing abilities.