<commit_before># Cryptography


### Some Tips:

- Often data is just encoded in base64 or hex. Other times it's just compressed (gzip):
    - Text 32 characters long --> md5 hash.
    - 40 characters long --> SHA1 hash.
    - equal signs spread --> base64 encoded string.
    - text only letters, without numbers or special characters --> Caesar, Vigenere, or other type of cipher.
    - hints about keys and signing --> likely RSA.


---

## MD5

- The [MD5 hashing algorithm](http://www.fastsum.com/support/md5-checksum-utility-faq/md5-hash.php) always returns 128 bit values, so the chance that two randomly chosen objects have the same hash is 1:2**128.



* Command Line:

```
$ echo -n password | md5sum
5f4dcc3b5aa765d61d8327deb882cf99
```

* Or you can use Python's md5.md5().digest()

- md5 hashes: [here](http://hash-killer.com/), [here](http://www.md5this.com/), [here](http://www.hashkiller.co.uk/).

- [md5sum](http://linux.about.com/library/cmd/blcmdl1_md5sum.htm)
- [md5 creator](http://www.md5-creator.com/)


### Scripts Available

- Hash length extension attack
- Brute force hex digest chars



------

## SHA

- SHA-1 has output size of 160 bits, so chances of collisions are 2**160.

- [Hash maker](http://ratfactor.com/sha1).

### Scripts
- SHA-256 brute force


### Command Line

- Brute force:
```
import hashlib, itertools
hash = '6307c5441ebac07051e3b90d53c3106230dd9aa128601dcd5f63efcf824ce1ba'
ch = 'abcdef0123456789'
for a, b, c, d, e, f in itertools.product(ch, ch, ch, ch, ch, ch):
    if hashlib.sha256('ASIS_a9%s00f497f2eaa4372a7fc21f0d' % (a + b + c + d + e + f)).hexdigest() == hash:
        print 'ASIS_a9%s00f497f2eaa4372a7fc21f0d' % (a + b + c + d + e + f)
```




--------

## Rotation Ciphers


### Scripts
- Caesar
- Brute force rotation
- Pygenere
- Frequency analysis


### Online tools:

- Frequency analysis: [here](http://www.simonsingh.net/The_Black_Chamber/hintsandtips.html) and [here](http://www.xarg.org/tools/caesar-cipher)

- [Cesar Cipher decryption](http://www.xarg.org/tools/caesar-cipher/) and [here](http://tools.zenverse.net/caesar-cipher/).

- [Vigenere Cipher breaker](http://www.mygeocachingprofile.com/codebreaker.vigenerecipher.aspx) and [here](http://smurfoncrack.com/pygenere/index.php).

### In the terminal...

```sh
$ VAR=$(cat data.txt)
$ echo "$VAR"
$ alias rot13="tr A-Za-z N-ZA-Mn-za-m"
$ echo "$VAR" | rot13
```
### In Python...

In Python [we can use decoding](https://docs.python.org/2/library/codecs.html#codec-base-classes):

```python
"YRIRY GJB CNFFJBEQ EBGGRA".decode(encoding="ROT13")
```

### Readings:

- [How Vigenere works](http://sharkysoft.com/vigenere/).





---

## RSA

* Public-key cryptosystem which uses a public-private key pair to encrypt and decrypt information securely

* [RSA Python](https://pypi.python.org/pypi/rsa)


----

## Pailier Cryptosystem

### Scripts

- POC
- Primes

---


## Tools

### Scripts

- Finding GDC
- Finding if prime
- Generate prime
- Quick Select
- XORtool


### Other Resources

- [Cryptol](https://www.cryptool.org/en/cryptool1-en)

- [PyCrypto](https://www.dlitz.net/software/pycrypto/)

- hashpump

- Sage

- John the Ripper

![John](./images/john.png)


#### Carperter's Formula

- Very large number: ```bin``` and check if patterns. For example, using the [Carpenter's Formula]:
```<commit_msg>Add john the ripper cheatsheet<commit_after># Cryptography


### Some Tips:

- Often data is just encoded in base64 or hex. Other times it's just compressed (gzip):
    - Text 32 characters long --> md5 hash.
    - 40 characters long --> SHA1 hash.
    - equal signs spread --> base64 encoded string.
    - text only letters, without numbers or special characters --> Caesar, Vigenere, or other type of cipher.
    - hints about keys and signing --> likely RSA.


---

## MD5

- The [MD5 hashing algorithm](http://www.fastsum.com/support/md5-checksum-utility-faq/md5-hash.php) always returns 128 bit values, so the chance that two randomly chosen objects have the same hash is 1:2**128.



* Command Line:

```
$ echo -n password | md5sum
5f4dcc3b5aa765d61d8327deb882cf99
```

* Or you can use Python's md5.md5().digest()

- md5 hashes: [here](http://hash-killer.com/), [here](http://www.md5this.com/), [here](http://www.hashkiller.co.uk/).

- [md5sum](http://linux.about.com/library/cmd/blcmdl1_md5sum.htm)
- [md5 creator](http://www.md5-creator.com/)


### Scripts Available

- Hash length extension attack
- Brute force hex digest chars



------

## SHA

- SHA-1 has output size of 160 bits, so chances of collisions are 2**160.

- [Hash maker](http://ratfactor.com/sha1).

### Scripts
- SHA-256 brute force


### Command Line

- Brute force:
```
import hashlib, itertools
hash = '6307c5441ebac07051e3b90d53c3106230dd9aa128601dcd5f63efcf824ce1ba'
ch = 'abcdef0123456789'
for a, b, c, d, e, f in itertools.product(ch, ch, ch, ch, ch, ch):
    if hashlib.sha256('ASIS_a9%s00f497f2eaa4372a7fc21f0d' % (a + b + c + d + e + f)).hexdigest() == hash:
        print 'ASIS_a9%s00f497f2eaa4372a7fc21f0d' % (a + b + c + d + e + f)
```




--------

## Rotation Ciphers


### Scripts
- Caesar
- Brute force rotation
- Pygenere
- Frequency analysis


### Online tools:

- Frequency analysis: [here](http://www.simonsingh.net/The_Black_Chamber/hintsandtips.html) and [here](http://www.xarg.org/tools/caesar-cipher)

- [Cesar Cipher decryption](http://www.xarg.org/tools/caesar-cipher/) and [here](http://tools.zenverse.net/caesar-cipher/).

- [Vigenere Cipher breaker](http://www.mygeocachingprofile.com/codebreaker.vigenerecipher.aspx) and [here](http://smurfoncrack.com/pygenere/index.php).

### In the terminal...

```sh
$ VAR=$(cat data.txt)
$ echo "$VAR"
$ alias rot13="tr A-Za-z N-ZA-Mn-za-m"
$ echo "$VAR" | rot13
```
### In Python...

In Python [we can use decoding](https://docs.python.org/2/library/codecs.html#codec-base-classes):

```python
"YRIRY GJB CNFFJBEQ EBGGRA".decode(encoding="ROT13")
```

### Readings:

- [How Vigenere works](http://sharkysoft.com/vigenere/).





---

## RSA

* Public-key cryptosystem which uses a public-private key pair to encrypt and decrypt information securely

* [RSA Python](https://pypi.python.org/pypi/rsa)


----

## Pailier Cryptosystem

### Scripts

- POC
- Primes

---


## Tools

### Scripts

- Finding GDC
- Finding if prime
- Generate prime
- Quick Select
- XORtool


### Other Resources

- [Cryptol](https://www.cryptool.org/en/cryptool1-en)

- [PyCrypto](https://www.dlitz.net/software/pycrypto/)

- hashpump

- Sage

- <NAME>

![John](/Cryptography/images/john.png)


#### Carperter's Formula

- Very large number: ```bin``` and check if patterns. For example, using the [Carpenter's Formula]:
```
{'fraction_non_alphanumeric': 0.1635710005991612, 'fraction_numerical': 0.04553624925104853, 'mean_word_length': 3.5084402430790007, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 14, 'https://': 8, 'lorem ipsum': 0, 'www.': 20, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2934028', 'n_tokens_mistral': 2736, 'n_tokens_neox': 2452, 'n_words': 592}