简体   繁体   中英

Encoding a UTF-8 string signed with SHA1RSA in Base64 and not obtaining right results

I have this string which I have to sign with a certificate and .key file. I never had any problems doing it, till the String contained Latin chars (ñ, é, í, etc). The string is converted to UTF-8 then signed with SHA1RSA algorithm, ending in a Base64 encoding. However the signing is giving me a weird answer and the Base64 encoding ends with a wrong format

cadena = "||3.2|2013-01-25T17:24:00|ingreso|PAGO EN UNA SOLA EXHIBICION|6386.21|MXN|7408.00|No identificado|NAUCALPAN DE JUÁREZ, ESTADO DE MEXICO|CAOS640116HT5|OSCAR MARTIN CARRERA|CTO. ORADORES 33|33|CD. SATELITE|NAUCALPAN DE JUÁREZ|ESTADO DE MEXICO|MÉXICO|53100|CTO. ORADORES 33|33|CD. SATELITE|NAUCALPAN DE JUÁREZ|ESTADO DE MEXICO|MÉXICO|53100|Persona Física con Actividad Empresarial|BAÑ930616R66|BAÑOMOBIL, S.A. DE C.V.|Av. 1° de Mayo|197|San. Lorenzo|TLALNEPANTLA DE BAZ|ESTADO DE MEXICO|MÉXICO|54047|1|NO APLICA|Dominio .com|Dominio por 1 año www.sanitariosportatiles.com|586.21|586.21|1|NO APLICA|Hospedaje 2 Gb|Hospedaje 2 Gb por 1 año www.sanitariosportatiles.com|5800.00|5800.00|IVA|16.00|1021.79|1021.79||";
Signature rsa = Signature.getInstance("SHA1withRSA");
rsa.initSign(pk);

rsa.update(cadena.getBytes("UTF-8"));
signature = rsa.sign();
sello = Base64.encodeBase64String(signature);

I omitted obtaining the key and certificate because this procedure works with strings without these characters.

Turns out the encoding and signing was being done right, however it returned a "\\r\\n" that should not been there. All it took was getting rid of it for it to work correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM