June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
27
Task/RSA-code/Java/rsa-code.java
Normal file
27
Task/RSA-code/Java/rsa-code.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
public static void main(String[] args) {
|
||||
/*
|
||||
This is probably not the best method...or even the most optimized way...however it works since n and d are too big to be ints or longs
|
||||
This was also only tested with 'Rosetta Code' and 'Hello World'
|
||||
It's also pretty limited on plainText size (anything bigger than the above will fail)
|
||||
*/
|
||||
BigInteger n = new BigInteger("9516311845790656153499716760847001433441357");
|
||||
BigInteger e = new BigInteger("65537");
|
||||
BigInteger d = new BigInteger("5617843187844953170308463622230283376298685");
|
||||
Charset c = Charsets.UTF_8;
|
||||
String plainText = "Rosetta Code";
|
||||
System.out.println("PlainText : " + plainText);
|
||||
byte[] bytes = plainText.getBytes();
|
||||
BigInteger plainNum = new BigInteger(bytes);
|
||||
System.out.println("As number : " + plainNum);
|
||||
BigInteger Bytes = new BigInteger(bytes);
|
||||
if (Bytes.compareTo(n) == 1) {
|
||||
System.out.println("Plaintext is too long");
|
||||
return;
|
||||
}
|
||||
BigInteger enc = plainNum.modPow(e, n);
|
||||
System.out.println("Encoded: " + enc);
|
||||
BigInteger dec = enc.modPow(d, n);
|
||||
System.out.println("Decoded: " + dec);
|
||||
String decText = new String(dec.toByteArray(), c);
|
||||
System.out.println("As text: " + decText);
|
||||
}
|
||||
18
Task/RSA-code/Julia/rsa-code.julia
Normal file
18
Task/RSA-code/Julia/rsa-code.julia
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
function rsaencode(clearmsg::AbstractString, nmod::Integer, expub::Integer)
|
||||
bytes = parse(BigInt, "0x" * bytes2hex(collect(UInt8, clearmsg)))
|
||||
return powermod(bytes, expub, nmod)
|
||||
end
|
||||
|
||||
function rsadecode(cryptmsg::Integer, nmod::Integer, dsecr::Integer)
|
||||
decoded = powermod(encoded, dsecr, nmod)
|
||||
return join(Char.(hex2bytes(hex(decoded))))
|
||||
end
|
||||
|
||||
msg = "Rosetta Code."
|
||||
nmod = big"9516311845790656153499716760847001433441357"
|
||||
expub = 65537
|
||||
dsecr = big"5617843187844953170308463622230283376298685"
|
||||
|
||||
encoded = rsaencode(msg, nmod, expub)
|
||||
decoded = rsadecode(encoded, nmod, dsecr)
|
||||
println("\n# $msg\n -> ENCODED: $encoded\n -> DECODED: $decoded")
|
||||
Loading…
Add table
Add a link
Reference in a new issue