> For the complete documentation index, see [llms.txt](https://ivalexev.gitbook.io/rednote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ivalexev.gitbook.io/rednote/pentesting-process/crypto-attacks/utility.md).

# Utility

* [Crypton](https://github.com/ashutosh1206/Crypton/)
* [Crypto-Attacks](https://github.com/jvdsn/crypto-attacks/)
* [CryptoBook](https://cryptohack.gitbook.io/cryptobook)

## Conversions

`1 byte` = `8 bit` = `2 hex`

### int —> char

{% code overflow="wrap" %}

```python
chr()
```

{% endcode %}

### char —> int

{% code overflow="wrap" %}

```python
ord()
```

{% endcode %}

### bytes —> HEX

{% code overflow="wrap" %}

```python
<BYTES>.hex()
```

{% endcode %}

### HEX —> bytes

{% code overflow="wrap" %}

```python
bytes.fromhex(<HEX>)
```

{% endcode %}

### HEX —> int

{% code overflow="wrap" %}

```python
int(<HEX>, 16)
```

{% endcode %}

### int —> HEX

{% code overflow="wrap" %}

```python
format(<INT>, 'x') # ff
```

{% endcode %}

{% code overflow="wrap" %}

```python
hex(<INT>) # 0xff
```

{% endcode %}

### bytes —> int

{% code overflow="wrap" %}

```python
int.from_bytes(<BYTES>, <endianes>)
```

{% endcode %}

From `Crypto.Util.number`

```
bytes_to_long(<BYTES>)
```

### int —> bytes

{% code overflow="wrap" %}

```python
<INT>.to_bytes(<nblock>, <endianes>)
```

{% endcode %}

From `Crypto.Util.number`

```
long_to_bytes(<INT>)
```

### bytes —> Base64

From `base64`

{% code overflow="wrap" %}

```python
b64encode(<BASE64>)
```

{% endcode %}

### Base64 —> bytes

From `base64`

{% code overflow="wrap" %}

```python
b64decode(<BYTES>)
```

{% endcode %}

### bytes XOR bytes

From `PwnTools`

{% code overflow="wrap" %}

```python
xor()
```

{% endcode %}

## Functions

### GCD

From `sympy`

{% code overflow="wrap" %}

```python
gcd(<A>, <B>)
```

{% endcode %}

### Modular Inverse

From `Crypto.Util.number`

{% code overflow="wrap" %}

```python
inverse(<NUM>, <MOD>)
```

{% endcode %}

From `sympy`

{% code overflow="wrap" %}

```python
mod_inverse(<NUM>, <MOD>)
```

{% endcode %}

### Extended Euclidean

`X*A + Y*B = GCD(A,B)`

From `sympy`

{% code overflow="wrap" %}

```python
X, Y, MCD = gcdex(<A>, <B>)
```

{% endcode %}

### Chinese Remainder Theorem

`x % M1 = U1`   `x = U1 % M1`\
`x % M2 = U2`   `x = U2 % M2`

From `sympy.ntheory.modular`

{% code overflow="wrap" %}

```python
crt([M1, M2, …], [U1, U2, …])
```

{% endcode %}

### Congruence Resolution

`N1 == N2 (mod M)`\
Solves the modules and returns if they are congruent modulo M

From `sympy.ntheory.modular`

{% code overflow="wrap" %}

```python
solve_congruence((N1, M), (N2, M))
```

{% endcode %}

### A\*? = B mod N

`x` such that `A*x = B mod N`

From `Crypto.Util.number`

{% code overflow="wrap" %}

```python
inverse(A, N)*B)%N)
```

{% endcode %}

From `sympy`

{% code overflow="wrap" %}

```python
mod_inverse(A, N)*B)%N)
```

{% endcode %}

### Discrete Logarithm

Given a prime `p`, `g` is defined to be a primitive root of `p` if for every `y` ∈ {1, .... , p-1} there exists an `i` such that: `y = g^i mod p`

From `sympy.ntheory`

{% code overflow="wrap" %}

```python
discrete_log(p, y, g)
```

{% endcode %}

### N-th root

N-th root of `v`. Returns 2 values, the approximate root, and if it is perfect (with or without approximation).

From `gmpy2`

{% code overflow="wrap" %}

```python
iroot(v, n)
```

{% endcode %}

### N-th root Extended

N-th root of `A` (`A^(1/N)`)

From `decimal.Decimal`

{% code overflow="wrap" %}

```python
int(pow(Decimal(<A>)), Decimal(Decimal('1')/Decimal(<N>))))
```

{% endcode %}

### Large Number

From `decimal.Decimal`, `decimal.getcontext`

{% code overflow="wrap" %}

```python
getcontext().prec = 1000
Decimal(<N>)
```

{% endcode %}

### Modular Square Root

Find the square root of `x mod p` (hard problem, like factorization).

From `sympy`

{% code overflow="wrap" %}

```python
sqrt_mod(x, p)
```

{% endcode %}

### SAT & SMT solver

From `z3`

{% code overflow="wrap" %}

```bash
pip install z3-solver
```

{% endcode %}

{% code overflow="wrap" %}

```python
# Define variables 
a, b = Ints('a b')
# Define equations
equation1 = a + b == 50 
equation2 = a * b == 625
# Create Solver 
s = Solver()
# Add equations 
s.add(equation1) 
s.add(equation2)
# If there are solutions extract them
if s.check() == sat: 
  model = s.model() 
  a_val = model[a].as_long() 
  b_val = model[b].as_long()
```

{% endcode %}

## [SAGE](https://doc.sagemath.org/html/en/tutorial/tour.html)

For my setting on macOS

{% code overflow="wrap" %}

```python
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/")
from sage.all import *
```

{% endcode %}

From `sage`

### Discrete Logarithm

Calculate `i` in: `RESULT = BASE^i mod M`

{% code overflow="wrap" %}

```python
discrete_log(Mod(<RESULT>, <M>), Mod(<BASE>, <M>), ord=<EULER_FUNCTION>)
```

{% endcode %}

### Factorization

Gets the factorization of the number `n`. Returns the list of factors.

{% code overflow="wrap" %}

```python
factor(n)
```

{% endcode %}

### Chinese Remainder Theorem

`x % M1 = U1`   `x = U1 % M1`\
`x % M2 = U2`   `x = U2 % M2`

{% code overflow="wrap" %}

```python
crt([U1, U2, …], [M1, M2, …])
```

{% endcode %}
