❓ Help Algorithm for Hashing, AES and DES.

Status
Not open for further replies.
Oo, mayroon akong alam na mga code para sa Hashing, AES, at DES encryption at decryption.

Para sa Hashing, maaari kang gumamit ng mga libraries tulad ng hashlib sa Python. Narito ang isang halimbawa ng paggamit ng SHA-256 algorithm:

Python:
import hashlib

message = "Hello World!".encode('utf-8')
hash_object = hashlib.sha256(message)
hex_dig = hash_object.hexdigest()

print(hex_dig)

Para sa AES encryption at decryption, maaari kang gumamit ng PyCrypto library. Narito ang isang halimbawa ng pag-encrypt at pag-decrypt ng mensahe gamit ang AES:

Python:
from Crypto.Cipher import AES
import base64

def pad(s):
    return s + b"\0" * (AES.block_size - len(s) % AES.block_size)

def encrypt(message, key):
    message = pad(message)
    iv = b'Sixteen byte key'
    cipher = AES.new(key, AES.MODE_CBC, iv)
    encrypted = cipher.encrypt(message)
    return base64.b64encode(encrypted)

def decrypt(ciphertext, key):
    iv = b'Sixteen byte key'
    ciphertext = base64.b64decode(ciphertext)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    decrypted = cipher.decrypt(ciphertext)
    return decrypted.rstrip(b"\0")

message = b"Hello World!"
key = b"1234567890123456"
encrypted = encrypt(message, key)
print(encrypted)
decrypted = decrypt(encrypted, key)
print(decrypted)

Para sa DES encryption at decryption, maaari kang gumamit ng PyCrypto library din. Narito ang isang halimbawa ng pag-encrypt at pag-decrypt ng mensahe gamit ang DES:

Python:
from Crypto.Cipher import DES
import base64

def pad(s):
    return s + b"\0" * (DES.block_size - len(s) % DES.block_size)

def encrypt(message, key):
    message = pad(message)
    iv = b'00000000'
    cipher = DES.new(key, DES.MODE_CBC, iv)
    encrypted = cipher.encrypt(message)
    return base64.b64encode(encrypted)

def decrypt(ciphertext, key):
    iv = b'00000000'
    ciphertext = base64.b64decode(ciphertext)
    cipher = DES.new(key, DES.MODE_CBC, iv)
    decrypted = cipher.decrypt(ciphertext)
    return decrypted.rstrip(b"\0")

message = b"Hello World!"
key = b"12345678"
encrypted = encrypt(message, key)
print(encrypted)
decrypted = decrypt(encrypted, key)
print(decrypted)

Tandaan na ang mga halimbawang ito ay para lamang sa mga pagpapakita at hindi dapat gamitin sa mga sensitibong application na nangangailangan ng tunay na security.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 381
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,159
Guests online
883
Total visitors
2,042

Forum statistics

Threads
2,273,748
Posts
28,951,295
Members
1,234,942
Latest member
nuzlock
Back
Top