Created
September 4, 2018 10:18
-
-
Save bitsnaps/47ee7298bb5c14ca4a45627b2e124b76 to your computer and use it in GitHub Desktop.
Groovy simple random encryption
This file contains 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
/* | |
groovy equivalent of java encryption snippet: | |
https://gist.github.com/bitsnaps/9cea5e16e09065d20c22db6a4d15cb65 | |
*/ | |
def encrypt(def str){ | |
str.collect{s -> (Math.random()*9+1 as int).with{"${it}${((s.toCharacter() as int^it) as byte[]).encodeHex()}"} }.join('-') | |
} | |
def decrypt(def str){ | |
str.split('-').collect{((it[1..2].decodeHex()[0] as int)^ it[0] as int) as char}.join() | |
} | |
def msg = 'Hello' | |
def e = encrypt(msg) | |
println(e) | |
println(decrypt(e)) | |
// N.B. string input shouldn't be empty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment