php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 465|回复: 0

三进制加解密程序

[复制链接]

2623

主题

2630

帖子

9319

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6574
贡献
0
注册时间
2021-4-14
最后登录
2024-4-26
在线时间
667 小时
QQ
发表于 2022-5-28 20:08:01 | 显示全部楼层 |阅读模式
三进制加解密程序
[mw_shl_code=python,true]#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 作者:伍耀晖        Author: YaoHui.Wu
# 开源日期:2022年5月27日        Open Source Date: 2022-5-27
# 国家:中国        Country: China

import sys

def Ternary(iNumeric, iRadix):
    i, lTrinary = 0, [0, 0, 0, 0, 0, 0]

    if iNumeric and i < 6:
        while iNumeric:
            iNumeric, iRemainder = divmod(iNumeric, iRadix)

            lTrinary = iRemainder

            i += 1

    return lTrinary

if __name__ == "__main__":
    if len(sys.argv) < 4: print("Usage\n\tEncryption: TrinaryCipher -e/-E Plaintext.file Ciphertext.file Password\n\tDecrytion: TrinaryCipher -d/-D Ciphertext.file Plaintext.file Password")

    elif sys.argv[1] == "-e" or sys.argv[1] == "-E":
        strPassword, lData = sys.argv[4], []

        iLength, k = len(strPassword), 0

        with open(sys.argv[2], "br") as fdPlaintext:
            bData = fdPlaintext.read()

            iFileSize = fdPlaintext.tell()

        for i in range(iFileSize):
            lTrinary, lPassword, strData = Ternary(bData, 3), Ternary(ord(strPassword[k]), 3), ""

            for j in range(6):
                if lTrinary[j] == lPassword[j] == 0: strData += "0"

                elif lTrinary[j] == 0 and lPassword[j] == 1: strData += "2"

                elif lTrinary[j] == 0 and lPassword[j] == 2: strData += "2"

                elif lTrinary[j] == 1 and lPassword[j] == 0: strData += "1"

                elif lTrinary[j] == lPassword[j] == 1: strData += "1"

                elif lTrinary[j] == 1 and lPassword[j] == 2: strData += "1"

                elif lTrinary[j] == 2 and lPassword[j] == 0: strData += "2"

                elif lTrinary[j] == 2 and lPassword[j] == 1: strData += "0"

                elif lTrinary[j] == lPassword[j] == 2: strData += "0"

            lData.append(int(strData[::-1], 3))

            k = (k + 1) % iLength

        with open(sys.argv[3], "bw") as fdCiphertext:
            for iData in lData:
                fdCiphertext.write(iData.to_bytes(2, "little"))

    elif sys.argv[1] == "-d" or sys.argv[1] == "-D":
        strPassword, lData = sys.argv[4], []

        iLength, k = len(strPassword), 0

        with open(sys.argv[2], "br") as fdCiphertext:
            bData = fdCiphertext.read(2)

            while bData:
                lTrinary, lPassword, strData = Ternary(int.from_bytes(bData, "little"), 3), Ternary(ord(strPassword[k]), 3), ""

                for j in range(6):
                    if lTrinary[j] == lPassword[j] == 0: strData += "0"

                    elif lTrinary[j] == 0 and lPassword[j] == 1: strData += "2"

                    elif lTrinary[j] == 0 and lPassword[j] == 2: strData += "2"

                    elif lTrinary[j] == 1 and lPassword[j] == 0: strData += "1"

                    elif lTrinary[j] == lPassword[j] == 1: strData += "1"

                    elif lTrinary[j] == 1 and lPassword[j] == 2: strData += "1"

                    elif lTrinary[j] == 2 and lPassword[j] == 0: strData += "2"

                    elif lTrinary[j] == 2 and lPassword[j] == 1: strData += "0"

                    elif lTrinary[j] == lPassword[j] == 2: strData += "0"

                lData.append(int(strData[::-1], 3))

                k = (k + 1) % iLength

                bData = fdCiphertext.read(2)

        with open(sys.argv[3], "bw") as fdPlaintext:
            for iData in lData:
                fdPlaintext.write(iData.to_bytes(1, "little"))

    else: print("Usage\n\tEncryption: TrinaryCipher -e/-E Plaintext.file Ciphertext.file Password\n\tDecrytion: TrinaryCipher -d/-D Ciphertext.file Plaintext.file Password")[/mw_shl_code]





上一篇:c语言约瑟夫生死游戏
下一篇:流星雨网页特效
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-4-26 10:41 , Processed in 0.185666 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表