Category:Encryption/rol-ror

From aldeid
Jump to navigation Jump to search
You are here
rol & ror

Description

  • rol and rot state for rotate left and rotate right
  • rol and rot are not reversible. They need to be used in tandem (one to encode and the other to decode).

Example

Asssembly

rol byte ptr [edx], 0xa3
ror byte ptr [edx], 0xbc
cmp byte ptr [edx], 0x38

Reversing

     ENCODING                 DECODING
┌─────────────────┐      ┌─────────────────┐
│ initial letter? │      │        p        │
└─────────────────┘      └─────────────────┘
        │ ord(letter)             ▲
        ▼                         │ chr(112 % 256)
┌─────────────────┐      ┌─────────────────┐
│ initial ordinal │      │       112       │
└─────────────────┘      └─────────────────┘
        │ rol 0xa3                ▲
        ▼                         │ ror 0xa3
┌─────────────────┐      ┌─────────────────┐
│ initial value?  │      │       131       │
└─────────────────┘      └─────────────────┘
        │ ror 0xbc                ▲
        ▼                         │ rol 0xbc
┌─────────────────┐      ┌─────────────────┐
│  result = 0x38  │ ───► │       0x38      │
└─────────────────┘      └─────────────────┘

The decryption can be performed in python as follows:

# Rotate left. Set max_bits to 8.
rol = lambda val, r_bits, max_bits=8: \
    (val << r_bits%max_bits) & (2**max_bits-1) | \
    ((val & (2**max_bits-1)) >> (max_bits-(r_bits%max_bits)))
 
# Rotate right. Set max_bits to 8.
ror = lambda val, r_bits, max_bits=8: \
    ((val & (2**max_bits-1)) >> r_bits%max_bits) | \
    (val << (max_bits-(r_bits%max_bits)) & (2**max_bits-1))

print chr(ror(rol(0x38,0xbc),0xa3)%256)

Pages in category "Encryption/rol-ror"

The following 3 pages are in this category, out of 3 total.