使用树莓派(Raspberry Pi)的真正随机数生成器

使用电视上的静态信号将Raspberry Pi变成真正的随机数生成器。在国外,我们不再接收模拟地面广播,因此在电视上查找静态信号就像将其放在模拟频道上一样简单。

我使用的设置是插入Raspberry Pi的eSecure USB 8MP网络摄像头,我将其指向电视。我使用python脚本来计算随机数。

第一步是在电视上拍摄静电图像。逐步,我在python中使用了子进程模块。

captureImage = subprocess.Popen(["fswebcam", "-r", "356x292", "-d", "/dev/video0", "static.jpg", "--skip", "10"], stdout=devNull, stderr=devNull)
captureImage.communicate()

如您所见,这只是生成了fswebcam进展进行快照其另存为static.jpg。
使用树莓派(Raspberry Pi)的真正随机数生成器_第1张图片
下一步是将这些图像转换为黑白图像。我将Python图像库导入到脚本中,以操作和读取图像文件。

staticImage = Image.open("static.jpg")
bW_Image = staticImage.convert('1')

下图是这些黑白转换的示例。
使用树莓派(Raspberry Pi)的真正随机数生成器_第2张图片
每个值可以是0或255,具体而言是是白色还是黑色。该值已输入到随机Bits的变量中,其中白色表示为0 ,黑色必然为1。

while pixelRow < staticImage.size[0]:
    while pixelColumn < staticImage.size[1]:
        if imageToProcess[pixelRow, pixelColumn] == 0:
            randomBits = randomBits + "0"
        else:
            randomBits = randomBits + "1"
        pixelColumn = pixelColumn + 1
    pixelRow = pixelRow + 1
    pixelColumn = 0

然后,将randomBits变量作为基数10写入输出文件。这意味着长二进制字符串将转换为十进制值并写入输出文件。该十进制数是从图像计算出的随机值。

output = open('output.txt', 'w')
    output.write(str(int(randomBits, 2)))
    print int(randomBits, 2)
    output.close()

完整的源代码可以从下面复制:

import Image
import subprocess
devNull = open('/dev/null', 'w')#used to output the fswebcam stdout and stderr
name = 0
while True:
    name = name + 1
    randomBits = ""
    pixelRow = 0
    pixelColumn = 0
    captureImage = subprocess.Popen(["fswebcam", "-r", "356x292", "-d", "/dev/video0", "static.jpg", "--skip", "10"], stdout=devNull, stderr=devNull)
    captureImage.communicate()#executes the command detailed above with takes a picture using the webcam
    staticImage = Image.open("static.jpg")#Opens the image
    bW_Image = staticImage.convert('1')#Converts the image to a black or white image
    imageToProcess = bW_Image.load()#Saves the image to a variable that can be iterated through
    while pixelRow < staticImage.size[0]:#Iterates through the image pixel by pixel
        while pixelColumn < staticImage.size[1]:
            if imageToProcess[pixelRow, pixelColumn] == 0:
                randomBits = randomBits + "0"#Adds a 0 to the randomBits variable if the current pixel is white
            else:
                randomBits = randomBits + "1"#Adds a 1 to the randomBits variable if the current pixel is black
            pixelColumn = pixelColumn + 1
        pixelRow = pixelRow + 1
        pixelColumn = 0
    output = open('output.txt', 'w')
    output.write(str(int(randomBits, 2)))#Writes the randomBits Variable to the output file converted to a decimal number
    print int(randomBits, 2)#Also prints this decimal number to the terminal
    output.close()

关注:Hunter网络安全 获取更多资讯
网站:bbs.kylzrv.com
CTF团队:Hunter网络安全
文章:Xtrato
排版:Hunter-匿名者

你可能感兴趣的:(技术,python,安全,经验分享,raspberry,pi,其他)