Saltar al contenido

Cifrado rsa usando el ejemplo de código oaep js y java

Nuestro team de expertos despúes de varios días de investigación y de juntar de información, dieron con los datos necesarios, nuestro deseo es que te sea útil para tu plan.

Ejemplo 1: cifrado rsa js

//src https://www.sohamkamani.com/nodejs/rsa-encryption///e.g. https://gist.github.com/sohamkamani/b14a9053551dbe59c39f83e25c829ea7////////////////////////////////////////////////////////////////
npm install crypto
///////////////////////////////////////////////////////////////const crypto =require("crypto")// The `generateKeyPairSync` method accepts two arguments:// 1. The type ok keys we want, which in this case is "rsa"// 2. An object with the properties of the keyconst publicKey, privateKey = crypto.generateKeyPairSync("rsa",// The standard secure default length for RSA keys is 2048 bits
	modulusLength:2048,)// use the public and private keys// ...// This is the data we want to encryptconst data ="my secret data"const encryptedData = crypto.publicEncrypt(
		key: publicKey,
		padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
		oaepHash:"sha512",,// We convert the data string to a buffer using `Buffer.from`Buffer.from(data))// The encrypted data is in the form of bytes, so we print it in base64 format// so that it's displayed in a more readable formconsole.log("encypted data: ", encryptedData.toString("base64"))const decryptedData = crypto.privateDecrypt(
		key: privateKey,// In order to decrypt the data, we need to specify the// same hashing function and padding scheme that we used to// encrypt the data in the previous step
		padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
		oaepHash:"sha512",,
	encryptedData
)// The decrypted data is of the Buffer type, which we can convert to a// string to reveal the original dataconsole.log("decrypted data: ", decryptedData.toString())// Create some sample data that we want to signconst verifiableData ="this need to be verified"// The signature method takes the data we want to sign, the// hashing algorithm, and the padding scheme, and generates// a signature in the form of bytesconst signature = crypto.sign("sha512",Buffer.from(verifiableData),
	key: privateKey,
	padding: crypto.constants.RSA_PKCS1_PSS_PADDING,)console.log(signature.toString("base64"))// To verify the data, we provide the same hashing algorithm and// padding scheme we provided to generate the signature, along// with the signature itself, the data that we want to// verify against the signature, and the public keyconst isVerified = crypto.verify("sha512",Buffer.from(verifiableData),
		key: publicKey,
		padding: crypto.constants.RSA_PKCS1_PSS_PADDING,,
	signature
)// isVerified should be `true` if the signature is validconsole.log("signature verified: ", isVerified)

Ejemplo 2: cifrado RSA, JavaScript

<script type="text/javascript">$(function()//Change the key size value for new keys$(".change-key-size").each(function(index, value)var el =$(value);var keySize = el.attr('data-value');
            el.click(function(e)var button =$('#key-size');
                button.attr('data-value', keySize);
                button.html(keySize +' bit ');
                e.preventDefault();););// Execute when they click the button.$('#execute').click(function()// Create the encryption object.var crypt =newJSEncrypt();// Set the private.
            crypt.setPrivateKey($('#privkey').val());//return;// If no public key is set then set it here...var pubkey =$('#pubkey').val();if(!pubkey)$('#pubkey').val(crypt.getPublicKey());// Get the input and crypted values.var input =$('#input').val();var crypted =$('#crypted').val();// Alternate the values.if(input)$('#crypted').val(crypt.encrypt(input));$('#input').val('');elseif(crypted)var decrypted = crypt.decrypt(crypted);if(!decrypted)
                    decrypted ='This is a test!';$('#input').val(decrypted);$('#crypted').val(''););vargenerateKeys=function()var sKeySize =$('#key-size').attr('data-value');var keySize =parseInt(sKeySize);var crypt =newJSEncrypt( default_key_size: keySize );var async =$('#async-ck').is(':checked');var dt =newDate();var time =-(dt.getTime());if(async)$('#time-report').text('.');var load =setInterval(function()var text =$('#time-report').text();$('#time-report').text(text +'.');,500);
                crypt.getKey(function()clearInterval(load);
                    dt =newDate();
                    time +=(dt.getTime());$('#time-report').text('Generated in '+ time +' ms');$('#privkey').val(crypt.getPrivateKey());$('#pubkey').val(crypt.getPublicKey()););return;
            crypt.getKey();
            dt =newDate();
            time +=(dt.getTime());$('#time-report').text('Generated in '+ time +' ms');$('#privkey').val(crypt.getPrivateKey());$('#pubkey').val(crypt.getPublicKey());;// If they wish to generate new keys.$('#generate').click(generateKeys);generateKeys(););</script>

Aquí tienes las reseñas y valoraciones

Te invitamos a añadir valor a nuestro contenido cooperando tu veteranía en las explicaciones.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *