Private methods and fields in python [encryption]

0. Summary for those who don't want to waste time

This article incorporates nonces and encryption to force private members to be implemented in python, but it just doesn't make sense because it's less readable and can be easily broken.

1. Idea

All you have to do is design an encryption nonce. Regarding the encryption mechanism, teitei_tk's article was plagiarized. In particular, ʻaes_cipher.pywas saved in the directory as it is. However,pip install pycryptoseems to cause a build error in the windows environment, so An alternative topip install pycryptodome`.

2. Implementation

Private method


import numpy as np
from aes_cipher import AESCipher
import string, random;
#All imports are for private methods

class Test:
    def __init__(self):
        self.cipher = AESCipher(''.join([random.choice(string.ascii_letters + string.digits) for i in range(50)])) #Key creation, immediate registration
        self.nonce # nonce

    def caller(self):
        print('From caller_private_Call callee')
        self.nonce = np.random.rand() #nonce update
        self._private_callee(self.cipher.encrypt(self.nonce)) #Encrypt new nonce and send it to private method

    def _private_callee(self, ciphered_nonce):
        nonce = cipher.decrypt(ciphered_nonce) #Decrypt the encrypted nonce sent
        if nonce==self.nonce:
            print('Since the nonces matched, the call from the caller was as expected.')
            #Write processing here
        else:
            print('Illegal call')

Of course, if you do ʻobj._private_callee (obj.cipher.encrypt (obj.nonce))` from the outside, an illegal call will succeed, so it is ** a good place out of the question in terms of security **. But well, it's unlikely that you'll inadvertently call a private method if you do this much. In the first place, even java's private method can be called from the outside by using reflection. Isn't this enough just to prevent strangers from calling private methods?

You can also use the same approach to create private fields and define getters and setters.

Private field


import numpy as np
from aes_cipher import AESCipher
import string, random;
#All imports are for private fields

class Test:
    def __init__(self):
        self.cipher = AESCipher(''.join([random.choice(string.ascii_letters + string.digits) for i in range(50)])) #Key creation, immediate registration
        self._private_field = self.cipher.encrypt('"You can be seen at worst" but "I don't want to be seen inadvertently"')

    def setter(self, value):
        self._private_field = self.cipher.encrypt(value)

    def getter(self):
        return self.cipher.decrypt(self._private_field)
    

2. Conclusion

After all, it seems better not to encrypt or introduce nonce, but to just negotiate "Those with _ at the beginning are private! ". It doesn't make much sense to forcibly implement something like that, but the readability of the code is messed up.

Recommended Posts

Private methods and fields in python [encryption]
Private method in python
Encryption and decryption with Python
String object methods in Python
Stack and Queue in Python
Unittest and CI in Python
Dynamically call methods in Python
Nosalt AES encryption in Python
MIDI packages in Python midi and pretty_midi
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Dynamically define functions (methods) in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Python classes and instances, instance methods
Create and read messagepacks in Python
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Dealing with "years and months" in Python
Read and write JSON files in Python
Easily graph data in shell and Python
Find and check inverse matrix in Python
Linear Independence and Basis: Linear Algebra in Python <6>
Call sudo in Python and autofill password
Summary of built-in methods in Python list
Differences in multithreading between Python and Jython
Module import and exception handling in python
How to use is and == in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Organize python modules and packages in a mess
Geocoding in python
SendKeys in Python
What is "functional programming" and "object-oriented" in Python?