首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何按位操作加密灰度图像?

如何按位操作加密灰度图像?
EN

Cryptography用户
提问于 2019-06-30 23:49:02
回答 1查看 512关注 0票数 1

如何按位操作加密灰度图像?

在我的汗学院课程中,他们用bitwise_andbitwise_orbitwise_xor加密图像。我试图复制他们的成果,但没有成功。

可汗学院写作:https://www.khanacademy.org/computing/computer-science/cryptography/ciphers/a/xor-and-the-one-time-pad

我的结果:https://imgur.com/a/ltvj5os

我所做的操作是这样的(Python代码):

代码语言:javascript
复制
def get_bitwise_image(image, key, op):

    and_image = []
    row=0
    col=0
    while row < len(image):
        new_row = []
        col = 0
        while col < len(image[row]):
            # a bitwise and between say 243 and 1 is bitwise and between 2 intergers and not binaries. So, their constituent bits and and-ed together
            # ex: 1 => 00000001 and 243 => 11110011
            # 00000001
            # 11110011
            # --------
            # 00000001 => 1
            if op == 'and':
                new_row.append(image[row, col] & key[row, col])
            elif op == 'or':
                new_row.append(image[row, col] | key[row, col])
            else:
                new_row.append(image[row, col] ^ key[row, col])
            col += 1
        and_image.append(new_row)
        row+=1
    return and_image

我的第二个图像是带有值0-255的灰度图像,我的关键是一个与随机1s和0s图像相同维数的矩阵。

键被定义为:key = numpy.random.randint(0, 2, image.shape)

为什么汗学院的成绩和我的完全不同?如何用逐位操作加密灰度图像?

EN

回答 1

Cryptography用户

发布于 2019-07-01 22:19:15

我已经解决了。

主要原因是我应该使用逻辑操作而不是BITWISE操作。

新结果:https://imgur.com/a/NLa5Q8J

我的职能:

代码语言:javascript
复制
def logical_xor(message, key, op):
    message = np.asarray(message)
    key = np.asarray(key)
    new_image = []
    row=0
    col=0
    while row < len(message):
        new_row = []
        col = 0
        while col < len(message[row]):
            if op == 'and':
                new_row.append(message[row, col] and key[row, col]) # and instead of &
            elif op == 'or':
                new_row.append(message[row, col] or key[row, col])
            else:
                new_row.append(message[row, col] !=  key[row, col]) # xor
            col += 1
        new_image.append(new_row)
        row+=1
    return new_image

但是在使用这个函数之前,我必须这样做:

代码语言:javascript
复制
from PIL import Image
m = Image.open("test_image.jpg") # message
m = m.convert("1") # converts to boolean?
票数 0
EN
页面原文内容由Cryptography提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://crypto.stackexchange.com/questions/71677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档