Created
December 15, 2019 07:29
-
-
Save bassa01/00ea841dad0876ff5b209988a3aceaae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import array | |
| import secrets | |
| CIPHER_MAX = 54 | |
| PLAIN_MAX = 45 | |
| base62_table = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] | |
| printable_table = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~', ' ', '\n', '\0', '\t'] | |
| tablesize = len(printable_table)//1 | |
| # 鍵を定義するクラス | |
| class CubeOP(): | |
| def __init__(self,direction=None,column=None,times=None): | |
| self.direction = direction | |
| self.column = column | |
| self.times = times | |
| # 暗号化するクラス | |
| class Cube(): | |
| cubing = '' | |
| def __init__(self, string:str): | |
| self.cubing = string | |
| # equal文が呼び出されたときこの関数が使われる | |
| def __eq__(self, a): | |
| for i in range(CIPHER_MAX): | |
| if (self.cubing[i] != a.cubing[i]): | |
| return False | |
| return True | |
| def cubingPrint(self): | |
| for i in range(CIPHER_MAX): | |
| print("cubing[i] = " + self.cubing[i]) | |
| # 転置 | |
| def rotate(self,key): | |
| letter = '' | |
| col = key.column - 1 | |
| if key.direction == 1: #縦方向 | |
| for _ in range(key.times * 3): | |
| letter = self.cubing[0 + col] | |
| self.cubing[0 + col] = self.cubing[3 + col] | |
| self.cubing[3 + col] = self.cubing[6 + col] | |
| self.cubing[6 + col] = self.cubing[12 + col] | |
| self.cubing[12 + col] = self.cubing[24 + col] | |
| self.cubing[24 + col] = self.cubing[36 + col] | |
| self.cubing[36 + col] = self.cubing[45 + col] | |
| self.cubing[45 + col] = self.cubing[48 + col] | |
| self.cubing[48 + col] = self.cubing[51 + col] | |
| self.cubing[51 + col] = self.cubing[44 - col] | |
| self.cubing[44 - col] = self.cubing[32 - col] | |
| self.cubing[32 - col] = self.cubing[20 - col] | |
| self.cubing[20 - col] = letter | |
| return | |
| if key.direction == 2: #横方向 | |
| for _ in range(key.times * 3): | |
| letter = self.cubing[9 + 12 * col] | |
| before_number = 9 + 12 * col | |
| after_number = 10 + 12 * col | |
| for m in range(11): | |
| self.cubing[before_number + m] = self.cubing[after_number + m] | |
| self.cubing[20 + 12 * col] = letter | |
| return | |
| if key.direction == 3: #回転方向 | |
| for _ in range(key.times * 3): | |
| letter = self.cubing[6 - 3*col] | |
| self.cubing[6 - 3*col] = self.cubing[7 - 3*col] | |
| self.cubing[7 - 3*col] = self.cubing[8 - 3*col] | |
| self.cubing[8 - 3*col] = self.cubing[15 + col] | |
| self.cubing[15 + col] = self.cubing[27 + col] | |
| self.cubing[27 + col] = self.cubing[39 + col] | |
| self.cubing[39 + col] = self.cubing[47 + 3*col] | |
| self.cubing[47 + 3*col] = self.cubing[46 + 3*col] | |
| self.cubing[46 + 3*col] = self.cubing[45 + 3*col] | |
| self.cubing[45 + 3*col] = self.cubing[35 - col] | |
| self.cubing[35 - col] = self.cubing[23 - col] | |
| self.cubing[23 - col] = self.cubing[11 - col] | |
| self.cubing[11 - col] = letter | |
| return | |
| print("direction is not found") | |
| exit(self, -500) | |
| # aとbが等しいかを確認する | |
| def assertEqual(a, b): | |
| if (a == b): | |
| print("AC") | |
| else: | |
| print("WA") | |
| # 文字列をcubeに代入する | |
| def str2cube(string): | |
| cube = Cube(string) | |
| return cube | |
| # cubeの配列の中身を取り出してリストにする | |
| def cube2str(cube, string): | |
| for i in range(CIPHER_MAX): | |
| string.append(cube.cubing[i]) | |
| return | |
| def encrypt(key, string, ct): | |
| cube = str2cube(string) | |
| for i in key: | |
| cube.rotate(i) | |
| cube2str(cube, ct) | |
| return | |
| def decrypt(key, string, pt): | |
| cube = str2cube(string) | |
| for i in reversed(key): | |
| beforetimes = i.times | |
| i.times = 4 - i.times | |
| cube.rotate(i) | |
| i.times = beforetimes | |
| cube2str(cube, pt) | |
| def encoding(plainblock, encoded_block, blocknum): | |
| for i in range(PLAIN_MAX): | |
| encoded_block.append(plainblock[i]) | |
| for i in range(5): | |
| sum = 0 | |
| for j in range(9): | |
| sum += ord(plainblock[i * 9 + j]) | |
| encoded_block.append(chr(sum % 26 + ord('a'))) | |
| encoded_block.append(base62_table[blocknum // 62]) | |
| encoded_block.append(base62_table[blocknum % 62]) | |
| chartype1 = secrets.randbelow(62) | |
| chartype2 = secrets.randbelow(62) | |
| encoded_block.append(base62_table[chartype1]) | |
| encoded_block.append(base62_table[chartype2]) | |
| return | |
| # 1 ~ 45 | |
| def masking1(plain, masked, iv): | |
| for i in range(PLAIN_MAX): | |
| iv.append(secrets.randbelow(tablesize)) | |
| dis = printable_table.index(plain[i]) | |
| idx = (iv[len(iv) - 1] + dis) % tablesize | |
| masked.append(printable_table[idx]) | |
| return | |
| # 46 ~ 52 | |
| def masking2(plain, masked, iv): | |
| for i in range(PLAIN_MAX): | |
| masked.append(plain[i]) | |
| for i in range(52 - PLAIN_MAX): | |
| iv.append(secrets.randbelow(tablesize)) | |
| dis = printable_table.index(plain[i + PLAIN_MAX]) | |
| idx = (iv[len(iv) - 1] + dis) % tablesize | |
| masked.append(printable_table[idx]) | |
| masked.append(plain[52]) | |
| masked.append(plain[53]) | |
| return | |
| # 1 ~ 45 | |
| def unMasking1(plain, masked, iv, nowblock): | |
| for i in range(45): | |
| dis = printable_table.index(plain[i]) | |
| idx:int = (tablesize + dis - iv[nowblock * 45 + i]) % tablesize | |
| masked.append(printable_table[int(idx)]) | |
| return | |
| # 46 ~ 54 | |
| def unMasking2(plain, masked, iv, nowblock): | |
| for i in range(PLAIN_MAX): | |
| masked.append(plain[i]) | |
| for i in range(52 - PLAIN_MAX): | |
| dis = printable_table.index(plain[i + PLAIN_MAX]) | |
| idx = (tablesize + dis - iv[nowblock * 7 + i]) % tablesize | |
| masked.append(printable_table[idx]) | |
| masked.append(plain[52]) | |
| masked.append(plain[53]) | |
| return | |
| def shuffle(string): | |
| x = reversed(range(len(string))) | |
| for i in x: | |
| print("i = " , i) | |
| j = secrets.randbelow(i + 1) | |
| print(len(string)) | |
| for k in range(68): | |
| #for k in range(60): | |
| string[i][k], string[j][k] = string[j][k], string[i][k] | |
| return | |
| def cubingmodeEncrypt(key, string,ct, iv1, iv2): | |
| ShuffleText = [] | |
| pt = [] | |
| for i in string: | |
| pt.append(i) | |
| # nullとランダム埋め | |
| if (len(pt) % 45 != 0): | |
| pt.append('\0') | |
| while (len(pt) % 45 != 0): | |
| chartype = secrets.randbelow(62) | |
| pt.append(base62_table[chartype]) | |
| blocknum = len(pt) // 45 | |
| for i in range(blocknum): # ブロック数だけやる | |
| plainblock = [] | |
| encoded_block = [] | |
| cipherblock = [] | |
| masked_block1 = [] | |
| masked_block2 = [] | |
| tmp = [] | |
| for j in range(PLAIN_MAX): # 平文ブロックの作成 | |
| plainblock.append(pt[i * 45 + j]) | |
| masking1(plainblock, masked_block1, iv1) | |
| encoding(masked_block1,encoded_block,i) | |
| masking2(encoded_block, masked_block2, iv2) | |
| encrypt(key, masked_block2, cipherblock) | |
| for j in range(54): | |
| tmp.append(cipherblock[j]) | |
| for j in range(7): | |
| if (iv2[7 * i + j] < 10): | |
| tmp.append('0') | |
| tmp.append(chr(iv2[7 * i + j] + ord('0'))) | |
| else: | |
| tmp.append(chr(iv2[7 * i + j] // 10 + ord('0'))) | |
| tmp.append(chr(iv2[7 * i + j] % 10 + ord('0'))) | |
| ShuffleText.append(tmp) | |
| print(ShuffleText) | |
| shuffle(ShuffleText) | |
| for i in range(len(ShuffleText)): | |
| for j in range(54 + 14): | |
| ct.append(ShuffleText[i][j]) | |
| return | |
| # 62進数のソート周りっぽい | |
| def sequenceNum(f, s): | |
| return base62_table.index(f) * 62 + base62_table.index(s) | |
| def sortComp(l): | |
| left = sequenceNum(l[50], l[51]) | |
| #right = sequenceNum(r[50], r[51]) | |
| #return left < right | |
| return left | |
| def decoding(before_shuffle_text, pt, iv): | |
| tmp = [] | |
| before_shuffle_text.sort(key=sortComp) #TODO:ここなに | |
| for i in range(len(before_shuffle_text)): | |
| unMasking1(before_shuffle_text[i], tmp, iv, i) | |
| for j in range(45): | |
| pt.append(tmp[j]) | |
| if before_shuffle_text.count('\0') != 0: | |
| del pt[before_shuffle_text.index('\0'):] | |
| return | |
| def cubingmodeDecrypt(key, string, pt): | |
| ct = [] | |
| iv1 = [] | |
| iv2 = [] | |
| tmpiv2 = [] | |
| print(string) | |
| for i in range(len(string) // 158 * 68): | |
| if (i % 68 < 54): | |
| ct.append(string[i]) | |
| else: | |
| tmpiv2.append(ord(string[i]) - ord('0')) | |
| print(string) | |
| x = range(len(string))[len(string) // 168 * 68:] | |
| for i in (j for j in x if j%2==0): | |
| iv1.append((ord(string[i]) - ord('0')) * 10 + (ord(string[i + 1]) - ord('0'))) | |
| for i in (j for j in range(len(tmpiv2) - 1) if j%2 ==0): | |
| iv2.append(tmpiv2[i] * 10 + tmpiv2[i + 1]) | |
| i += 2 | |
| print(len(ct)) | |
| print(len(iv2)) | |
| if(len(ct) % 54 != 0 or len(iv2) % 7 != 0): | |
| print("decryption error") | |
| exit(-500) | |
| print(ct) | |
| blocknum = len(ct) // 54 | |
| before_shuffle_text = [] | |
| for i in range(blocknum): | |
| cipherblock = [] | |
| decrypted_block = [] | |
| masked = [] | |
| tmp = [] | |
| for j in range(54): | |
| cipherblock.append(ct[i * 54 + j]) | |
| decrypt(key, cipherblock, decrypted_block) | |
| print("decrypted", decrypted_block) | |
| unMasking2(decrypted_block, masked, iv2, i) | |
| for j in range(54): | |
| tmp.append(masked[j]) | |
| before_shuffle_text.append(tmp) | |
| decoding(before_shuffle_text,pt,iv1) | |
| return | |
| def cubingmodeEncodeDecodeTest(): | |
| string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S'] | |
| chencoded = [] | |
| chdecoded = [] | |
| vcdecoded = [] | |
| vcencoded = [] | |
| todecode = [] | |
| iv = [] | |
| for _ in range(45): | |
| iv.append(0) | |
| op = CubeOP(1, 2, 1) | |
| key = [] | |
| key.append(op) | |
| encoding(string, chencoded,0) | |
| for i in chencoded: | |
| vcencoded.append(i) | |
| todecode.append(vcencoded) | |
| decoding(todecode, vcdecoded, iv) | |
| for i in range(45): | |
| chdecoded.append(vcencoded[i]) | |
| print(string) | |
| print(chdecoded) | |
| assertEqual(string,chdecoded) | |
| def cubingmodeEncodeDecodeTest2(): | |
| string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!'] | |
| ct = [] | |
| pt = [] | |
| op = CubeOP(1, 2, 1) | |
| key = [] | |
| key.append(op) | |
| iv1 = [] | |
| iv2 = [] | |
| cubingmodeEncrypt(key, string, ct, iv1, iv2) | |
| for i in range(len(iv1)): | |
| if (iv1[i] < 10): | |
| ct.append('0') | |
| else: | |
| ct.append(chr(iv1[i] // 10 + ord('0'))) | |
| ct.append(chr(iv1[i] % 10 + ord('0'))) | |
| cubingmodeDecrypt(key, ct, pt) | |
| print(ct) | |
| print(string) | |
| print(pt) | |
| assertEqual(string,pt) | |
| def encryptTest(): | |
| string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| ExpectedStr = ['a', 'n', 'c', 'd', 'z', 'f', 'g', 'L', 'i', 'j', 'k', 'l', 'm', 'U', 'o', 'p', 'q', 'r', 's', 'h', 'u', 'v', 'w', 'x', 'y', 'X', 'A', 'B', 'C', 'D', 'E', 'e', 'G', 'H', 'I', 'J', 'K', '!', 'M', 'N', 'O', 'P', 'Q', 'b', 'S', 'T', 'R', 'V', 'W', 'F', 'Y', 'Z', 't', '?'] | |
| ct = [] | |
| op = CubeOP(1, 2, 1) | |
| key = [] | |
| key.append(op) | |
| encrypt(key, string, ct) | |
| assertEqual(ct, ExpectedStr) | |
| return | |
| def decryptTest(): | |
| string = ['a', 'n', 'c', 'd', 'z', 'f', 'g', 'L', 'i', 'j', 'k', 'l', 'm', 'U', 'o', 'p', 'q', 'r', 's', 'h', 'u', 'v', 'w', 'x', 'y', 'X', 'A', 'B', 'C', 'D', 'E', 'e', 'G', 'H', 'I', 'J', 'K', '!', 'M', 'N', 'O', 'P', 'Q', 'b', 'S', 'T', 'R', 'V', 'W', 'F', 'Y', 'Z', 't', '?'] | |
| ExpectedStr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| pt = [] | |
| op = CubeOP(1, 2, 1) | |
| key = [] | |
| key.append(op) | |
| decrypt(key, string, pt) | |
| assertEqual(ExpectedStr,pt) | |
| return | |
| def encodeTest(): | |
| plainblock = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S'] | |
| encoded_block = [] | |
| encoding(plainblock, encoded_block, 0) | |
| before_rand_block = [] | |
| for i in range(52): | |
| before_rand_block.append(encoded_block[i]) | |
| Expectedblock_1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'z', 'c', 'z', 'g', 'j', 'A', 'A'] | |
| assertEqual(before_rand_block, Expectedblock_1) | |
| def encodeDecodeTest(): | |
| string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| ExpectedStr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| cube = str2cube(string) | |
| assertEqual(cube.cubing, ExpectedStr) | |
| string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| ExpectedStr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| buf = [] | |
| cube = str2cube(string) | |
| cube2str(cube,buf) | |
| assertEqual(buf, ExpectedStr) | |
| def unitTest(): | |
| #縦方向のテスト | |
| string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| ExpectedStr = ['a', 'n', 'c', 'd', 'z', 'f', 'g', 'L', 'i', 'j', 'k', 'l', 'm', 'U', 'o', 'p', 'q', 'r', 's', 'h', 'u', 'v', 'w', 'x', 'y', 'X', 'A', 'B', 'C', 'D', 'E', 'e', 'G', 'H', 'I', 'J', 'K', '!', 'M', 'N', 'O', 'P', 'Q', 'b', 'S', 'T', 'R', 'V', 'W', 'F', 'Y', 'Z', 't', '?'] | |
| cube = Cube(string) | |
| expected = Cube(ExpectedStr) | |
| op = CubeOP(1, 2, 1) | |
| key = [] | |
| key.append(op) | |
| cube.rotate(key[0]) | |
| assertEqual(cube.cubing, expected.cubing) | |
| #横方向テスト | |
| string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| ExpectedStr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'v', 'w', 'x', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| cube = Cube(string) | |
| expected = Cube(ExpectedStr) | |
| op = CubeOP(2, 2, 1) | |
| key = [] | |
| key.append(op) | |
| cube.rotate(key[0]) | |
| assertEqual(cube.cubing, expected.cubing) | |
| #回転方向テスト | |
| string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '?'] | |
| ExpectedStr = ['a', 'b', 'c', 'q', 'C', 'O', 'g', 'h', 'i', 'j', 'f', 'l', 'm', 'n', 'o', 'p', 'Y', 'r', 's', 't', 'u', 'v', 'e', 'x', 'y', 'z', 'A', 'B', 'X', 'D', 'E', 'F', 'G', 'H', 'd', 'J', 'K', 'L', 'M', 'N', 'W', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'k', 'w', 'I', 'Z', '!', '?'] | |
| cube = Cube(string) | |
| expected = Cube(ExpectedStr) | |
| op = CubeOP(3, 2, 1) | |
| key = [] | |
| key.append(op) | |
| cube.rotate(key[0]) | |
| assertEqual(cube.cubing, expected.cubing) | |
| def main(): | |
| print("-------unitTest-------") | |
| unitTest() | |
| print("------encodeDecodeTest------") | |
| encodeDecodeTest() | |
| print("-------encryptTest--------") | |
| encryptTest() | |
| print("----------decryptTest--------") | |
| decryptTest() | |
| print("---------encodeTest---------") | |
| encodeTest() | |
| print("-----cubingmodeEncryptDecryptTest------") | |
| cubingmodeEncodeDecodeTest() | |
| print("-----cubingmodeEncryptDecryptTest2------") | |
| cubingmodeEncodeDecodeTest2() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment