# RSA

[RSA intro video](https://www.youtube.com/watch?v=fq6SXByItUI\&index=11\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

[RSA video](https://www.youtube.com/watch?v=QSlWzKNbKrU\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy\&index=12)

[RSA sign](https://www.youtube.com/watch?v=jbBe4AS5pk0\&index=18\&list=PL6N5qY2nvvJE8X75VkXglSrVhLv1tVcfy)

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

### RSA

[spec](https://es.wikipedia.org/wiki/RSA)

Description: Public-key cryptosystem

Problem: Integer Factoriation

Uses: Encrypt, digital signatures

* It only supports "type":"string"

## How to ???

```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 rsa(data_js, bits):
    req=json.loads(data_js)
    req["length"]=bits
    print "Send gen parameters : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ=json.loads(data_js_n)
    print "Recived  rsa gen: \n"+(json.dumps(answ)) +"\n\n\n"
    json_s=json_v='{ "version": 1 , "algorithm":"RSA", "type":"string","plaintext": "Hello world!", "hex":0,"privkey": "" ,"operation":"sign"}'
    req=json.loads(json_s)
    req["privkey"]=answ["privkey"]
    print "Send sign : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_1=json.loads(data_js_n)
    print "Recived  sign done: \n"+(json.dumps(answ_1)) +"\n\n\n"
    json_v='{ "version": 1 , "algorithm":"RSA", "type":"string","plaintext": "Hello world!", "hex":0,"pubkey": "" ,"sign":"","operation":"verify"}'
    req=json.loads(json_v)
    req["pubkey"]=answ["pubkey"]
    req["sign"]=answ_1["sign"]
    print "Send verify : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_2=json.loads(data_js_n)
    print "Recived  verify done: \n"+(json.dumps(answ_2)) +"\n\n\n"
    json_enc='{ "version": 1 , "algorithm":"RSA", "type":"string","pubkey": "" ,"operation":"enc", "plaintext":"Hello world!","hex":0 }'
    req=json.loads(json_enc)
    req["pubkey"]=answ["pubkey"]
    print "Send enc : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_3=json.loads(data_js_n)
    print "Recived  enc done: \n"+(json.dumps(answ_3)) +"\n\n\n"
    json_dec='{ "version": 1 , "algorithm":"RSA", "type":"string","privkey": "" ,"operation":"dec", "plaintext":"" }'
    req=json.loads(json_dec)
    req["privkey"]=answ["privkey"]
    req["plaintext"]=answ_3["result"]
    print "Send dec : \n " + json.dumps(req) +"\n"
    data_js_n=sending(json.dumps(req))
    answ_3=json.loads(data_js_n)
    print "Recived  dec done: \n"+(json.dumps(answ_3)) +"\n\n\n"



rsa_gen='{ "version": 1 , "algorithm":"RSA", "operation":"gen" , "length": 0 }'
rsa(rsa_gen,1024)
```

In this example we generate a RSA key (1024 bits), sign and validate *"Hello world!"* string, we enc and dec *"Hello world!"* string.

The default hash function to sign is sha3*256, you can change by adding* "hash*sign"* and can be one of *{"sha3\_512","sha3\_384","sha3\_256","sha3\_224","sha\_512","sha\_384","sha\_256","sha\_224","sha\_1","whirlpool"}*

Json to gen

```javascript
{ "version": 1 , "algorithm":"RSA", "operation":"gen" , "length": INT }
```

Json to sign

```javascript
{ "version": 1 , "algorithm":"RSA", "type":"string","plaintext": "your string", "hex":BOOL,"privkey": "your hex privkey" ,"operation":"sign"}
```

Json to verify

```javascript
{ "version": 1 , "algorithm":"RSA", "type":"string","plaintext": "your string", "hex":BOOL,"pubkey": "your hex pubkey" ,"sign":"your hex signature","operation":"verify"}
```

Json to enc

```javascript
{ "version": 1 , "algorithm":"RSA", "type":"string","pubkey": "your hex pubkey" ,"operation":"enc", "plaintext":"your string","hex":BOOL }
```

Json to dec

```javascript
{ "version": 1 , "algorithm":"RSA", "type":"string","privkey": "your hex privkey" ,"operation":"dec", "plaintext":"your enc hex string" }
```


---

# 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/rsa.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.
