0

嗨,我是加密算法的新手,对术语以及在哪种情况下选择什么感到有些困惑。现在我不需要最安全的算法,但它应该具有以下属性:

  • 共享密钥,用于加密/解密消息(两种方式)。
  • 如果已使用相同的加密文本,则密码应该相同。

我在 Python 中使用了 Fernet 算法,但是密文总是不同的。因此不适合我的需要。有没有类似于 Fernet 但没有 Salt 的算法?

4

2 回答 2

0

实际上,没有现成的算法可以做你所要求的“至少我所知道的”。但在过去,我用 Python 编写了一个带有真实服务器和真实客户端的 GUI 聊天室。而且我编写了一个基本的加密/解密算法来传递加密的“类似消息”的数据。如果你愿意,你可以使用它,因为我从未使用过我制作它只是为了好玩的服务器;)。

算法:

from cryptography.fernet import Fernet

CODES_KEY = b'LYsrKDiTRNJDm3g_h25GW0uyUzPTk8l8G02hcPM_d_U='

def encryption(msg):
    '''
    Encryption Func
    '''

    class RSA:
        '''
        RSA Encryption Func
        '''
        def __init__(self):
            self.e = self.d = self.p = self.q = self.phi = 0

        def __egcd(self, a, b):
            if a == 0:
                return (b, 0, 1)
            g, y, x = self.__egcd(b % a, a)
            return (g, x - (b // a) * y, y)

        def __modinv(self, a, m):
            g, x, _ = self.__egcd(a, m)
            if g != 1:
                raise Exception('modular inverse does not exist')
            return x % m

        def encrypt(self, m, key_pair=None):
            '''
            Encrypting Func
            '''
            if key_pair is None:
                key_pair[0] = self.e
                key_pair[1] = self.n

            return pow(m, key_pair[0], key_pair[1])

        def generate_keys(self, p, q, e=3):
            '''
            Generating Keys Func
            '''
            self.p = p
            self.q = q

            self.n = self.p * self.q
            self.phi = (self.p - 1) * (self.q - 1)
            self.e = e
            self.d = self.__modinv(self.e, self.phi)

            if self.phi % self.e == 0:
                raise Exception('invalid values for p and q')

        def get_public_key(self):
            '''
            Getting PublicKey Func
            '''
            return self.e, self.n

    rsa = RSA()
    # Two 1024-bit primes
    rsa.generate_keys(17055899557196527525682810191339089909014331959812898993437334555169285087976951946809555356817674844913188193949144165887100694620944167618997411049745043243260854998720061941490491091205087788373487296637817044103762239946752241631032791287021875863785226376406279424552454153388492970310795447866569138481,
                      171994050316145327367864378293770397343246561147593187377005295591120640129800725892235968688434055779668692095961697434700708550594137135605048681344218643671046905252163983827396726536078773766353616572531688390937410451433665914394068509329532352022301339189851111636176939179510955519440490431177444857017)

    int_message = []

    for i in msg:

        int_message.append(ord(i))

    codes = []

    for i in int_message:

        codes.append(len(str(i)))

    message = int(''.join([str(x) for x in int_message]))

    # Encrypting
    encrypted_message = rsa.encrypt(message, key_pair=rsa.get_public_key())

    f = Fernet(CODES_KEY)
    encrypted_codes = f.encrypt(str(codes).encode())

    return encrypted_message, encrypted_codes

def decryption(msg, codes):
    '''
    Decryption Func
    '''

    class RSA:
        '''
        RSA Decryption Func
        '''
        def __init__(self):
            self.e = self.d = self.p = self.q = self.phi = 0

        def __egcd(self, a, b):
            if a == 0:
                return (b, a, 1)
            g, y, x = self.__egcd(b % a, a)
            return (g, x - (b // a) * y, y)

        def __modinv(self, a, m):
            g, x, _ = self.__egcd(a, m)
            if g != 1:
                raise Exception('modular inverse does not exist')
            return x % m

        def decrypt(self, c, key_pair=None):
            '''
            Decrypting Func
            '''
            if key_pair is None:
                key_pair[0] = self.d
                key_pair[1] = self.n

            return pow(c, key_pair[0], key_pair[1])

        def generate_keys(self, p, q, e=3):
            '''
            Generating Keys Func
            '''
            self.p = p
            self.q = q

            self.n = self.p * self.q
            self.phi = (self.p - 1) * (self.q - 1)
            self.e = e
            self.d = self.__modinv(self.e, self.phi)

            if self.phi % self.e == 0:
                raise Exception('invalid values for p and q')

        def get_private_key(self):
            '''
            Getting Private Key Func
            '''
            return self.d, self.n

    rsa = RSA()
    # Two 1024-bit primes
    rsa.generate_keys(17055899557196527525682810191339089909014331959812898993437334555169285087976951946809555356817674844913188193949144165887100694620944167618997411049745043243260854998720061941490491091205087788373487296637817044103762239946752241631032791287021875863785226376406279424552454153388492970310795447866569138481,
                      171994050316145327367864378293770397343246561147593187377005295591120640129800725892235968688434055779668692095961697434700708550594137135605048681344218643671046905252163983827396726536078773766353616572531688390937410451433665914394068509329532352022301339189851111636176939179510955519440490431177444857017)

    # Decrypting
    f = Fernet(CODES_KEY)
    # set_trace()
    decrypted_message = str(rsa.decrypt(int(msg), key_pair=rsa.get_private_key()))
    decrypted_codes = f.decrypt(codes).decode()

    decrypted_message = list(decrypted_message)

    original_codes = []

    for i in decrypted_codes:

        if i not in ['[', ']', ',', ' ']:
            original_codes.append(int(i))

    original_message = ""

    for _, value in enumerate(original_codes):

        handler = ""
        for i in decrypted_message[:value]:
            handler += i

        original_message += chr(int(handler))
        decrypted_message = decrypted_message[value:]

    return original_message

def main():

    message = "Hi, This is a secret message"
    encrypted_message, encrypted_codes = encryption(message)
    decrypted_message = decryption(encrypted_message, encrypted_codes)
    print(decrypted_message)

if __name__ == '__main__':
    main()

对不起,如果这是一团糟,但我才 17 岁,兄弟 :) !也许您可以对其进行更多优化我在刚接触python时写过这个。您总是可以通过在 python 中运行它来生成新的“CODES_KEY”:

from cryptography.fernet import Fernet
f = Fernet
print(f.generate_key())

编辑:我将其编写为 RSA,但随后更改了代码,这就是为什么它被命名为 RSA 但它不是 :)

于 2020-08-21T20:44:36.497 回答
0

假设您正在使用加密模块(您应该这样做):而不是使用generate_key(),构建密钥并将其传递给Fernet构造函数。

检查此链接以获取精确示例:https ://cryptography.io/en/latest/fernet/#using-passwords-with-fernet

此外,如果您还没有,请检查https://www.crypto101.io/ 。

于 2020-08-21T20:21:10.377 回答