September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
44
Task/RSA-code/FreeBASIC/rsa-code.freebasic
Normal file
44
Task/RSA-code/FreeBASIC/rsa-code.freebasic
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
' version 17-01-2017
|
||||
' compile with: fbc -s console
|
||||
|
||||
#Include Once "gmp.bi"
|
||||
|
||||
Dim As Mpz_ptr e, d, n, pt, ct
|
||||
|
||||
e = Allocate(Len(__mpz_struct))
|
||||
d = Allocate(Len(__mpz_struct))
|
||||
n = Allocate(Len(__mpz_struct))
|
||||
pt = Allocate(Len(__mpz_struct)) : mpz_init(pt)
|
||||
ct = Allocate(Len(__mpz_struct)) : mpz_init(ct)
|
||||
|
||||
mpz_init_set_str(e, "65537", 10)
|
||||
mpz_init_set_str(d, "5617843187844953170308463622230283376298685", 10)
|
||||
mpz_init_set_str(n, "9516311845790656153499716760847001433441357", 10)
|
||||
|
||||
Dim As ZString Ptr plaintext : plaintext = Allocate(1000)
|
||||
Dim As ZString Ptr text : text = Allocate(1000)
|
||||
*plaintext = "Rosetta Code"
|
||||
|
||||
mpz_import(pt, Len(*plaintext), 1, 1, 0, 0, plaintext)
|
||||
|
||||
If mpz_cmp(pt, n) > 0 Then GoTo clean_up
|
||||
|
||||
mpz_powm(ct, pt, e, n)
|
||||
gmp_printf(!" Encoded: %Zd\n", ct)
|
||||
|
||||
mpz_powm(pt, ct, d, n)
|
||||
gmp_printf(!" Decoded: %Zd\n", pt)
|
||||
|
||||
mpz_export(text, NULL, 1, 1, 0, 0, pt)
|
||||
Print "As string: "; *text
|
||||
|
||||
clean_up:
|
||||
DeAllocate(plaintext) : DeAllocate(text)
|
||||
mpz_clear(e) : mpz_clear(d) : mpz_clear(n)
|
||||
mpz_clear(pt) : mpz_clear(ct)
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
28
Task/RSA-code/Kotlin/rsa-code.kotlin
Normal file
28
Task/RSA-code/Kotlin/rsa-code.kotlin
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// version 1.1.4-3
|
||||
|
||||
import java.math.BigInteger
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val n = BigInteger("9516311845790656153499716760847001433441357")
|
||||
val e = BigInteger("65537")
|
||||
val d = BigInteger("5617843187844953170308463622230283376298685")
|
||||
val c = Charsets.UTF_8
|
||||
val plainText = "Rosetta Code"
|
||||
println("PlainText : $plainText")
|
||||
val bytes = plainText.toByteArray(c)
|
||||
val plainNum = BigInteger(bytes)
|
||||
println("As number : $plainNum")
|
||||
if (plainNum > n) {
|
||||
println("Plaintext is too long")
|
||||
return
|
||||
}
|
||||
|
||||
val enc = plainNum.modPow(e, n)
|
||||
println("Encoded : $enc")
|
||||
|
||||
val dec = enc.modPow(d, n)
|
||||
println("Decoded : $dec")
|
||||
|
||||
val decText = dec.toByteArray().toString(c)
|
||||
println("As text : $decText")
|
||||
}
|
||||
32
Task/RSA-code/Scala/rsa-code.scala
Normal file
32
Task/RSA-code/Scala/rsa-code.scala
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
object RSA_saket{
|
||||
val d = BigInt("5617843187844953170308463622230283376298685")
|
||||
val n = BigInt("9516311845790656153499716760847001433441357")
|
||||
val e = 65537
|
||||
val text = "Rosetta Code"
|
||||
val encode = (msg:BigInt) => pow_mod(msg,e,n)
|
||||
val decode = (msg:BigInt) => pow_mod(msg,d,n)
|
||||
val getmsg = (txt:String) => BigInt(txt.map(x => "%03d".format(x.toInt)).reduceLeft(_+_))
|
||||
def pow_mod(p:BigInt, q:BigInt, n:BigInt):BigInt = {
|
||||
if(q==0) BigInt(1)
|
||||
else if(q==1) p
|
||||
else if(q%2 == 1) pow_mod(p,q-1,n)*p % n
|
||||
else pow_mod(p*p % n,q/2,n)
|
||||
}
|
||||
def gettxt(num:String) = {
|
||||
if(num.size%3==2)
|
||||
("0" + num).grouped(3).toList.foldLeft("")(_ + _.toInt.toChar)
|
||||
else
|
||||
num.grouped(3).toList.foldLeft("")(_ + _.toInt.toChar)
|
||||
}
|
||||
def main(args: Array[String]): Unit = {
|
||||
println(f"Original String \t: "+text)
|
||||
val msg = getmsg(text)
|
||||
println(f"Converted Signal \t: "+msg)
|
||||
val enc_sig = encode(msg)
|
||||
println("Encoded Signal \t\t: "+ enc_sig)
|
||||
val dec_sig = decode(enc_sig)
|
||||
println("Decoded String \t\t: "+ dec_sig)
|
||||
val rec_msg = gettxt(dec_sig.toString)
|
||||
println("Retrieved Signal \t: "+rec_msg)
|
||||
}
|
||||
}
|
||||
16
Task/RSA-code/Zkl/rsa-code.zkl
Normal file
16
Task/RSA-code/Zkl/rsa-code.zkl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
var BN=Import("zklBigNum");
|
||||
|
||||
n:=BN("9516311845790656153499716760847001433441357");
|
||||
e:=BN("65537");
|
||||
d:=BN("5617843187844953170308463622230283376298685");
|
||||
|
||||
const plaintext="Rossetta Code";
|
||||
pt:=BN(Data(Int,0,plaintext)); // convert string (as stream of bytes) to big int
|
||||
if(pt>n) throw(Exception.ValueError("Message is too large"));
|
||||
|
||||
println("Plain text: ",plaintext);
|
||||
println("As Int: ",pt);
|
||||
ct:=pt.powm(e,n); println("Encoded: ",ct);
|
||||
pt =ct.powm(d,n); println("Decoded: ",pt);
|
||||
txt:=pt.toData().text; // convert big int to bytes, treat as string
|
||||
println("As String: ",txt);
|
||||
Loading…
Add table
Add a link
Reference in a new issue