Skip to content

Commit

Permalink
RSA
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Stieglmayr committed Oct 28, 2019
1 parent 0bc6769 commit 54051fa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
25 changes: 19 additions & 6 deletions Verschlüsselung.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,19 @@
<li>Teilerfremde Zahl zu &phi;(N) finden. Bedingung: 1&lt;e&lt;&phi;(N)</li>
<li>e*d mod(&phi;(N))=1 (d wird gesucht)</li>
</ol>
</div>
</div><br>
Private Key:(N;d)<br>
Public Key: (N;e)<br>
Public Key: (N;e)<br><br>
Verschlüsselt wird durch (m= Nachricht,c=Verschlüsselt)<br>
<div class="code-block">
c = m<sup>e</sup> mod(N)
</div>
Entschlüsselt wird mit<br>
<div class="code-block">
m = c<sup>d</sup> mod(N)
</div><br>
Anmerkung: Aufgrund von einem datatype Overflows und fehlender Präzision der Verwendeten Datentypen wurde eine
Funktion zum verschlüsseln verwendet, die höhere Mathematik verwendet um die Zahlen kleiner zu halten.
</div>
<div class="card-footer">
<a href="https://de.wikipedia.org/wiki/RSA-Kryptosystem">Wikipedia</a> + Student
Expand All @@ -202,6 +212,7 @@
</div>
</div>
</body>

<body>
<div class="modal fade" id="RSAModal">
<div class="modal-dialog modal-xl">
Expand All @@ -223,19 +234,19 @@ <h4>RSA-Algorithmus</h4>
</div>
<div class="col-md-2">
<label for="componentN">N:</label>
<input class="form-control" id="componentN" readonly />
<input class="form-control" id="componentN" />
</div>
<div class="col-md-2">
<label for="componentphiN">&phi;(N):</label>
<input class="form-control" id="componentphiN" readonly />
</div>
<div class="col-md-2">
<label for="componente">e:</label>
<input class="form-control" id="componente" readonly />
<input class="form-control" id="componente" />
</div>
<div class="col-md-2">
<label for="componente">d:</label>
<input class="form-control" id="componentd" readonly />
<input class="form-control" id="componentd" />
</div>
</div>
<div class="row">
Expand Down Expand Up @@ -282,13 +293,15 @@ <h4>RSA-Algorithmus</h4>
fürs Design verwendet.
</div>
</footer>

</html>

<script type="text/javascript">
$('#generateRSA').on("click", function () {
KeyGen();
encrypt();
});
$('#text').on('input',function(){
$('#text').on('input', function () {
encrypt();
});
$("#CäsarCodieren").on("click", function () {
Expand Down
50 changes: 49 additions & 1 deletion scripts/RSA.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,63 @@ function KeyGen() {
}

function encrypt() {
if($('#text').val()){
var N = $('#componentN').val();
N = parseInt(N);
var e = $('#componente').val();
e = parseInt(e);
var text = $('#text').val();
var Output = "";
for (var i in text) {
var KeyCode = text.charCodeAt(i);

KeyCode = Math.pow(KeyCode, e) % N;
KeyCode = powFun(KeyCode,e,N)
Output += KeyCode;
Output+=" ";
}
$('#encrypted').val(Output);





}
}
/* Encrypt function https://stackoverflow.com/questions/30630603/javascript-self-made-pow-with-modulo */
function addmod(x, y, n)
{
// Precondition: x<n, y<n
// If it will overflow, use alternative calculation
if (x + y <= x) x = x - (n - y) % n;
else x = (x + y) % n;
return x;
}

function sqrmod(a, n)
{
var b;
var sum = 0;

// Make sure original number is less than n
a = a % n;

// Use double and add algorithm to calculate a*a mod n
for (b = a; b != 0; b >>= 1) {
if (b & 1) {
sum = addmod(sum, a, n);
}
a = addmod(a, a, n);
}
return sum;
}

function powFun(base, ex, mo) {
var r;
if(ex === 0)
return 1;
else if(ex % 2 === 0) {
r = powFun(base, ex/2, mo) % mo ;
// return (r * r) % mo;
return sqrmod(r, mo);
}else return (base * powFun(base, ex - 1, mo)) % mo;
}

0 comments on commit 54051fa

Please sign in to comment.