Created
December 29, 2016 11:58
-
-
Save kenduigraha/e5056c833f906ded23de681333263164 to your computer and use it in GitHub Desktop.
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
function enkripsi (input){ | |
var L = input.length | |
var data_before = [] | |
var data_after = [] | |
var square = 0 | |
var M = L | |
var result = [] | |
var check_string = true | |
// cek string | |
for(var i = 0 ; i < input.length ; i++){ | |
if(input[i].charCodeAt() < 65 || (input[i].charCodeAt() > 90 && input[i].charCodeAt() < 97) || input[i].charCodeAt() > 122){ | |
check_string = false | |
} | |
} | |
if(check_string === false){ | |
return "input must string" | |
}else if(input < 1 || input > 100){ | |
return "input must in range 1 - 100" | |
}else{ | |
while(Math.sqrt(M) % 1 != 0){ | |
M++ | |
} | |
square = Math.sqrt(M) | |
// fill data before encryption | |
for(var i = 0 ; i < square ; i++){ | |
data_before[i] = [] | |
for(var j = i*square ; j < (i*square)+square ; j++){ | |
if(input[j] === undefined){ | |
data_before[i].push("*") | |
}else{ | |
data_before[i].push(input[j]) | |
} | |
} | |
} | |
// fill data after encryption | |
for(var i = 0 ; i < square ; i++){ | |
data_after[i] = [] | |
for(var j = 0 ; j < square; j++){ | |
data_after[i][j] = data_before[square - j - 1][i] | |
} | |
} | |
// result (array) | |
for(var i = 0 ; i < square ; i++){ | |
for(var j = 0 ; j < square; j++){ | |
if(data_after[i][j] !== "*"){ | |
result.push(data_after[i][j]) | |
} | |
} | |
} | |
//result to string | |
result = result.join("") | |
return result | |
} | |
} | |
console.log(enkripsi("akucintakamu")) | |
console.log(enkripsi("maafAkuenggak")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment