给定纯文本消息和数字键,请使用“列式换位密码”对给定文本进行加密/解密
柱状换位密码是换位密码的一种形式,就像Rail Fence Cipher一样。列式转置包括将明文按行写出,然后逐列逐列读取密文。
例子:
Encryption
Input : Geeks for Geeks
Key = HACK
Output : e kefGsGsrekoe_
Decryption
Input : e kefGsGsrekoe_
Key = HACK
Output : Geeks for Geeks
Encryption
Input : Geeks on work
Key = HACK
Output : e w_eoo_Gs kknr_
Decryption
Input : e w_eoo_Gs kknr_
Key = HACK
Output : Geeks on work
加密
在换位密码中,重新排列字母的顺序以获得密文。
- 消息以固定长度的行写出,然后逐列再次读出,并以某种加扰的顺序选择列。
- 行的宽度和列的排列通常由关键字定义。
- 例如,单词HACK的长度为4(因此行的长度为4),并且排列由关键字中字母的字母顺序定义。在这种情况下,顺序将为“ 3 1 2 4”。
- 任何备用空间都将填充为null或保留为空白,或由字符放置(例如:_)。
- 最后,按关键字指定的顺序在列中读取消息。
解密方式
- 要解密它,接收者必须通过将消息长度除以密钥长度来算出列长度。
- 然后,将消息再次写到各列中,然后通过重新设置关键字来重新排列各列的顺序。
C++
// CPP program for illustrating // Columnar Transposition Cipher #include
using namespace std; // Key for Columnar Transposition string const key = "HACK"; map keyMap; void setPermutationOrder() { // Add the permutation order into map for(int i=0; i < key.length(); i++) { keyMap[key[i]] = i; } } // Encryption string encryptMessage(string msg) { int row,col,j; string cipher = ""; /* calculate column of the matrix*/ col = key.length(); /* calculate Maximum row of the matrix*/ row = msg.length()/col; if (msg.length() % col) row += 1; char matrix[row][col]; for (int i=0,k=0; i < row; i++) { for (int j=0; j ::iterator ii = keyMap.begin(); ii!=keyMap.end(); ++ii) { j=ii->second; // getting cipher text from matrix column wise using permuted key for (int i=0; i ::iterator ii=keyMap.begin(); ii!=keyMap.end(); ++ii) ii->second = index++; /* Arrange the matrix column wise according to permutation order by adding into new matrix */ char decCipher[row][col]; map
::iterator ii=keyMap.begin(); int k = 0; for (int l=0,j; key[l]!='\0'; k++) { j = keyMap[key[l++]]; for (int i=0; i
Python3
# Python3 implementation of # Columnar Transposition import math key = "HACK" # Encryption def encryptMessage(msg): cipher = "" # track key indices k_indx = 0 msg_len = float(len(msg)) msg_lst = list(msg) key_lst = sorted(list(key)) # calculate column of the matrix col = len(key) # calculate maximum row of the matrix row = int(math.ceil(msg_len / col)) # add the padding character '_' in empty # the empty cell of the matix fill_null = int((row * col) - msg_len) msg_lst.extend('_' * fill_null) # create Matrix and insert message and # padding characters row-wise matrix = [msg_lst[i: i + col] for i in range(0, len(msg_lst), col)] # read matrix column-wise using key for _ in range(col): curr_idx = key.index(key_lst[k_indx]) cipher += ''.join([row[curr_idx] for row in matrix]) k_indx += 1 return cipher # Decryption def decryptMessage(cipher): msg = "" # track key indices k_indx = 0 # track msg indices msg_indx = 0 msg_len = float(len(cipher)) msg_lst = list(cipher) # calculate column of the matrix col = len(key) # calculate maximum row of the matrix row = int(math.ceil(msg_len / col)) # convert key into list and sort # alphabetically so we can access # each character by its alphabetical position. key_lst = sorted(list(key)) # create an empty matrix to # store deciphered message dec_cipher = [] for _ in range(row): dec_cipher += [[None] * col] # Arrange the matrix column wise according # to permutation order by adding into new matrix for _ in range(col): curr_idx = key.index(key_lst[k_indx]) for j in range(row): dec_cipher[j][curr_idx] = msg_lst[msg_indx] msg_indx += 1 k_indx += 1 # convert decrypted msg matrix into a string try: msg = ''.join(sum(dec_cipher, [])) except TypeError: raise TypeError("This program cannot", "handle repeating words.") null_count = msg.count('_') if null_count > 0: return msg[: -null_count] return msg # Driver Code msg = "Geeks for Geeks" cipher = encryptMessage(msg) print("Encrypted Message: {}". format(cipher)) print("Decryped Message: {}". format(decryptMessage(cipher))) # This code is contributed by Aditya K
输出:
Encrypted Message: e kefGsGsrekoe_ Decrypted Message: Geeks for Geeks
自己尝试:双列换位(第一次世界大战中,美国陆军使用了它,它只是一个列换位,然后是另一个列换位)。