El proyecto esta desarrollado en python con django
requisito installar pip install cryptography ==44.0.2
*******************HTML*********************************
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'styles/general.css' %}" />
<style>
.cntpreview{
background: #000;
padding: 10px;
width: 50%;
min-height: 300px;
color: #fff
}
</style>
</head>
<body>
<div id="divLoading" class="wrap_loading hide">
<div class="lds-ring">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="loading_text">Procesando ...</div>
</div>
<div class="titulo">Scrapping RUES (Colombia)</div>
<div class="fila">
<input type="text" id="txtIdentificador" value="900582706"/>
<button id="btnProcesar" class="boton">Procesar</button>
</div>
<div style="display: flex;justify-content: center;">
<pre id="txtPre" class="cntpreview">
</pre>
</div>
<div>
{% csrf_token %}
</div>
<script src="{% static 'scripts/Demo08.js' %}" ></script>
</body>
</html>
*********************************JAVASCRIPT********************
window.onload=function(){
let btnProcesar=document.getElementById("btnProcesar");
btnProcesar.onclick=function(){
let txtIdentificador=document.getElementById("txtIdentificador").value;
let fd=new FormData();
fd.append("data",txtIdentificador)
servidor({url:"procesar",data:fd,responsetype:"json"}).then((data)=>{
document.getElementById("txtPre").innerHTML=JSON.stringify( data,null,2);
});
}
}
function servidor({ metodo = "post", url = null, data = null, responsetype = "text" } = {}) {
return new Promise((resolve, reject) => {
let divLoading=document.getElementById("divLoading");
if(divLoading){
divLoading.classList.remove("hide");
}
let xhr = new XMLHttpRequest();
xhr.open(metodo, url);
var csrftoken=document.getElementsByName("csrfmiddlewaretoken")[0].value;
xhr.setRequestHeader("X-CSRFToken", csrftoken);
xhr.responseType = responsetype;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
divLoading.classList.add("hide");
resolve(xhr.response);
}
}
xhr.onerror = function (e) {
reject(e)
}
xhr.send(data);
});
}
*****************************VIEW*********************
from django.shortcuts import render
from django.http.response import HttpResponse,JsonResponse
import urllib.request as req
import json
import os
import base64
from hashlib import md5
#pip install cryptography
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
# Create your views here.
tokenGlobal=""
def index(request):
return render(request,"Demo08.html")
def procesar(request):
identificador=request.POST.get("data")
rptaJson=None
try:
clave=b'ac1244b5-8bee-47b2-a4a5-924a748d907f'
objFiltro='{"Razon":null,"Nit":'+identificador+',"Dpto":null,"Cod_Camara":null,"Matricula":null}'
datacifrada=encrypt(objFiltro.encode("utf-8"), clave)
print(datacifrada.decode("utf-8"))
payload={"dataBody":datacifrada.decode("utf-8")}
_request=req.Request("https://elasticprd.rues.org.co/api/ConsultasRUES/BusquedaAvanzadaRM",data=json.dumps(payload).encode("utf-8"),
method="POST",headers={"content-type":"application/json"})
with req.urlopen(_request) as r:
rpta=json.loads( r.read().decode("utf-8"))
print(rpta)
if rpta is not None and rpta["registros"] is not None and len(rpta["registros"])>0:
informacionEmpresa=rpta["registros"][0]
id_rm=informacionEmpresa["id_rm"]
objFiltro='{"id":"'+id_rm+'"}'
datacifrada=encrypt(objFiltro.encode("utf-8"), clave)
print(datacifrada.decode("utf-8"))
payload={"dataBody":datacifrada.decode("utf-8")}
_request=req.Request("https://elasticprd.rues.org.co/api/Expediente/DetalleRM",data=json.dumps(payload).encode("utf-8"),
method="POST",headers={"content-type":"application/json"})
with req.urlopen(_request) as r:
rptaJson=json.loads( r.read().decode("utf-8"))
except Exception as e:
print("Error "+ str(e))
return JsonResponse(rptaJson,safe=False)
def pad(s):
return s + (16 - len(s) % 16) * chr(16 - len(s) % 16).encode()
def unpad(s):
return s[0:-ord(s[len(s)-1:])]
def bytes_to_key(data, salt, output=48):
assert len(salt) == 8, len(salt)
data += salt
key = md5(data).digest()
final_key = key
while len(final_key) < output:
key = md5(key + data).digest()
final_key += key
return final_key[:output]
def encrypt(data, passphrase):
salt = os.urandom(8)
key_iv = bytes_to_key(passphrase, salt, 32+16)
key = key_iv[:32]
iv = key_iv[32:]
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
encryptor = cipher.encryptor()
encrypted = encryptor.update(pad(data)) + encryptor.finalize()
cipherbyte = base64.b64encode(b"Salted__" + salt + encrypted)
return cipherbyte
def decrypt(data, passphrase):
data = base64.b64decode(data)
assert data[:8] == b'Salted__'
salt = data[8:16]
key_iv = bytes_to_key(passphrase, salt, 32+16)
key = key_iv[:32]
iv = key_iv[32:]
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
decryptor = cipher.decryptor()
plainbyte = unpad(decryptor.update(data[16:]) + decryptor.finalize())
return plainbyte
Comentarios
Publicar un comentario