Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Created September 4, 2018 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsnaps/47ee7298bb5c14ca4a45627b2e124b76 to your computer and use it in GitHub Desktop.
Save bitsnaps/47ee7298bb5c14ca4a45627b2e124b76 to your computer and use it in GitHub Desktop.
Groovy simple random encryption
/*
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