普通三進位制邏輯數學世界先驅者——伍耀暉先生面向全球公佈普通三進位制邏輯數學全部真值表科研成果(伍氏定律)
準備組建“國際三進位制科技聯盟” 著手製造三進位制 CPU
三進位制一個位元組用6個位表示 最高位為左 最低位為右表示 5 4 3 2 1 0 值範圍從0到727 包含二進位制位元組全值範圍
三進位制負數採用同二進位制一樣的原理——補碼錶示 但是與二進位制不同 很特別 1是不變的 只能0和2互變
TrinaryNot 0 1 2
2 1 0
TrinaryAnd 0 1 2 TrinaryNotAnd 0 1 2
0 0 0 0 0 2 2 2
1 0 1 1 1 2 1 1
2 0 1 2 2 2 1 0
TrinaryOr 0 1 2 TrinaryNotOr 0 1 2
0 0 1 2 0 2 1 0
1 1 1 2 1 1 1 0
2 2 2 2 2 0 0 0
(相當於Xor異或 用於加密)TrinaryXor 0 1 2 等價 0 1 2 等價 0 1 2 等價 0 1 2
0 0 0 2 0 0 2 2 0 0 1 2 0 0 1 2
1 1 1 1 1 1 1 1 1 0 1 2 1 2 1 0
2 2 2 0 2 2 0 0 2 2 1 0 2 2 1 0
(相當於Xand同或 用於加密)TrinaryXand 0 1 2 等價 0 1 2 等價 0 1 2 等價 0 1 2
0 2 0 0 0 2 2 0 0 2 1 0 0 2 1 0
1 1 1 1 1 1 1 1 1 0 1 2 1 2 1 0
2 0 2 2 2 0 0 2 2 0 1 2 2 0 1 2
普通三進位制邏輯數學特有
(用於雜湊碼)TrinaryAdd 0 1 2 TrinaryNotAdd 0 1 2
0 0 1 2 0 2 1 0
1 1 2 0 1 1 0 2
2 2 0 1 2 0 2 1
(用於雜湊碼)TrinarySubtraction 0 1 2 或 0 1 2 TrinaryNotSubtraction 0 1 2 或 0 1 2
0 0 2 1 0 0 1 2 0 2 0 1 0 2 1 0
1 1 0 2 1 2 0 1 1 1 2 0 1 0 2 1
2 2 1 0 2 1 2 0 2 0 1 2 2 1 0 2
(用於雜湊碼)TrinaryMultiplication 0 1 2 TrinaryNotMultiplication 0 1 2
0 0 0 0 0 2 2 2
1 0 1 2 1 2 1 0
2 0 2 1 2 2 0 1
(用於雜湊碼)TrinaryDivision 0 1 2 或 0 1 2 TrinaryNotDivision 0 1 2 或 0 1 2
0 0 0 0 0 0 0 0 0 2 2 2 0 2 2 2
1 0 1 0 1 0 1 2 1 2 1 2 1 2 1 0
2 0 2 1 2 0 0 1 2 2 0 1 2 2 2 1
以下是演示程式碼
C 原始碼
/******************************************************* * 作者:伍耀暉 Author: YaoHui.Wu * * 開源日期:2022年6月7日 Open Source Date: 2022-6-7 * * 國家:中國 Country: China * *******************************************************/ #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> void Usage() { printf("Usage\n\tEncryption: TrinaryCipher -e/-E Plaintext.file Ciphertext.file Password\n\tDecryption: TrinaryCipher -d/-D Ciphertext.file Plaintext.file Password\n"); } void Ternary(long long lNumeric, unsigned char *ucpTrinary) { if(lNumeric < 1) { ucpTrinary[0] = ucpTrinary[1] = ucpTrinary[2] = ucpTrinary[3] = ucpTrinary[4] = ucpTrinary[5] = 0; } else { for(long long i = 5; i >= 0; --i) { ucpTrinary[i] = lNumeric % 3; lNumeric /= 3; } } } // 0 ? 2 0 1 2 // 1 1 1 or ? 1 ? // 2 ? 0 2 1 0 void TernaryXor(unsigned char *ucpCiphertextOrPlaintext, unsigned char *ucpPassword) { for(long long j = 0; j < 6; ++j) { if(ucpCiphertextOrPlaintext[j] == 0 && ucpPassword[j] == 0) { ucpCiphertextOrPlaintext[j] = 0; } else if(ucpCiphertextOrPlaintext[j] == 0 && ucpPassword[j] == 1) { ucpCiphertextOrPlaintext[j] = 2; } else if(ucpCiphertextOrPlaintext[j] == 0 && ucpPassword[j] == 2) { ucpCiphertextOrPlaintext[j] = 2; } else if(ucpCiphertextOrPlaintext[j] == 1 && ucpPassword[j] == 0) { ucpCiphertextOrPlaintext[j] = 1; } else if(ucpCiphertextOrPlaintext[j] == 1 && ucpPassword[j] == 1) { ucpCiphertextOrPlaintext[j] = 1; } else if(ucpCiphertextOrPlaintext[j] == 1 && ucpPassword[j] == 2) { ucpCiphertextOrPlaintext[j] = 1; } else if(ucpCiphertextOrPlaintext[j] == 2 && ucpPassword[j] == 0) { ucpCiphertextOrPlaintext[j] = 2; } else if(ucpCiphertextOrPlaintext[j] == 2 && ucpPassword[j] == 1) { ucpCiphertextOrPlaintext[j] = 0; } else if(ucpCiphertextOrPlaintext[j] == 2 && ucpPassword[j] == 2) { ucpCiphertextOrPlaintext[j] = 0; } } } // 2 ? 0 2 1 0 // 1 1 1 or ? 1 ? // 0 ? 2 0 1 2 void TernaryXand(unsigned char *ucpCiphertextOrPlaintext, unsigned char *ucpPassword) { for(long long j = 0; j < 6; ++j) { if(ucpCiphertextOrPlaintext[j] == 0 && ucpPassword[j] == 0) { ucpCiphertextOrPlaintext[j] = 2; } else if(ucpCiphertextOrPlaintext[j] == 0 && ucpPassword[j] == 1) { ucpCiphertextOrPlaintext[j] = 0; } else if(ucpCiphertextOrPlaintext[j] == 0 && ucpPassword[j] == 2) { ucpCiphertextOrPlaintext[j] = 0; } else if(ucpCiphertextOrPlaintext[j] == 1 && ucpPassword[j] == 0) { ucpCiphertextOrPlaintext[j] = 1; } else if(ucpCiphertextOrPlaintext[j] == 1 && ucpPassword[j] == 1) { ucpCiphertextOrPlaintext[j] = 1; } else if(ucpCiphertextOrPlaintext[j] == 1 && ucpPassword[j] == 2) { ucpCiphertextOrPlaintext[j] = 1; } else if(ucpCiphertextOrPlaintext[j] == 2 && ucpPassword[j] == 0) { ucpCiphertextOrPlaintext[j] = 0; } else if(ucpCiphertextOrPlaintext[j] == 2 && ucpPassword[j] == 1) { ucpCiphertextOrPlaintext[j] = 2; } else if(ucpCiphertextOrPlaintext[j] == 2 && ucpPassword[j] == 2) { ucpCiphertextOrPlaintext[j] = 2; } } } long long main(long long argc, char *argv[]) { if(argc < 5) { Usage(); } else if(*(short*)argv[1] == 0x452D || *(short*)argv[1] == 0x652D) { unsigned char ucPasswordLength = -1; while(argv[4][++ucPasswordLength]); unsigned char *ucpPassword = malloc(6 * ucPasswordLength); for(unsigned char i = 0; i < ucPasswordLength; ++i) { Ternary(argv[4][i], ucpPassword + 6 * i); } struct stat tStatFileSize; stat(argv[2], &tStatFileSize); long long lFileSize = tStatFileSize.st_size; int fdPlaintextOrCiphertext = open(argv[2], O_BINARY | O_RDONLY, S_IREAD | S_IWRITE); unsigned char *ucpPlaintext = malloc(lFileSize), ucaPlaintextOrCiphertext[6]; read(fdPlaintextOrCiphertext, ucpPlaintext, lFileSize); close(fdPlaintextOrCiphertext); unsigned short *uspCiphertext = malloc(2 * lFileSize); for(long long j = 0, k = 0; j < lFileSize; ++j) { Ternary(ucpPlaintext[j], ucaPlaintextOrCiphertext); TernaryXor(ucaPlaintextOrCiphertext, ucpPassword + 6 * k); //TernaryXand(ucaPlaintextOrCiphertext, ucpPassword + 6 * k); uspCiphertext[j] = 243 * ucaPlaintextOrCiphertext[0] + 81 * ucaPlaintextOrCiphertext[1] + 27 * ucaPlaintextOrCiphertext[2] + 9 * ucaPlaintextOrCiphertext[3] + 3 * ucaPlaintextOrCiphertext[4] + ucaPlaintextOrCiphertext[5]; k = ++k % ucPasswordLength; } fdPlaintextOrCiphertext = open(argv[3], O_BINARY | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); write(fdPlaintextOrCiphertext, uspCiphertext, 2 * lFileSize); close(fdPlaintextOrCiphertext); free(uspCiphertext); free(ucpPlaintext); } else if(*(short*)argv[1] == 0x442D || *(short*)argv[1] == 0x642D) { unsigned char ucPasswordLength = -1; while(argv[4][++ucPasswordLength]); unsigned char *ucpPassword = malloc(6 * ucPasswordLength); for(long long i = 0; i < ucPasswordLength; ++i) { Ternary(argv[4][i], ucpPassword + 6 * i); } struct stat tStatFileSize; stat(argv[2], &tStatFileSize); long long lFileSize = tStatFileSize.st_size; int fdCiphertextOrPlaintext = open(argv[2], O_BINARY | O_RDONLY, S_IREAD | S_IWRITE); unsigned short *uspCiphertext = malloc(lFileSize); read(fdCiphertextOrPlaintext, uspCiphertext, lFileSize); close(fdCiphertextOrPlaintext); lFileSize /= 2; unsigned char *ucpPlaintext = malloc(lFileSize), ucaCiphertextOrPlaintext[6]; for(long long j = 0, k = 0; j < lFileSize; ++j) { Ternary(uspCiphertext[j], ucaCiphertextOrPlaintext); TernaryXor(ucaCiphertextOrPlaintext, ucpPassword + 6 * k); //TernaryXand(ucaCiphertextOrPlaintext, ucpPassword + 6 * k); ucpPlaintext[j] = 243 * ucaCiphertextOrPlaintext[0] + 81 * ucaCiphertextOrPlaintext[1] + 27 * ucaCiphertextOrPlaintext[2] + 9 * ucaCiphertextOrPlaintext[3] + 3 * ucaCiphertextOrPlaintext[4] + ucaCiphertextOrPlaintext[5]; k = ++k % ucPasswordLength; } fdCiphertextOrPlaintext = open(argv[3], O_BINARY | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); write(fdCiphertextOrPlaintext, ucpPlaintext, lFileSize); close(fdCiphertextOrPlaintext); free(ucpPlaintext); free(uspCiphertext); } else { Usage(); } return 0; }
Python 原始碼
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #******************************************************* # 作者:伍耀暉 Author: YaoHui.Wu * # 開源日期:2022年6月7日 Open Source Date: 2022-6-7 * # 國家:中國 Country: China * #******************************************************* import sys def Usage(): print("Usage\n\tEncryption: python TrinaryCipher.py -e/-E Plaintext.file Ciphertext.file Password\n\tDecryption: python TrinaryCipher.py -d/-D Ciphertext.file Plaintext.file Password") def Ternary(iNumeric): i, lTrinary = 0, [0, 0, 0, 0, 0, 0] if iNumeric and i < 6: while iNumeric: iNumeric, iRemainder = divmod(iNumeric, 3) lTrinary[i] = iRemainder i += 1 return lTrinary # 0 ? 2 0 1 2 # 1 1 1 or ? 1 ? # 2 ? 0 2 1 0 def TernaryXor(lTrinary, lPassword): for l in range(6): if lTrinary[l] == lPassword[l] == 0: lTrinary[l] = "0" elif lTrinary[l] == 0 and lPassword[l] == 1: lTrinary[l] = "2" elif lTrinary[l] == 0 and lPassword[l] == 2: lTrinary[l] = "2" elif lTrinary[l] == 1 and lPassword[l] == 0: lTrinary[l] = "1" elif lTrinary[l] == lPassword[l] == 1: lTrinary[l] = "1" elif lTrinary[l] == 1 and lPassword[l] == 2: lTrinary[l] = "1" elif lTrinary[l] == 2 and lPassword[l] == 0: lTrinary[l] = "2" elif lTrinary[l] == 2 and lPassword[l] == 1: lTrinary[l] = "0" elif lTrinary[l] == lPassword[l] == 2: lTrinary[l] = "0" return int("".join(lTrinary[::-1]), 3) # 2 ? 0 2 1 0 # 1 1 1 or ? 1 ? # 0 ? 2 0 1 2 def TernaryXand(lTrinary, lPassword): for l in range(6): if lTrinary[l] == lPassword[l] == 0: lTrinary[l] = "2" elif lTrinary[l] == 0 and lPassword[l] == 1: lTrinary[l] = "0" elif lTrinary[l] == 0 and lPassword[l] == 2: lTrinary[l] = "0" elif lTrinary[l] == 1 and lPassword[l] == 0: lTrinary[l] = "1" elif lTrinary[l] == lPassword[l] == 1: lTrinary[l] = "1" elif lTrinary[l] == 1 and lPassword[l] == 2: lTrinary[l] = "1" elif lTrinary[l] == 2 and lPassword[l] == 0: lTrinary[l] = "0" elif lTrinary[l] == 2 and lPassword[l] == 1: lTrinary[l] = "2" elif lTrinary[l] == lPassword[l] == 2: lTrinary[l] = "2" return int("".join(lTrinary[::-1]), 3) if __name__ == "__main__": if len(sys.argv) < 5: Usage() elif sys.argv[1] == "-e" or sys.argv[1] == "-E": strPassword, lPassword, lCiphertext = sys.argv[4], [], [] iPasswordLength, i = len(strPassword), 0 for j in range(iPasswordLength): lPassword.append(Ternary(ord(strPassword[j]))) with open(sys.argv[2], "br") as fdPlaintext: bPlaintext = fdPlaintext.read() iFileSize = fdPlaintext.tell() for k in range(iFileSize): lCiphertext.append(TernaryXor(Ternary(bPlaintext[k]), lPassword[i])) #lCiphertext.append(TernaryXand(Ternary(bPlaintext[k]), lPassword[i])) i = (i + 1) % iPasswordLength with open(sys.argv[3], "bw") as fdCiphertext: for iCiphertext in lCiphertext: fdCiphertext.write(iCiphertext.to_bytes(2, "little")) elif sys.argv[1] == "-d" or sys.argv[1] == "-D": strPassword, lPassword, lPlaintext = sys.argv[4], [], [] iPasswordLength, i = len(strPassword), 0 for j in range(iPasswordLength): lPassword.append(Ternary(ord(strPassword[j]))) with open(sys.argv[2], "br") as fdCiphertext: bCiphertext = fdCiphertext.read(2) while bCiphertext: lPlaintext.append(TernaryXor(Ternary(int.from_bytes(bCiphertext, "little")), lPassword[i])) #lPlaintext.append(TernaryXand(Ternary(int.from_bytes(bCiphertext, "little")), lPassword[i])) i = (i + 1) % iPasswordLength bCiphertext = fdCiphertext.read(2) with open(sys.argv[3], "bw") as fdPlaintext: for iPlaintext in lPlaintext: fdPlaintext.write(iPlaintext.to_bytes(1, "little")) else: Usage()
Java 原始碼
/******************************************************* * 作者:伍耀暉 Author: YaoHui.Wu * * 開源日期:2022年6月7日 Open Source Date: 2022-6-7 * * 國家:中國 Country: China * *******************************************************/ import java.io.*; public class TrinaryCipher { private static void Usage() { System.out.println("Usage\n\tEncryption: java TrinaryCipher -e/-E Plaintext.file Ciphertext.file Password\n\tDecryption: java TrinaryCipher -d/-D Ciphertext.file Plaintext.file Password\n"); } private static void Ternary(int iNumeric, byte[] baTrinary) { if(iNumeric < 1) { baTrinary[0] = baTrinary[1] = baTrinary[2] = baTrinary[3] = baTrinary[4] = baTrinary[5] = 0; } else { for(int i = 5; i >= 0; --i) { baTrinary[i] = (byte)(iNumeric % 3); iNumeric /= 3; } } } // 0 ? 2 0 1 2 // 1 1 1 or ? 1 ? // 2 ? 0 2 1 0 private static void TernaryXor(byte[] baCiphertextOrPlaintext, byte[] baPassword) { for(int j = 0; j < 6; ++j) { if(baCiphertextOrPlaintext[j] == 0 && baPassword[j] == 0) { baCiphertextOrPlaintext[j] = 0; } else if(baCiphertextOrPlaintext[j] == 0 && baPassword[j] == 1) { baCiphertextOrPlaintext[j] = 2; } else if(baCiphertextOrPlaintext[j] == 0 && baPassword[j] == 2) { baCiphertextOrPlaintext[j] = 2; } else if(baCiphertextOrPlaintext[j] == 1 && baPassword[j] == 0) { baCiphertextOrPlaintext[j] = 1; } else if(baCiphertextOrPlaintext[j] == 1 && baPassword[j] == 1) { baCiphertextOrPlaintext[j] = 1; } else if(baCiphertextOrPlaintext[j] == 1 && baPassword[j] == 2) { baCiphertextOrPlaintext[j] = 1; } else if(baCiphertextOrPlaintext[j] == 2 && baPassword[j] == 0) { baCiphertextOrPlaintext[j] = 2; } else if(baCiphertextOrPlaintext[j] == 2 && baPassword[j] == 1) { baCiphertextOrPlaintext[j] = 0; } else if(baCiphertextOrPlaintext[j] == 2 && baPassword[j] == 2) { baCiphertextOrPlaintext[j] = 0; } } } // 2 ? 0 2 1 0 // 1 1 1 or ? 1 ? // 0 ? 2 0 1 2 private static void TernaryXand(byte[] baCiphertextOrPlaintext, byte[] baPassword) { for(int j = 0; j < 6; ++j) { if(baCiphertextOrPlaintext[j] == 0 && baPassword[j] == 0) { baCiphertextOrPlaintext[j] = 2; } else if(baCiphertextOrPlaintext[j] == 0 && baPassword[j] == 1) { baCiphertextOrPlaintext[j] = 0; } else if(baCiphertextOrPlaintext[j] == 0 && baPassword[j] == 2) { baCiphertextOrPlaintext[j] = 0; } else if(baCiphertextOrPlaintext[j] == 1 && baPassword[j] == 0) { baCiphertextOrPlaintext[j] = 1; } else if(baCiphertextOrPlaintext[j] == 1 && baPassword[j] == 1) { baCiphertextOrPlaintext[j] = 1; } else if(baCiphertextOrPlaintext[j] == 1 && baPassword[j] == 2) { baCiphertextOrPlaintext[j] = 1; } else if(baCiphertextOrPlaintext[j] == 2 && baPassword[j] == 0) { baCiphertextOrPlaintext[j] = 0; } else if(baCiphertextOrPlaintext[j] == 2 && baPassword[j] == 1) { baCiphertextOrPlaintext[j] = 2; } else if(baCiphertextOrPlaintext[j] == 2 && baPassword[j] == 2) { baCiphertextOrPlaintext[j] = 2; } } } public static void main(String[] args) { if(args.length < 4) { Usage(); } else if(args[0].equals("-e") || args[0].equals("-E")) { int iPasswordLength = args[3].length(); byte[][] baPassword = new byte[iPasswordLength][6]; for(int i = 0; i < iPasswordLength; ++i) { Ternary(args[3].getBytes()[i], baPassword[i]); } RandomAccessFile rafPlaintext = null, rafCiphertext = null; try { rafPlaintext = new RandomAccessFile(args[1], "r"); rafCiphertext = new RandomAccessFile(args[2], "rw"); long lFileSize = rafPlaintext.length(); rafCiphertext.setLength(2 * lFileSize); byte[] baPlaintextOrCiphertext = new byte[6]; int k = 0; for(long j = 0; j < lFileSize; ++j) { Ternary(rafPlaintext.readUnsignedByte(), baPlaintextOrCiphertext); TernaryXor(baPlaintextOrCiphertext, baPassword[k]); //TernaryXand(baPlaintextOrCiphertext, baPassword[k]); rafCiphertext.writeShort(243 * baPlaintextOrCiphertext[0] + 81 * baPlaintextOrCiphertext[1] + 27 * baPlaintextOrCiphertext[2] + 9 * baPlaintextOrCiphertext[3] + 3 * baPlaintextOrCiphertext[4] + baPlaintextOrCiphertext[5]); k = ++k % iPasswordLength; } } catch(Exception e) { e.printStackTrace(); } finally { try { if(rafCiphertext != null) { rafCiphertext.close(); } if(rafPlaintext != null) { rafPlaintext.close(); } } catch(Exception e) { e.printStackTrace(); } } } else if(args[0].equals("-d") || args[0].equals("-D")) { int iPasswordLength = args[3].length(); byte[][] baPassword = new byte[iPasswordLength][6]; for(int i = 0; i < iPasswordLength; ++i) { Ternary(args[3].getBytes()[i], baPassword[i]); } RandomAccessFile rafCiphertext = null, rafPlaintext = null; try { rafCiphertext = new RandomAccessFile(args[1], "r"); rafPlaintext = new RandomAccessFile(args[2], "rw"); long lFileSize = rafCiphertext.length() / 2; rafPlaintext.setLength(lFileSize); byte[] baCiphertextOrPlaintext = new byte[6]; int k = 0; for(long j = 0; j < lFileSize; ++j) { Ternary(rafCiphertext.readUnsignedShort(), baCiphertextOrPlaintext); TernaryXor(baCiphertextOrPlaintext, baPassword[k]); //TernaryXand(baCiphertextOrPlaintext, baPassword[k]); rafPlaintext.writeByte(243 * baCiphertextOrPlaintext[0] + 81 * baCiphertextOrPlaintext[1] + 27 * baCiphertextOrPlaintext[2] + 9 * baCiphertextOrPlaintext[3] + 3 * baCiphertextOrPlaintext[4] + baCiphertextOrPlaintext[5]); k = ++k % iPasswordLength; } } catch(Exception e) { e.printStackTrace(); } finally { try { if(rafPlaintext != null) { rafPlaintext.close(); } if(rafCiphertext != null) { rafCiphertext.close(); } } catch(Exception e) { e.printStackTrace(); } } } else { Usage(); } } }
Pascal 原始碼
(******************************************************* * 作者:伍耀暉 Author: YaoHui.Wu * * 開源日期:2022年6月7日 Open Source Date: 2022-6-7 * * 國家:中國 Country: China * *******************************************************) (* Compiled by free pascal. free pascal website: www.freepascal.org *) Program TrinaryCipher; Type TrinaryArray = Array[0..5] Of Byte; Procedure Usage(); Begin writeln('Usage'#10#9'Encryption: TrinaryCipher -e/-E Plaintext.file Ciphertext.file Password'#10#9'Decryption: TrinaryCipher -d/-D Ciphertext.file Plaintext.file Password'); End; Procedure Ternary(wNumeric : Word; Var baTrinary : TrinaryArray); Var i : Byte; Begin If wNumeric < 1 Then Begin baTrinary[0] := 0; baTrinary[1] := 0; baTrinary[2] := 0; baTrinary[3] := 0; baTrinary[4] := 0; baTrinary[5] := 0; End Else Begin For i := 5 DownTo 0 Do Begin baTrinary[i] := wNumeric Mod 3; wNumeric := wNumeric Div 3; End; End; End; (* 0 ? 2 0 1 2 1 1 1 or ? 1 ? 2 ? 0 2 1 0 *) Procedure TernaryXor(Var baCiphertextOrPlaintext : TrinaryArray; Var baPassword : TrinaryArray); Var j : Byte; Begin For j := 0 To 5 Do Begin If (baCiphertextOrPlaintext[j] = 0) And (baPassword[j] = 0) Then Begin baCiphertextOrPlaintext[j] := 0; End Else If (baCiphertextOrPlaintext[j] = 0) And (baPassword[j] = 1) Then Begin baCiphertextOrPlaintext[j] := 2; End Else If (baCiphertextOrPlaintext[j] = 0) And (baPassword[j] = 2) Then Begin baCiphertextOrPlaintext[j] := 2; End Else If (baCiphertextOrPlaintext[j] = 1) And (baPassword[j] = 0) Then Begin baCiphertextOrPlaintext[j] := 1; End Else If (baCiphertextOrPlaintext[j] = 1) And (baPassword[j] = 1) Then Begin baCiphertextOrPlaintext[j] := 1; End Else If (baCiphertextOrPlaintext[j] = 1) And (baPassword[j] = 2) Then Begin baCiphertextOrPlaintext[j] := 1; End Else If (baCiphertextOrPlaintext[j] = 2) And (baPassword[j] = 0) Then Begin baCiphertextOrPlaintext[j] := 2; End Else If (baCiphertextOrPlaintext[j] = 2) And (baPassword[j] = 1) Then Begin baCiphertextOrPlaintext[j] := 0; End Else If (baCiphertextOrPlaintext[j] = 2) And (baPassword[j] = 2) Then Begin baCiphertextOrPlaintext[j] := 0; End End; End; (* 2 ? 0 2 1 0 1 1 1 or ? 1 ? 0 ? 2 0 1 2 *) Procedure TernaryXand(Var baCiphertextOrPlaintext : TrinaryArray; Var baPassword : TrinaryArray); Var j : Byte; Begin For j := 0 To 5 Do Begin If (baCiphertextOrPlaintext[j] = 0) And (baPassword[j] = 0) Then Begin baCiphertextOrPlaintext[j] := 2; End Else If (baCiphertextOrPlaintext[j] = 0) And (baPassword[j] = 1) Then Begin baCiphertextOrPlaintext[j] := 0; End Else If (baCiphertextOrPlaintext[j] = 0) And (baPassword[j] = 2) Then Begin baCiphertextOrPlaintext[j] := 0; End Else If (baCiphertextOrPlaintext[j] = 1) And (baPassword[j] = 0) Then Begin baCiphertextOrPlaintext[j] := 1; End Else If (baCiphertextOrPlaintext[j] = 1) And (baPassword[j] = 1) Then Begin baCiphertextOrPlaintext[j] := 1; End Else If (baCiphertextOrPlaintext[j] = 1) And (baPassword[j] = 2) Then Begin baCiphertextOrPlaintext[j] := 1; End Else If (baCiphertextOrPlaintext[j] = 2) And (baPassword[j] = 0) Then Begin baCiphertextOrPlaintext[j] := 0; End Else If (baCiphertextOrPlaintext[j] = 2) And (baPassword[j] = 1) Then Begin baCiphertextOrPlaintext[j] := 2; End Else If (baCiphertextOrPlaintext[j] = 2) And (baPassword[j] = 2) Then Begin baCiphertextOrPlaintext[j] := 2; End End; End; Var i, k, bPasswordLength : Byte; j, uiFileSize : LongWord; baPassword : Array Of TrinaryArray; baPlaintextOrCiphertext : TrinaryArray; fdPlaintextOrCiphertext : File; bpPlaintext : PByte; wpCiphertext : PWord; Begin If(ParamCount < 4) Then Begin Usage(); End Else If (ParamStr(1) = '-e') Or (ParamStr(1) = '-E') Then Begin bPasswordLength := Length(ParamStr(4)); SetLength(baPassword, bPasswordLength); For i := 1 To bPasswordLength Do Begin Ternary(Ord(ParamStr(4)[i]), baPassword[i - 1]); End; Assign(fdPlaintextOrCiphertext, ParamStr(2)); Reset(fdPlaintextOrCiphertext, 1); uiFileSize := FileSize(fdPlaintextOrCiphertext); bpPlaintext := GetMem(uiFileSize); BlockRead(fdPlaintextOrCiphertext, bpPlaintext^, uiFileSize); Close(fdPlaintextOrCiphertext); wpCiphertext := GetMem(2 * uiFileSize); k := 0; For j := 0 To uiFileSize - 1 Do Begin Ternary(bpPlaintext[j], baPlaintextOrCiphertext); TernaryXor(baPlaintextOrCiphertext, baPassword[k]); (*TernaryXand(baPlaintextOrCiphertext, baPassword[k]);*) wpCiphertext[j] := 243 * baPlaintextOrCiphertext[0] + 81 * baPlaintextOrCiphertext[1] + 27 * baPlaintextOrCiphertext[2] + 9 * baPlaintextOrCiphertext[3] + 3 * baPlaintextOrCiphertext[4] + baPlaintextOrCiphertext[5]; k := (k + 1) Mod bPasswordLength; End; Assign(fdPlaintextOrCiphertext, Paramstr(3)); Rewrite(fdPlaintextOrCiphertext, 1); BlockWrite(fdPlaintextOrCiphertext, wpCiphertext^, 2 * uiFileSize); Close(fdPlaintextOrCiphertext); FreeMem(wpCiphertext); FreeMem(bpPlaintext); End Else If (paramstr(1) = '-d') Or (paramstr(1) = '-D') Then Begin bPasswordLength := Length(ParamStr(4)); SetLength(baPassword, bPasswordLength); For i := 1 To bPasswordLength Do Begin Ternary(Ord(ParamStr(4)[i]), baPassword[i - 1]); End; Assign(fdPlaintextOrCiphertext, ParamStr(2)); Reset(fdPlaintextOrCiphertext, 1); uiFileSize := FileSize(fdPlaintextOrCiphertext); wpCiphertext := GetMem(uiFileSize); BlockRead(fdPlaintextOrCiphertext, wpCiphertext^, uiFileSize); Close(fdPlaintextOrCiphertext); uiFileSize := uiFileSize Div 2; bpPlaintext := GetMem(uiFileSize); k := 0; For j := 0 To uiFileSize - 1 Do Begin Ternary(wpCiphertext[j], baPlaintextOrCiphertext); TernaryXor(baPlaintextOrCiphertext, baPassword[k]); (*TernaryXand(baPlaintextOrCiphertext, baPassword[k]);*) bpPlaintext[j] := 243 * baPlaintextOrCiphertext[0] + 81 * baPlaintextOrCiphertext[1] + 27 * baPlaintextOrCiphertext[2] + 9 * baPlaintextOrCiphertext[3] + 3 * baPlaintextOrCiphertext[4] + baPlaintextOrCiphertext[5]; k := (k + 1) Mod bPasswordLength; End; Assign(fdPlaintextOrCiphertext, Paramstr(3)); Rewrite(fdPlaintextOrCiphertext, 1); BlockWrite(fdPlaintextOrCiphertext, bpPlaintext^, uiFileSize); Close(fdPlaintextOrCiphertext); FreeMem(bpPlaintext); FreeMem(wpCiphertext); End Else Begin Usage(); End End.
Basic 原始碼
/'****************************************************** * 作者:伍耀暉 Author: YaoHui.Wu * * 開源日期:2022年6月7日 Open Source Date: 2022-6-7 * * 國家:中國 Country: China * ******************************************************'/ ' Compiled by free basic. free basic website: www.freebasic.net #include "file.bi" Sub Usage() print "Usage" & Chr(10) & Chr(9) & "Encryption: TrinaryCipher -e/-E Plaintext.file Ciphertext.file Password" & Chr(10) & Chr(9) & "Decryption: TrinaryCipher -d/-D Ciphertext.file Plaintext.file Password" End Sub Sub Ternary(ByVal usNumeric As UShort, ubpTrinary As UByte Pointer) If usNumeric < 1 Then ubpTrinary[0] = 0 ubpTrinary[1] = 0 ubpTrinary[2] = 0 ubpTrinary[3] = 0 ubpTrinary[4] = 0 ubpTrinary[5] = 0 Else For i As Byte = 5 To 0 Step -1 ubpTrinary[i] = usNumeric Mod 3 usNumeric \= 3 Next i End If End Sub /' 0 ? 2 0 1 2 1 1 1 or ? 1 ? 2 ? 0 2 1 0 '/ Sub TernaryXor(ubaPlaintextOrCiphertext() As UByte, ubpPassword As UByte Pointer) For j As UByte = 0 To 5 If ubaPlaintextOrCiphertext(j) = 0 AndAlso ubpPassword[j] = 0 Then ubaPlaintextOrCiphertext(j) = 2 Elseif ubaPlaintextOrCiphertext(j) = 0 AndAlso ubpPassword[j] = 1 Then ubaPlaintextOrCiphertext(j) = 0 Elseif ubaPlaintextOrCiphertext(j) = 0 AndAlso ubpPassword[j] = 2 Then ubaPlaintextOrCiphertext(j) = 0 Elseif ubaPlaintextOrCiphertext(j) = 1 AndAlso ubpPassword[j] = 0 Then ubaPlaintextOrCiphertext(j) = 1 Elseif ubaPlaintextOrCiphertext(j) = 1 AndAlso ubpPassword[j] = 1 Then ubaPlaintextOrCiphertext(j) = 1 Elseif ubaPlaintextOrCiphertext(j) = 1 AndAlso ubpPassword[j] = 2 Then ubaPlaintextOrCiphertext(j) = 1 Elseif ubaPlaintextOrCiphertext(j) = 2 AndAlso ubpPassword[j] = 0 Then ubaPlaintextOrCiphertext(j) = 0 Elseif ubaPlaintextOrCiphertext(j) = 2 AndAlso ubpPassword[j] = 1 Then ubaPlaintextOrCiphertext(j) = 2 Elseif ubaPlaintextOrCiphertext(j) = 2 AndAlso ubpPassword[j] = 2 Then ubaPlaintextOrCiphertext(j) = 2 End If Next j End Sub /' 2 ? 0 2 1 0 1 1 1 or ? 1 ? 0 ? 2 0 1 2 '/ Sub TernaryXand(ubaPlaintextOrCiphertext() As UByte, ubpPassword As UByte Pointer) For j As UByte = 0 To 5 If ubaPlaintextOrCiphertext(j) = 0 AndAlso ubpPassword[j] = 0 Then ubaPlaintextOrCiphertext(j) = 2 Elseif ubaPlaintextOrCiphertext(j) = 0 AndAlso ubpPassword[j] = 1 Then ubaPlaintextOrCiphertext(j) = 0 Elseif ubaPlaintextOrCiphertext(j) = 0 AndAlso ubpPassword[j] = 2 Then ubaPlaintextOrCiphertext(j) = 0 Elseif ubaPlaintextOrCiphertext(j) = 1 AndAlso ubpPassword[j] = 0 Then ubaPlaintextOrCiphertext(j) = 1 Elseif ubaPlaintextOrCiphertext(j) = 1 AndAlso ubpPassword[j] = 1 Then ubaPlaintextOrCiphertext(j) = 1 Elseif ubaPlaintextOrCiphertext(j) = 1 AndAlso ubpPassword[j] = 2 Then ubaPlaintextOrCiphertext(j) = 1 Elseif ubaPlaintextOrCiphertext(j) = 2 AndAlso ubpPassword[j] = 0 Then ubaPlaintextOrCiphertext(j) = 0 Elseif ubaPlaintextOrCiphertext(j) = 2 AndAlso ubpPassword[j] = 1 Then ubaPlaintextOrCiphertext(j) = 2 Elseif ubaPlaintextOrCiphertext(j) = 2 AndAlso ubpPassword[j] = 2 Then ubaPlaintextOrCiphertext(j) = 2 End If Next j End Sub Sub Main() Dim As UByte k = 0, ubCLAA = 1, ubPasswordLength, ubaPassword(), ubaPlaintextOrCiphertext(5) Dim As String strPassword Dim As UInteger uiFileSize Dim As UByte Pointer ubpPlaintext Dim As UShort Pointer uspCiphertext Do Dim As String strCLA = Command(ubCLAA) If Len(strCLA) = 0 Then Exit Do End If ubCLAA += 1 Loop If ubCLAA < 5 Then Usage() Elseif Command(1) = "-e" OrElse Command(1) = "-E" Then strPassword = Command(4) ubPasswordLength = Len(Command(4)) ReDim ubaPassword(ubPasswordLength - 1, 5) For i As UByte = 0 To ubPasswordLength - 1 Ternary(strPassword[i], @ubaPassword(i, 0)) Next i uiFileSize = FileLen(Command(2)) ubpPlaintext = New UByte[uiFileSize] Open Command(2) For Binary Access Read As #3 Get #3, , *ubpPlaintext, uiFileSize Close #3 uspCiphertext = New UShort[uiFileSize] For j As ULong = 0 To uiFileSize - 1 Ternary(ubpPlaintext[j], @ubaPlaintextOrCiphertext(0)) TernaryXor(ubaPlaintextOrCiphertext(), @ubaPassword(k, 0)) 'TernaryXand(ubaPlaintextOrCiphertext(), @ubaPassword(k, 0)) uspCiphertext[j] = 243 * ubaPlaintextOrCiphertext(0) + 81 * ubaPlaintextOrCiphertext(1) + 27 * ubaPlaintextOrCiphertext(2) + 9 * ubaPlaintextOrCiphertext(3) + 3 * ubaPlaintextOrCiphertext(4) + ubaPlaintextOrCiphertext(5) k = (k + 1) Mod ubPasswordLength Next j Delete ubpPlaintext Open Command(3) For Binary Access Write As #4 Put #4, , *uspCiphertext, uiFileSize Close #4 Delete uspCiphertext Elseif Command(1) = "-d" OrElse Command(1) = "-D" Then strPassword = Command(4) ubPasswordLength = Len(Command(4)) ReDim ubaPassword(ubPasswordLength - 1, 5) For i As UByte = 0 To ubPasswordLength - 1 Ternary(strPassword[i], @ubaPassword(i, 0)) Next i uiFileSize = FileLen(Command(2)) \ 2 uspCiphertext = New UShort[uiFileSize] Open Command(2) For Binary Access Read As #3 Get #3, , *uspCiphertext, uiFileSize Close #3 ubpPlaintext = New UByte[uiFileSize] For j As UInteger = 0 To uiFileSize - 1 Ternary(uspCiphertext[j], @ubaPlaintextOrCiphertext(0)) TernaryXor(ubaPlaintextOrCiphertext(), @ubaPassword(k, 0)) 'TernaryXand(ubaPlaintextOrCiphertext(), @ubaPassword(k, 0)) ubpPlaintext[j] = 243 * ubaPlaintextOrCiphertext(0) + 81 * ubaPlaintextOrCiphertext(1) + 27 * ubaPlaintextOrCiphertext(2) + 9 * ubaPlaintextOrCiphertext(3) + 3 * ubaPlaintextOrCiphertext(4) + ubaPlaintextOrCiphertext(5) k = (k + 1) Mod ubPasswordLength Next j Delete uspCiphertext Open Command(3) For Binary Access Write As #4 Put #4, , *ubpPlaintext, uiFileSize Close #4 Delete ubpPlaintext Else Usage() End If End Sub Main()