# Block-ciphers

## Block ciphers

[DES algorithm video - 1](https://www.youtube.com/watch?v=kPBJIhpcZgE\&index=5\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

[DES algorithm video - 2](https://www.youtube.com/watch?v=l-7YW06BFNs\&index=6\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

In order to understand *hex* and *type* parameters read [Hash](https://github.com/liesware/coherence/wiki/Hash)

In order to understand encryption and decryption read [Stream](https://github.com/liesware/coherence/wiki/Stream)

### AES

[spec](http://www.quadibloc.com/crypto/co040401.htm)

[AES algorithm video - 1](https://www.youtube.com/watch?v=x1v2tX4_dkQ\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=7)

[AES algorithm video - 2](https://www.youtube.com/watch?v=NHuibtoL_qk\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=8)

Description: Block cipher and AES winner.

Key:128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, Performance is impportant, FIPS 197, NIST.

### RC6

[spec](https://en.wikipedia.org/wiki/RC6)

Description: Block cipher and AES finalist.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### MARS

[spec](http://www.quadibloc.com/crypto/co040406.htm)

Description: Block cipher and AES finalist.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### Twofish

[spec](https://www.schneier.com/academic/twofish/)

Description: Block cipher and AES finalist.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### Serpent

[spec](http://www.cl.cam.ac.uk/~rja14/serpent.html)

Description: Block cipher and AES finalist.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### CAST-256

[spec](http://www.quadibloc.com/crypto/co040410.htm)

Description: Block cipher and AES finalist.

Key: 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### Camellia

[spec](https://info.isl.ntt.co.jp/crypt/eng/camellia/)

Description: Block cipher, The cipher has been approved for use by the ISO/IEC, the European Union's NESSIE project, the Japanese CRYPTREC.

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: General propuse, alternative to AES.

### Speck128

[spec](https://eprint.iacr.org/2013/404.pdf)

Key: 128, 192, 256 bits.

Iv: 128 bits.

Uses: IoT

### Simeck64

[spec](https://eprint.iacr.org/2015/612.pdf)

Key: 128 bits.

Iv: 64 bits.

Uses: IoT

## Modes of Operation

[Modes of Operation video](https://www.youtube.com/watch?v=4FBgb2uobWI\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=9)

### CTR

[spec](ps://en.wikipedia.org/wiki/Block_cipher_mode_of_operation)

Description: Block cipher mode

Uses: Performance is important.

### GCM

[spec](https://en.wikipedia.org/wiki/Galois/Counter_Mode)

Description: Block cipher mode

Uses: Authenticated encryption.

* It only support *"type":"string"*
* It doesn't support *"algorithm":"SIMECK64"*

## How to ???

```python
#!/usr/bin/env python

import requests
import json
import os,binascii

def sending(message):
    url = 'http://127.0.0.1:6613/'
    response = requests.post(url, data=message)
    print response.content
    return response.content

def stream(data_js):
    req=json.loads(data_js)
    print "Enc \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived enc: \n" + (json.dumps(answ)) +"\n\n\n"
    dec='{"algorithm":"AES","plaintext":"","iv":"b05691ef92cb9c9bb05691ef92cb9c9b",\
    "version":1,"key":"b05691ef92cb9c9bb05691ef92cb9c9b","operation":"dec","type":"string", "mode":"ctr"}'
    req=json.loads(dec)
    req["plaintext"]=answ["result"]
    print "Dec \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ2=json.loads(data_js_n)
    print "Recived dec: \n" + (json.dumps(answ2)) +"\n\n\n"


data_js='{ "version": 1 , "algorithm":"AES" , "type":"string", "plaintext": "Hello world!", "hex": 0,"operation":"enc",\
"key":"b05691ef92cb9c9bb05691ef92cb9c9b","iv":"b05691ef92cb9c9bb05691ef92cb9c9b", "mode":"ctr" }'
stream(data_js)
```

In this example we encrypt and decrypt Hello world! string using AES with the key and iv given in CTR mode.

On data*js\["algorithm"] can be one of* {AES, RC6, MARS, Twofish, SERPENT, CAST256, CAMELLIA, SPECK128, SIMECK64 }\_

```python
#!/usr/bin/env python

import socket
import json
import os,binascii

def sending(message):
    ip = '127.0.0.1'
    port = 6613
    BUFFER_SIZE = 65536
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, port))
    s.send(message)
    data = s.recv(BUFFER_SIZE)
    s.close()
    return data

def stream(data_js):
    req=json.loads(data_js)
    print "Enc \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived enc: \n" + (json.dumps(answ)) +"\n\n\n"
    dec='{"algorithm":"AES","plaintext":"","iv":"b05691ef92cb9c9bb05691ef92cb9c9b",\
    "version":1,"key":"b05691ef92cb9c9bb05691ef92cb9c9b","operation":"dec","type":"string", "mode":"gcm" ,"adata":"ABCD"}'
    req=json.loads(dec)
    req["plaintext"]=answ["result"]
    print "Dec \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ2=json.loads(data_js_n)
    print "Recived dec: \n" + (json.dumps(answ2)) +"\n\n\n"


data_js='{ "version": 1 , "algorithm":"AES" , "type":"string", "plaintext": "Hello world!", "hex": 0,"operation":"enc",\
"key":"b05691ef92cb9c9bb05691ef92cb9c9b","iv":"b05691ef92cb9c9bb05691ef92cb9c9b", "mode":"gcm" ,"adata":"ABCD"}'
stream(data_js)
```

In this example we encrypt and decrypt Hello world! string using AES with the key,iv,adata given in GCM mode.

On data*js\["algorithm"] can be one of* {AES, RC6, MARS, Twofish, SERPENT, CAST256, CAMELLIA, SPECK128}\_

Json to enc string (key and iv depends on algorithm you chose) in CTR mode

```javascript
{ "version": 1 , "algorithm":"block flavor" , "type":"string", "plaintext": "your string", "hex":BOOL,
"operation":"enc", "key":"Hex stringsize=32-64","iv":"Hex stringsize=16-32", "mode":"ctr"}
```

Json to dec string (key and iv depends on algorithm you chose) in CTR mode

```javascript
{ "version": 1 , "algorithm":"block flavor" , "type":"string", "plaintext": "your hex enc string ", 
"operation":"dec", "key":"Hex stringsize=32-64","iv":"Hex stringsize=16-32", "mode":"ctr"}
```

* To enc/dec a file you need to change *"type":"string" -> "type":"file" , "plaintext": "your hex enc string " -> "file":"your file"*
* To use enc/dec GCM mode you need to change *"mode":"ctr"->"mode":"gcm"* , add *"adata":"your string"* and *"hex"* parameters
* *"hex"* parameter apply for *"string"* and *"adata"* parameter


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coherence.3vidence.com/block-ciphers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
