Cryptography, Socket Programming, and Python OOP Concepts
Posted on Apr 7, 2025 in Computers
Cryptography, Socket Programming, and Python OOP
Base64 Encoding | Base64 Decoding |
---|
import base64 #encoding
encoded_data = base64.b64encode(bytes('D','ascii'))
print("Encoded text with base 64 is")
print(encoded_data)
|
import base64 #decoding
decoded_data = base64.b64decode("RW5jb2RlIHRoaXMgdGV4dA==")
print("decoded text is ")
print(decoded_data)
|
Ciphers and Encoding- Caesar’s Cipher: Simple cipher that uses an offset of 3. You just add the offset to the characters.
- Transposition Cipher: “hello world” -> 4×4, padding=”add”
- Base64 Encoding and Decoding: Base64 is also called PEM; it is used to convert binary to text.
Why Encode?Some data structures might require the data to be in an 8 or 16-bit format, which would require encoding to process. How long would it take to crack a 4-character key with no repeating characters? 26x25x24x23 | Python Class Example
class Parent1:
def __init__(self, par1):
self.par1 = 5
class Parent2:
def __init__(self, par2):
self.par2 = par2
class Child(Parent1):
def __init__(self, ch1, p1, p2):
self.ch1 = ch1
super().__init__(p1)
Parent2.__init__(self, p2)
print([self.par1, self.ch1, self.par2])
@overload
def method(self):
print(self.ch1)
@overload
def method(self, p1):
print(" called second implementation with "+str(p1))
firstCh = Child(4, 5, 6)
firstCh.method()
|
Listener Socket Setup | Receiving Data |
---|
HOST = '127.0.0.1'
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
|
with conn:
print(f"connected by {addr}")
while True:
data=conn.recv(1024)
if not data:
break
conn.sendall(data)
|
Python Class Definitions
class Dog: #Old style, not recommended
pass
class Dog():
pass
class Dog(Object): #New style, best practice
pass
| Inheritance Practices- Good Practice:
class Child(Parent):
def __init__(self, arg1, arg2):
super().__init__(arg1)
- Bad practice:
class Child(Parent):
def __init__(self, arg1, arg2):
Parent.__init__(self, arg1)
|
Class Attributes vs. Instance Variables
class Egg:
color = "white" #Class attribute
def __init__(self, size): #size is a local variable
self.size = size #self.size is a instance variable
| |
CIA TriadConfidentiality: The information should only be revealed to the intended audience. Achieved through encryption. Integrity: The data has not been altered. The sender should be who they claim to be, and the sender should not be able to deny their actions (non-repudiation). This is done through hashing, message digest, and authentication. - A replay would be an attack against integrity.
- Data in processing, data in storage, data in transit.
Availability: The information/Network or service should be available for authorized users only. It should also be highly available. This is done through access control and redundancy of servers. - Data should be provided reliably and timely to legitimate users.
| Key Management- Longer keys are harder for the attacker to guess, therefore less prone to brute force attacks.
- Keys must be renewed frequently – secure against eavesdropping.
- Keys must be revoked if required – secure against compromised accounts.
- Must be communicated through a separate secure channel.
|
Encryption Considerations- Many encryption algorithms are prone to cryptanalysis – the art or process of deciphering coded messages without being told the key.
- Hybrid cryptography is a process of using multiple ciphers of different types together by including the benefits of each cipher.
- Another alternative is to use a different key for each block of data or to use a very long key.
| The Map Function- The
map() function returns a map object (which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple, etc.). - Syntax:
map(fun, iter)
|