Created
June 15, 2017 13:24
-
-
Save demacdolincoln/79effc0d75938f95c7d6cd7fa7e0165c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Pkg.add("Primes") | |
using Primes | |
function genBigPrime() | |
prime = 0 | |
while prime == 0 | |
#possiblePrime = rand(BigInt(1e+50):BigInt(1e+100)) | |
possiblePrime = Int(rand(1e+2:1e+4)) | |
if isprime(possiblePrime) | |
prime = possiblePrime | |
end | |
end | |
prime | |
end | |
mdc(a, b) = b == 0 ? a : mdc(b, a % b) | |
p, q = genBigPrime(), genBigPrime() | |
N = p*q | |
ϕ = (p-1) * (q-1) | |
E = begin | |
coprime = 0 | |
c = 2 | |
while coprime == 0 | |
if mdc(c, ϕ) == 1 | |
coprime = c | |
end | |
c += 1 | |
end | |
coprime | |
end | |
D = invmod(E, ϕ) | |
println(repeat("*", 80)) | |
println("parametros: | |
p, q = $p, $q | |
N = $N | |
ϕ = $ϕ | |
E = $E | |
D = $D") | |
println(repeat("*", 80)) | |
println("digite uma mensagem:") | |
msgEnc = [BigInt(i)^E % N for i in readline(STDIN)] | |
println(repeat("*", 80)) | |
println("\nMensagem Encriptada: $(msgEnc |> join)") | |
msgDec = map(x->Char(x^D % N), msgEnc) |> join | |
println("Mensagem Decriptada: $msgDec") | |
println(repeat("*", 80)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment