Esempio n. 1
0
def encrypt_cbc(s, iv, key):
    if len(s) % 32:
        s += "\x00" * (32 - (len(s) % 32))
    out = [iv]
    for i in range(0, len(s), 32):
        blk = "".join(chr(ord(s[i + j]) ^ ord(out[-1][j])) for j in xrange(32))
        out.append(rijndael.encrypt(key, blk))
    return "".join(out[1:])
def encrypt_cbc(s, iv, key):
    if len(s) % 32:
        s += b"\x00" * (32 - (len(s) % 32))
    out = [iv]
    for i in range(0, len(s), 32):
        blk = bytes(s[i+j] ^ out[-1][j] for j in range(32))
        out.append(rijndael.encrypt(key, blk))
    return b"".join(out[1:])
def encrypt_cbc(s, iv, key):
    if len(s) % 32:
        s += b"\x00" * (32 - (len(s) % 32))
    out = [iv]
    for i in range(0, len(s), 32):
        blk = bytes(s[i + j] ^ out[-1][j] for j in range(32))
        out.append(rijndael.encrypt(key, blk))
    return b"".join(out[1:])
Esempio n. 4
0
    def crypt_string(self, s):
        """Crypt given string, using current user password. Result is base64 
        version of resulting binary string"""
        l=len(s)
        s2=[] # split s into s2, made of 16 chars
        for i in range(0,l/16+1):
            ss=(s[16*i:16*(i+1)]).ljust(16) #substring
            s2.append(ss)

        cs2=[] # is ciphered s2
        for x in s2:
            y=rijndael.encrypt(self._mdkey,x) # crypt string, remove trailing \n
            cs2.append(y)

        cs=string.join(cs2,'') # cs is a string, joined from cs2
        cs=base64.encodestring(cs)   # base64 yourself
        return cs
Esempio n. 5
0
 def _crypt(self, s, key):
     """Returns the s string encrypted with key"""
     mdkey=md5.md5(key).hexdigest()
     result=rijndael.encrypt(mdkey,s)
     return base64.encodestring(result)
Esempio n. 6
0
    def clickedBtn(self):
        if self.radioButton.isChecked():
            way = '1'
        else:
            way = '2'

        input_path = self.lineEdit.text()

        if os.path.isfile(input_path):
            pass
        else:
            self.textEdit.setPlainText("This is not a file! Check a path!\n")
            return

        key = self.lineEdit2.text()

        time_before = time.time()

        # Input data
        with open(input_path, 'rb') as f:
            data = f.read()
        if way == '1':
            crypted_data = []
            temp = []
            for byte in data:
                temp.append(byte)
                if len(temp) == 16:
                    print("123")
                    crypted_part = rijndael.encrypt(temp, key)
                    crypted_data.extend(crypted_part)
                    del temp[:]
            else:
                #padding v1
                # crypted_data.extend(temp)

                # padding v2
                if 0 < len(temp) < 16:
                    empty_spaces = 16 - len(temp)
                    for i in range(empty_spaces - 1):
                        temp.append(0)
                    temp.append(1)
                    crypted_part = rijndael.encrypt(temp, key)
                    crypted_data.extend(crypted_part)

            out_path = os.path.join(os.path.dirname(input_path),
                                    'crypted_' + os.path.basename(input_path))

            if os.path.exists(out_path):
                os.remove(out_path)

            # Ounput data
            with open(out_path, 'xb') as ff:
                ff.write(bytes(crypted_data))

            self.textEdit.setPlainText(
                "Encryption is successful! Check the file with name crypted_" +
                os.path.basename(input_path) + "\n")

        else:  # if way == '2'
            decrypted_data = []
            temp = []
            for byte in data:
                temp.append(byte)
                if len(temp) == 16:
                    decrypted_part = rijndael.decrypt(temp, key)
                    decrypted_data.extend(decrypted_part)
                    del temp[:]
            else:
                #padding v1
                # decrypted_data.extend(temp)

                # padding v2
                if 0 < len(temp) < 16:
                    empty_spaces = 16 - len(temp)
                    for i in range(empty_spaces - 1):
                        temp.append(0)
                    temp.append(1)
                    decrypted_part = rijndael.encrypt(temp, key)
                    decrypted_data.extend(crypted_part)

            out_path = os.path.join(
                os.path.dirname(input_path),
                'decrypted_' + os.path.basename(input_path))

            if os.path.exists(out_path):
                os.remove(out_path)

            # Ounput data
            with open(out_path, 'xb') as ff:
                ff.write(bytes(decrypted_data))

            self.textEdit.setPlainText(
                "Decryption is successful! Check the file with name decrypted_"
                + os.path.basename(input_path) + "\n")

        time_after = time.time()

        return