Pregunta

This is my very first question in this website about javascript code. I've been given an RSA encryption code and our professor has given us a really wierd way to encrypt text.

The user enters the plain-text in the text-field and then the program should be able to:

First- Break the text every 4 characters and store them into an array(DONE!)

PROBLEM is now here that I'm facing... Second- Calculate the numeric values of each block using a radix= 16.(ex: Num("abc")=1*16^2+2*16^1+3*16^0)) Those letters should be converted into their matching numbers, like,

space= 0
A,a =1
B, b =2
C , c = 3
. 
. Z, z= 26

and then sent to a function that does that calculations! :-(

And then the program goes on encrypting each block using the encryption function and so on... But, for now, I'm looking a way to solve this mystery first..

I've searched alot about it, but found nothing!! Kindly, guide me on how to do it. Thank you very much!!!

This is my javascript code..

    function start()
    {
        var eB = document.getElementById( "encryptButton" );
        eB.addEventListener("click", str2num, false);
    }

function split(){
    var str = document.getElementById( "inputField" ).value;
    var chunks = [];
    for (var i = 0, charsLength = str.length; i < charsLength; i += 3) {
    chunks.push(str.substring(i, i + 3));
    }
   document.getElementById("results").innerHTML=chunks;
}

window.addEventListener("load", start, false);

This is the HTML one (the form where User inputs the text)..

<html>
   <head>
      <meta charset = "utf-8">
      <title>RSA Algorithm</title>
      <script src = "rsaj.js"></script>
   </head>
   <body>
      <form action = "#">
         <p>Enter a sentence to encrypt:</p>
         <p><input id = "inputField" type = "text">
            <input id = "encryptButton" type = "button" value = "Encrypt"></p>      
         <div id = "results"> </div>
      </form>
   </body>
</html>
¿Fue útil?

Solución

well, the parseInt function within Javascript has the radix parameter as the second param that can convert string to numbers based on the given radix

So you can do

var convert=parseInt("f", 16); //16
var convert2=parseInt("af", 16); //175

What I don't understand is your usage of "z" as an example. Maybe I don't understand, but radix 16 is HEX-Code and therefor exists of the following symbols; 012345689abcdef

Otros consejos

Your question changed a little bit.

I have the following:

  • create map with 'encryption' keys.
  • listen to changes from input field
  • uppercase the input value.
  • iterate through value for every character.
  • search the map and get the index of it (default value =0, space aswell).
  • increase the total value based on search.
  • every iteration decrements the power.
  • return results to div.results

jsfiddle: http://jsfiddle.net/kychan/X8R2p/4/

var map     = ' 123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var result  = document.getElementById('result');

function calculate(e)
{
    //    lower the cases.
    var x = e.value.toUpperCase();

    //    var holding total value.
    var total = 0;

    //    decrement on each character.
    //    start with the length of entered value -1.
    var decrementor = e.value.length-1;

    //    iterate through the string.
    for (var i in x)
    {
        //    search in map.
        var r = map.indexOf(x[i]);

        //    0 = 0, so no need to sum up or store it in the map.
        //    space = 0, in the map because 1 needs to return 1.
        if (r>-1)
        {
            total     +=    r*Math.pow(16, decrementor);

            decrementor--;
        }
    }

    //    print results.
    result.innerHTML = total;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top