def reconstruct_image(images, k, prime, shares): imgs = [np.array(Image.open(i)) for i in images] row_count = 0 mat_len, row_len, rgb = imgs[0].shape len_imgs = len(imgs) matrix = [[] for i in range(mat_len)] need_calc = [] img_dict = {} rec_info = {} counter = 0 for row in imgs[0]: rec_pix = [[] for i in range(row_len)] pix_count = 0 for pix in row: shares_r = [] shares_g = [] shares_b = [] save = False num = 0 for j in range(len_imgs): red, green, blue = imgs[j][row_count][pix_count][0], imgs[j][row_count][pix_count][1], \ imgs[j][row_count][pix_count][2] if (red == 0 or blue == 0 or green == 0) and ([row_count, pix_count] not in need_calc): need_calc.append([row_count, pix_count]) img_dict[counter] = [red, green, blue] save = True num = j shares_r.append(red) shares_g.append(green) shares_b.append(blue) if save: rec_info[counter] = [num, shares_r, shares_g, shares_b] counter += 1 r, g, b = int(Scheme.reconstruct_secret_img(shares[0][row_count][pix_count], shares_r, k, prime)), \ int(Scheme.reconstruct_secret_img(shares[1][row_count][pix_count], shares_g, k, prime)), \ int(Scheme.reconstruct_secret_img(shares[2][row_count][pix_count], shares_b, k, prime)) if r == 256: r = 0 if g == 256: g = 0 if b == 256: b = 0 rec_pix[pix_count] = [r, g, b] pix_count += 1 matrix[row_count] = rec_pix row_count += 1 matrix = np.asarray(matrix) for i in range(len(need_calc)): [red, green, blue] = img_dict[i] [row_count, pix_count] = need_calc[i] [r, g, b] = matrix[row_count][pix_count] R, G, B = euclidean_dist(i, need_calc[i][0], need_calc[i][1], [r, g, b], [red, green, blue], rec_info, matrix, shares, k, prime) matrix[need_calc[i][0]][need_calc[i][1]] = [R, G, B] return matrix
def euclidean_dist(cnt, row, col, pix, sh_pix, rec_info, rec_pic, shares, k, prime): h, w = len(rec_pic), len(rec_pic[0]) dist1 = 0 dist2 = 0 R = sh_pix[0] G = sh_pix[1] B = sh_pix[2] if R == 0: R = 256 if G == 0: G = 256 if B == 0: B = 256 adjacent = [] sec_adj = [] temp_rec = rec_pic [num, shares_r, shares_g, shares_b] = rec_info[cnt] shares_r[num], shares_g[num], shares_b[num] = R, G, B r, g, b = Scheme.reconstruct_secret_img(shares[0][row][col], shares_r, k, prime), \ Scheme.reconstruct_secret_img(shares[1][row][col], shares_g, k, prime), \ Scheme.reconstruct_secret_img(shares[2][row][col], shares_b, k, prime) temp_rec[row][col] = [r, g, b] R, G, B = r, g, b for j in range(row - 1, row + 2): for i in range(col - 1, col + 2): if 0 <= i < w and 0 <= j < h and not (j == row and i == col): adjacent.append(rec_pic[j][i]) sec_adj.append(temp_rec[j][i]) for a in range(len(adjacent)): cR = pix[0] - adjacent[a][0] cR2 = R - sec_adj[a][0] cG = pix[1] - adjacent[a][1] cG2 = G - sec_adj[a][1] cB = pix[2] - adjacent[a][2] cB2 = B - sec_adj[a][2] uR = (pix[0] + adjacent[a][0]) / 2 uR2 = (R + sec_adj[a][0]) / 2 dist1 += sqrt(cR * cR * (2 + uR / 256) + cG * cG * 4 + cB * cB * (2 + (255 - uR) / 256)) # computing reconstruct_secret over the previous value, ie. if we set the pixel to 256 (previously set to 0 in encryption) dist2 += sqrt(cR2 * cR2 * (2 + uR2 / 256) + cG2 * cG2 * 4 + cB2 * cB2 * (2 + (255 - uR2) / 256)) if dist1 < dist2: R, G, B = pix[0], pix[1], pix[2] return [R, G, B]
def euclidean_dist(cnt, row, col, pix, sh_pix, rec_info, rec_pic, shares, k, prime): h, w = len(rec_pic), len(rec_pic[0]) dist1 = 0 dist2 = 0 R = sh_pix[0] G = sh_pix[1] B = sh_pix[2] if R == 0: R = 256 if G == 0: G = 256 if B == 0: B = 256 adjacent = [] sec_adj = [] temp_rec = rec_pic [num, shares_r, shares_g, shares_b] = rec_info[cnt] shares_r[num], shares_g[num], shares_b[num] = R, G, B r, g, b = Scheme.reconstruct_secret_img(shares[0][row][col], shares_r, k, prime), \ Scheme.reconstruct_secret_img(shares[1][row][col], shares_g, k, prime), \ Scheme.reconstruct_secret_img(shares[2][row][col], shares_b, k, prime) temp_rec[row][col] = [r, g, b] R, G, B = r, g, b for j in range(row - 1, row + 2): for i in range(col - 1, col + 2): if 0 <= i < w and 0 <= j < h and not (j == row and i == col): adjacent.append(rec_pic[j][i]) sec_adj.append(temp_rec[j][i]) for a in range(len(adjacent)): cR = pix[0] - adjacent[a][0] cR2 = R - sec_adj[a][0] cG = pix[1] - adjacent[a][1] cG2 = G - sec_adj[a][1] cB = pix[2] - adjacent[a][2] cB2 = B - sec_adj[a][2] uR = (pix[0] + adjacent[a][0]) / 2 uR2 = (R + sec_adj[a][0]) / 2 dist1 += sqrt(cR * cR * (2 + uR / 256) + cG * cG * 4 + cB * cB * (2 + (255 - uR) / 256)) # racunanje reconstruct_secret preko prethodne vrijednosti, tj. ako piksel stavimo na 256 ( prethodno u ekripciji postavljen na 0) dist2 += sqrt(cR2 * cR2 * (2 + uR2 / 256) + cG2 * cG2 * 4 + cB2 * cB2 * (2 + (255 - uR2) / 256)) if dist1 < dist2: R, G, B = pix[0], pix[1], pix[2] return [R, G, B]
def decrypt_text(self): if self.textEdit.toPlainText() is None or self.textEdit.toPlainText( ) == "": self.error_message = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Critical, "Грешка", "Молим унесите текст за енкрипцију") self.error_message.exec_() elif self.spinBox_4.value() == 0 or self.spinBox_4.value( ) < self.spinBox_3.value() or self.spinBox_3.value( ) == 0 or self.spinBox_3.value() < 2 or self.spinBox_4.value() < 2: self.error_message = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Critical, "Грешка", "Неки од броја подјела није добар") self.error_message.exec_() elif self.listWidget_5.count() < self.spinBox_3.value(): self.error_message = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Critical, "Грешка", "Није унешено довољно дијелова") self.error_message.exec_() else: reconstructed_secret = "" b = 0 for dic in self.text_shares: reconstructed_secret += chr( int( Scheme.reconstruct_secret_img(dic, self.glavna_lista[b], self.spinBox_3.value(), 127))) b += 1 self.textEdit_2.setText(reconstructed_secret) self.listWidget_5.clear() self.glavna_lista = [[] for i in range(len(self.secret_text))]
def decrypt_text(self): if self.textEdit.toPlainText() is None or self.textEdit.toPlainText( ) == "": self.error_message = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Critical, "Error", "Please enter text for encryption") self.error_message.exec_() elif self.spinBox_4.value() == 0 or self.spinBox_4.value( ) < self.spinBox_3.value() or self.spinBox_3.value( ) == 0 or self.spinBox_3.value() < 2 or self.spinBox_4.value() < 2: self.error_message = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Critical, "Error", "Some of the number of divisions is not good") self.error_message.exec_() elif self.listWidget_5.count() < self.spinBox_3.value(): self.error_message = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Critical, "Error", "Not enough parts entered") self.error_message.exec_() else: reconstructed_secret = "" b = 0 for dic in self.text_shares: reconstructed_secret += chr( int( Scheme.reconstruct_secret_img(dic, self.glavna_lista[b], self.spinBox_3.value(), 127))) b += 1 self.textEdit_2.setText(reconstructed_secret) self.listWidget_5.clear() self.glavna_lista = [[] for i in range(len(self.secret_text))]