From e15ac027e540a971fff8ee27d1bc3e04e9ba2fc0 Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Wed, 28 Aug 2019 00:49:15 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E6=A9=A2=E5=9C=93=E5=BD=A2=E5=8A=A0=E5=AF=86=E6=BC=94=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- ExpandApplication/ecc.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ExpandApplication/ecc.py diff --git a/ExpandApplication/ecc.py b/ExpandApplication/ecc.py new file mode 100644 index 0000000..fefcded --- /dev/null +++ b/ExpandApplication/ecc.py @@ -0,0 +1,35 @@ +from fastecdsa import curve, ecdsa, keys +from fastecdsa.encoding.der import DEREncoder + +message = "a message to sign via ECDSA" # 一些訊息 + +""" +使用預設曲線跟雜湊函數(P256 and SHA2) +""" +private_key = keys.gen_private_key(curve.P256) +public_key = keys.get_public_key(private_key, curve.P256) +print(private_key) +print(public_key) +""" +產生數位簽章,為兩個整數r跟s。 +""" +r, s = ecdsa.sign(message, private_key) +print(r) +print(s) +""" +編碼數位簽章,使用DEREncoder為r跟s做編碼以利傳輸 +""" +encoded = DEREncoder.encode_signature(r, s) +print(encoded) +decoded_r, decoded_s = DEREncoder.decode_signature(encoded) +if r == decoded_r and s == decoded_s: + decoded_r, decoded_s + valid = ecdsa.verify((decoded_r, decoded_s), message, public_key) + print(valid) +""" +valid應該返回True,驗證前面產生的私密金鑰(private_key)、公開金鑰(public_key)、 +數位簽章(r, s)、加密訊息是有效的。 +""" +public_key = b'X: 0x2b71ffb22f1e918e401f19c2ab4afa0fa9a4221c28c61cfde09dbbd28fb332b\nY: 0xdeb8a709e838a712156e3a5a8f189f4d1d1798de1f20417c5be65fc61606a384\n(On curve )' +valid = ecdsa.verify((decoded_r, decoded_s), message, public_key) +print(valid) From b95e8f84e72e8153c4dfe18ff907fc38a75992dc Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Wed, 28 Aug 2019 00:57:27 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=AA=AA=E6=98=8E=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2e908d5..b46f108 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,32 @@ -# Talk -利用Python撰寫的點對點或中央談話程式。 +Talk +=== +利用Python撰寫的點對點或中央談話程式,可以選擇使用不同的加密演算法保護傳輸的資料。 Peer-to-peer or central talk program written in Python. +使用Python3.6,並且使用以下套件: +- fastecdsa +- pycryptodome + +支援的對稱性加密演算法: +- rc4-md5 +- aes-128-gcm +- aes-192-gcm +- aes-256-gcm +- aes-128-cfb +- aes-192-cfb +- aes-256-cfb +- aes-128-ctr +- aes-192-ctr +- aes-256-ctr +- camellia-128-cfb +- camellia-192-cfb +- camellia-256-cfb +- bf-cfb +- chacha20-ietf-poly1305 +- xchacha20-ietf-poly1305 +- salsa20 +- chacha20 +- chacha20-ietf + +支援的非對稱性加密演算法: +- RSA +- ECC From daa9a8cec91315583a488d6f4ccb18c9a3b30ead Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Sun, 1 Sep 2019 20:12:08 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E9=87=9D=E5=B0=8D?= =?UTF-8?q?=E6=9F=90=E4=B8=80=E5=80=8B=E5=A5=97=E4=BB=B6=E5=81=9A=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- ExpandApplication/{ecc.py => PublicKeyCryptography.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ExpandApplication/{ecc.py => PublicKeyCryptography.py} (100%) diff --git a/ExpandApplication/ecc.py b/ExpandApplication/PublicKeyCryptography.py similarity index 100% rename from ExpandApplication/ecc.py rename to ExpandApplication/PublicKeyCryptography.py From 3e76e20cf83eca55cf5e4f2712bb5c64b0085e54 Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Mon, 2 Sep 2019 00:01:08 +0800 Subject: [PATCH 04/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E6=9A=AB=E6=99=82?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=B0=8D=E7=A8=B1=E5=8A=A0=E5=AF=86=E6=BC=94?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E6=A8=A1=E7=B5=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- ExpandApplication/SymmetricKeyAlgorithm.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ExpandApplication/SymmetricKeyAlgorithm.py diff --git a/ExpandApplication/SymmetricKeyAlgorithm.py b/ExpandApplication/SymmetricKeyAlgorithm.py new file mode 100644 index 0000000..e69de29 From 1b7d9e014350b2154a480a68859d06e6d455ef1c Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Tue, 3 Sep 2019 15:02:32 +0800 Subject: [PATCH 05/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E6=9A=AB=E6=99=82?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=A7=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- ExpandApplication/PublicKeyCryptography.py | 36 +++++++++------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/ExpandApplication/PublicKeyCryptography.py b/ExpandApplication/PublicKeyCryptography.py index fefcded..e3c4783 100644 --- a/ExpandApplication/PublicKeyCryptography.py +++ b/ExpandApplication/PublicKeyCryptography.py @@ -3,33 +3,25 @@ message = "a message to sign via ECDSA" # 一些訊息 -""" -使用預設曲線跟雜湊函數(P256 and SHA2) -""" +print("使用預設曲線P256跟雜湊函數SHA2") private_key = keys.gen_private_key(curve.P256) public_key = keys.get_public_key(private_key, curve.P256) -print(private_key) -print(public_key) -""" -產生數位簽章,為兩個整數r跟s。 -""" +print("私密金鑰: " + str(private_key)) +print("公開金鑰: " + str(public_key)) +print(type(public_key)) + +print("產生數位簽章,為兩個整數r跟s") r, s = ecdsa.sign(message, private_key) print(r) print(s) -""" -編碼數位簽章,使用DEREncoder為r跟s做編碼以利傳輸 -""" + +print("編碼數位簽章,使用DEREncoder為r跟s做編碼以利傳輸") encoded = DEREncoder.encode_signature(r, s) -print(encoded) +b = "數位簽章: " +for i in encoded: + b += str(hex(i)) + " " +print(b) decoded_r, decoded_s = DEREncoder.decode_signature(encoded) -if r == decoded_r and s == decoded_s: - decoded_r, decoded_s - valid = ecdsa.verify((decoded_r, decoded_s), message, public_key) - print(valid) -""" -valid應該返回True,驗證前面產生的私密金鑰(private_key)、公開金鑰(public_key)、 -數位簽章(r, s)、加密訊息是有效的。 -""" -public_key = b'X: 0x2b71ffb22f1e918e401f19c2ab4afa0fa9a4221c28c61cfde09dbbd28fb332b\nY: 0xdeb8a709e838a712156e3a5a8f189f4d1d1798de1f20417c5be65fc61606a384\n(On curve )' valid = ecdsa.verify((decoded_r, decoded_s), message, public_key) -print(valid) +if r == decoded_r and s == decoded_s and valid: + print("成功") From 3a75508d59e956423d2aae2406d8e7beb984310f Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Tue, 3 Sep 2019 15:03:49 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E4=BE=9D=E7=85=A7?= =?UTF-8?q?=E5=8C=AF=E5=85=A5=E7=9A=84=E5=A5=97=E4=BB=B6=E7=B7=A8=E8=BC=AF?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E4=BD=BF=E7=94=A8=E7=9A=84=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E6=BC=94=E7=AE=97=E6=B3=95=E8=88=87=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- README.md | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b46f108..c70686e 100644 --- a/README.md +++ b/README.md @@ -2,30 +2,34 @@ Talk === 利用Python撰寫的點對點或中央談話程式,可以選擇使用不同的加密演算法保護傳輸的資料。 Peer-to-peer or central talk program written in Python. + 使用Python3.6,並且使用以下套件: - fastecdsa - pycryptodome -支援的對稱性加密演算法: -- rc4-md5 -- aes-128-gcm -- aes-192-gcm -- aes-256-gcm -- aes-128-cfb -- aes-192-cfb -- aes-256-cfb -- aes-128-ctr -- aes-192-ctr -- aes-256-ctr -- camellia-128-cfb -- camellia-192-cfb -- camellia-256-cfb -- bf-cfb -- chacha20-ietf-poly1305 -- xchacha20-ietf-poly1305 -- salsa20 -- chacha20 -- chacha20-ietf +支援的加密演算法 +- 「對稱性加密演算法」(Symmetric ciphers) + - 「串流加密法」(Stream ciphers) + - ChaCha20 + - XChaCha20 + - Salsa20 + - authentication (MAC) + - Poly1305 + - 「區塊加密法」(Block ciphers) + - AES + - Classic modes of operation + - ECB mode + - CBC mode + - CTR mode + - CFB mode + - OFB mode + - OpenPGP mode + - Modern modes of operation + - CCM mode + - EAX mode + - GCM mode + - SIV mode + - OCB mode 支援的非對稱性加密演算法: - RSA From 8db016031e08c850651d81e284cfbf3b0d073ebe Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Tue, 3 Sep 2019 15:04:59 +0800 Subject: [PATCH 07/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=AA=94=E6=A1=88=E5=90=8D=E7=A8=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- .../{SymmetricKeyAlgorithm.py => SymmetricCiphers.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ExpandApplication/{SymmetricKeyAlgorithm.py => SymmetricCiphers.py} (100%) diff --git a/ExpandApplication/SymmetricKeyAlgorithm.py b/ExpandApplication/SymmetricCiphers.py similarity index 100% rename from ExpandApplication/SymmetricKeyAlgorithm.py rename to ExpandApplication/SymmetricCiphers.py From e205b669480c751ac5766933fda5bdd7cb9a869d Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Tue, 3 Sep 2019 15:08:22 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=AA=AA=E6=98=8E=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c70686e..801bffc 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,18 @@ Peer-to-peer or central talk program written in Python. - fastecdsa - pycryptodome -支援的加密演算法 +支援的加密演算法: - 「對稱性加密演算法」(Symmetric ciphers) - 「串流加密法」(Stream ciphers) - - ChaCha20 - - XChaCha20 - - Salsa20 + - ciphers + - ChaCha20 + - XChaCha20 + - Salsa20 - authentication (MAC) - Poly1305 - 「區塊加密法」(Block ciphers) - - AES + - Ciphers + - AES - Classic modes of operation - ECB mode - CBC mode From bf56588a065ad1d4f6d508c78cdb9fce8183e3de Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Tue, 3 Sep 2019 15:14:25 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E5=86=8D=E6=AC=A1?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8A=A0=E5=AF=86=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 801bffc..fa27271 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ Peer-to-peer or central talk program written in Python. - SIV mode - OCB mode -支援的非對稱性加密演算法: -- RSA -- ECC +- 「非對稱性加密演算法」(Asymmetric ciphers): + - RSA + - ECC + +- Hybrid ciphers:結合兩者 From 75f7f5127fe252f2c786f6e80a4465ad1327fa94 Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Tue, 3 Sep 2019 15:16:08 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E9=87=9D=E5=B0=8D?= =?UTF-8?q?=E6=A8=A1=E7=B5=84=E5=90=8D=E7=A8=B1=E9=87=8D=E6=96=B0=E5=91=BD?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- .../{PublicKeyCryptography.py => AsymmetricCiphers.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ExpandApplication/{PublicKeyCryptography.py => AsymmetricCiphers.py} (100%) diff --git a/ExpandApplication/PublicKeyCryptography.py b/ExpandApplication/AsymmetricCiphers.py similarity index 100% rename from ExpandApplication/PublicKeyCryptography.py rename to ExpandApplication/AsymmetricCiphers.py From 13456dc0acce4c5f767293df5d230f06866f83ac Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Tue, 3 Sep 2019 21:09:33 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E8=87=AA=E5=8B=95=E5=88=A4=E6=96=B7=E8=BC=B8=E5=85=A5=E8=B3=87?= =?UTF-8?q?=E6=96=99=E4=BB=A5=E7=94=A8=E6=96=BC=E5=8A=A0=E8=A7=A3=E5=AF=86?= =?UTF-8?q?=E7=9A=84=E9=A1=9E=E5=88=A5=E8=88=87=E5=87=BD=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- ExpandApplication/SymmetricCiphers.py | 83 +++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/ExpandApplication/SymmetricCiphers.py b/ExpandApplication/SymmetricCiphers.py index e69de29..e19375b 100644 --- a/ExpandApplication/SymmetricCiphers.py +++ b/ExpandApplication/SymmetricCiphers.py @@ -0,0 +1,83 @@ +import json +from base64 import b64encode +from base64 import b64decode +from Crypto.Cipher import AES +from Crypto.Util.Padding import pad +from Crypto.Util.Padding import unpad +from Crypto.Random import get_random_bytes + + +class stream_ciphers(): + def __init__(self, key=None, encrypt_method="aes-256-cfb", iv=None, + segment_size=None, nonce=None, initial_value=None, + counter=None): + self.__key = key + self.__encrypt_method = encrypt_method + self.__iv = iv + self.__segment_size = segment_size + self.__nonce = nonce + self.__initial_value = initial_value + self.__counter = counter + + def encrypt(self, message=b'test'): + cipher = self.mode + ct_bytes = cipher.encrypt(pad(message, AES.block_size)) + iv = b64encode(cipher.iv).decode('utf-8') + ct = b64encode(ct_bytes).decode('utf-8') + result = json.dumps({'iv': iv, 'ciphertext': ct}) + yield result.encode('utf8') + + def decrypt(self): + # We assume that the key was securely shared beforehand + try: + b64 = json.loads(json_input) + iv = b64decode(b64['iv']) + ct = b64decode(b64['ciphertext']) + cipher = AES.new(key, AES.MODE_CBC, iv) + pt = unpad(cipher.decrypt(ct), AES.block_size) + print("The message was: ", pt) + except: + print("Incorrect decryption") + + @property + def mode(self): + if len(self.__key) % 8 == 0: + raise ValueError('字元數必須是符合8的倍數') + + if self.__key is None: + self.__key = get_random_bytes() + else: + self.__key = self.__key + + items = self.__encrypt_method.split("-") + if items[1] == len(self.__key) * 16: + raise ValueError('不符合金鑰設定的位元長度') + + if items[0] == "aes": + if items[2] == "ecb": + cipher = AES.new(self.__key, AES.MODE_ECB) + + elif items[2] == "cbc": + cipher = AES.new(self.__key, AES.MODE_CBC) + + elif items[2] == "ctr": + cipher = AES.new(self.__key, AES.MODE_CTR) + + elif items[2] == "cfb": + cipher = AES.new(self.__key, AES.MODE_CFB) + + elif items[2] == "ofb": + cipher = AES.new(self.__key, AES.MODE_OFB) + + elif items[2] == "openpgp": + cipher = AES.new(self.__key, AES.MODE_OPENPGP) + + else: + raise ValueError('沒有此加密演算法') + + else: + raise ValueError('沒有此加密演算法') + + @mode.setter + def mode(self, radius): + self.__radius = radius From 32d3f5a5e57fd41185966eaef0ac312ebcfe66b7 Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Wed, 4 Sep 2019 00:43:34 +0800 Subject: [PATCH 12/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E9=81=8B=E4=BD=9C=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=BE=97=E5=88=B0=E5=8A=A0=E5=AF=86=E5=AD=97=E4=B8=B2=EF=BC=8C?= =?UTF-8?q?=E5=BE=8C=E7=BA=8C=E9=81=8B=E4=BD=9C=E6=9C=83=E6=9C=89=E5=95=8F?= =?UTF-8?q?=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- ExpandApplication/SymmetricCiphers.py | 113 ++++++++++++++++++++------ 1 file changed, 89 insertions(+), 24 deletions(-) diff --git a/ExpandApplication/SymmetricCiphers.py b/ExpandApplication/SymmetricCiphers.py index e19375b..0e333c1 100644 --- a/ExpandApplication/SymmetricCiphers.py +++ b/ExpandApplication/SymmetricCiphers.py @@ -9,7 +9,7 @@ class stream_ciphers(): def __init__(self, key=None, encrypt_method="aes-256-cfb", iv=None, - segment_size=None, nonce=None, initial_value=None, + segment_size=8, nonce=None, initial_value=None, counter=None): self.__key = key self.__encrypt_method = encrypt_method @@ -19,37 +19,76 @@ def __init__(self, key=None, encrypt_method="aes-256-cfb", iv=None, self.__initial_value = initial_value self.__counter = counter - def encrypt(self, message=b'test'): - cipher = self.mode - ct_bytes = cipher.encrypt(pad(message, AES.block_size)) - iv = b64encode(cipher.iv).decode('utf-8') + def encrypt(self, message="test"): + cipher, iv, segment_size, nonce, initial_value, counter = next( + self.encrypt_method) + message = message.encode('utf8') + if self.items[2] == "cfb": + ct_bytes = cipher.encrypt(pad(message, AES.block_size)) + else: + ct_bytes = cipher.encrypt(message) + ct = b64encode(ct_bytes).decode('utf-8') - result = json.dumps({'iv': iv, 'ciphertext': ct}) + + result = { + 'iv': self.__iv, + 'segment_size': self.__segment_size, + 'nonce': self.__nonce, + 'initial_value': self.__initial_value, + 'counter': self.__counter, + 'ciphertext': ct, + } + + result = json.dumps(result) yield result.encode('utf8') - def decrypt(self): + def decrypt(self, cipher_message): # We assume that the key was securely shared beforehand try: - b64 = json.loads(json_input) + cipher_message = cipher_message.encode('utf8') + b64 = json.loads(cipher_message) + iv = b64decode(b64['iv']) + segment_size = b64decode(b64['segment_size']) + nonce = b64decode(b64['nonce']) + initial_value = b64decode(b64['initial_value']) + counter = b64decode(b64['counter']) ct = b64decode(b64['ciphertext']) - cipher = AES.new(key, AES.MODE_CBC, iv) - pt = unpad(cipher.decrypt(ct), AES.block_size) - print("The message was: ", pt) + + self.reset_encrypt_method( + key=self.__key, + encrypt_method=self.__encrypt_method, + iv=iv, + segment_size=segment_size, + nonce=nonce, + initial_value=initial_value, + counter=counter + ) + + cipher, iv, segment_size, nonce, initial_value, counter = next( + self.encrypt_method) + + if self.items[2] == "": + pt = unpad(cipher.decrypt(ct), AES.block_size) + else: + pt = cipher.decrypt(ct) + + yield pt + except: print("Incorrect decryption") @property - def mode(self): - if len(self.__key) % 8 == 0: - raise ValueError('字元數必須是符合8的倍數') - + def encrypt_method(self): if self.__key is None: - self.__key = get_random_bytes() + self.__key = get_random_bytes(16) else: self.__key = self.__key - items = self.__encrypt_method.split("-") + if len(self.__key) % 8 != 0: + raise ValueError('金鑰長度必須是符合8的倍數') + + self.items = items = self.__encrypt_method.split("-") if items[1] == len(self.__key) * 16: raise ValueError('不符合金鑰設定的位元長度') @@ -58,16 +97,23 @@ def mode(self): cipher = AES.new(self.__key, AES.MODE_ECB) elif items[2] == "cbc": - cipher = AES.new(self.__key, AES.MODE_CBC) + cipher = AES.new(self.__key, AES.MODE_CBC, iv=self.__iv) + self.__iv = b64encode(cipher.iv).decode('utf-8') elif items[2] == "ctr": - cipher = AES.new(self.__key, AES.MODE_CTR) + cipher = AES.new(self.__key, AES.MODE_CTR, nonce=self.__nonce, + initial_value=self.__initial_value, + counter=self.__counter) + self.__nonce = b64encode(cipher.nonce).decode('utf-8') elif items[2] == "cfb": - cipher = AES.new(self.__key, AES.MODE_CFB) + cipher = AES.new(self.__key, AES.MODE_CFB, iv=self.__iv, + segment_size=self.__segment_size) + self.__iv = b64encode(cipher.iv).decode('utf-8') elif items[2] == "ofb": - cipher = AES.new(self.__key, AES.MODE_OFB) + cipher = AES.new(self.__key, AES.MODE_OFB, iv=self.__iv) + self.__iv = b64encode(cipher.iv).decode('utf-8') elif items[2] == "openpgp": cipher = AES.new(self.__key, AES.MODE_OPENPGP) @@ -78,6 +124,25 @@ def mode(self): else: raise ValueError('沒有此加密演算法') - @mode.setter - def mode(self, radius): - self.__radius = radius + iv = self.__iv + segment_size = self.__segment_size + nonce = self.__nonce + initial_value = self.__initial_value + counter = self.__counter + yield cipher, iv, segment_size, nonce, initial_value, counter + + def reset_encrypt_method(self, key=None, encrypt_method="aes-256-cfb", + iv=None, segment_size=None, nonce=None, + initial_value=None, counter=None): + self.__key = key + self.__encrypt_method = encrypt_method + self.__iv = iv + self.__segment_size = segment_size + self.__nonce = nonce + self.__initial_value = initial_value + self.__counter = counter + + +sc = stream_ciphers() +print(next(sc.encrypt())) +print(next(sc.encrypt())) From 05ebcf7ff2e2731c09ba64f744350f6ce0220854 Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Wed, 4 Sep 2019 08:36:38 +0800 Subject: [PATCH 13/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E6=8C=81=E7=BA=8C?= =?UTF-8?q?=E4=BE=9D=E7=85=A7=E4=B8=8D=E5=90=8C=E7=9A=84=E9=A2=A8=E6=A0=BC?= =?UTF-8?q?=E9=A2=A8=E6=A0=BC=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- ExpandApplication/SymmetricCiphers.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ExpandApplication/SymmetricCiphers.py b/ExpandApplication/SymmetricCiphers.py index 0e333c1..0925b65 100644 --- a/ExpandApplication/SymmetricCiphers.py +++ b/ExpandApplication/SymmetricCiphers.py @@ -18,17 +18,19 @@ def __init__(self, key=None, encrypt_method="aes-256-cfb", iv=None, self.__nonce = nonce self.__initial_value = initial_value self.__counter = counter + self.__items = None def encrypt(self, message="test"): + """aaa.""" cipher, iv, segment_size, nonce, initial_value, counter = next( self.encrypt_method) message = message.encode('utf8') - if self.items[2] == "cfb": + if self.__items[2] == "cfb": ct_bytes = cipher.encrypt(pad(message, AES.block_size)) else: ct_bytes = cipher.encrypt(message) - ct = b64encode(ct_bytes).decode('utf-8') + ct_b64 = b64encode(ct_bytes).decode('utf-8') result = { 'iv': self.__iv, @@ -36,7 +38,7 @@ def encrypt(self, message="test"): 'nonce': self.__nonce, 'initial_value': self.__initial_value, 'counter': self.__counter, - 'ciphertext': ct, + 'ciphertext': ct_b64, } result = json.dumps(result) @@ -63,12 +65,12 @@ def decrypt(self, cipher_message): nonce=nonce, initial_value=initial_value, counter=counter - ) + ) cipher, iv, segment_size, nonce, initial_value, counter = next( self.encrypt_method) - if self.items[2] == "": + if self.__items[2] == "cfb": pt = unpad(cipher.decrypt(ct), AES.block_size) else: pt = cipher.decrypt(ct) @@ -88,7 +90,7 @@ def encrypt_method(self): if len(self.__key) % 8 != 0: raise ValueError('金鑰長度必須是符合8的倍數') - self.items = items = self.__encrypt_method.split("-") + self.__items = items = self.__encrypt_method.split("-") if items[1] == len(self.__key) * 16: raise ValueError('不符合金鑰設定的位元長度') From 2eda0bc1729212ce6b37dbc486607a4cd2523951 Mon Sep 17 00:00:00 2001 From: timmy61109 <38396747+timmy61109@users.noreply.github.com> Date: Wed, 4 Sep 2019 08:38:50 +0800 Subject: [PATCH 14/14] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A1=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E6=AD=A4=E6=8F=90=E4=BA=A4=E5=B0=87=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=B8=80=E5=80=8B=E5=A5=97=E4=BB=B6=E5=90=8D=E7=A8=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改項目: --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fa27271..ccb3593 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Peer-to-peer or central talk program written in Python. 使用Python3.6,並且使用以下套件: - fastecdsa - pycryptodome +- socket 支援的加密演算法: - 「對稱性加密演算法」(Symmetric ciphers)