Created
September 26, 2022 13:59
-
-
Save RobertApikyan/fe179737e97bb9b4ab737a5b80f98231 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
// A Simple Encryption/Decryption Algorithm | |
object CCED01_ASCII { | |
// text to encrypt | |
// rotationStep is a key for encryption | |
fun encrypt(text: String, rotationStep:Int): String { | |
val startIndex = 32 // Start of the printable characters of the ASCII table | |
val endIndex = 126 // End of the printable characters of the ASCII table | |
val length = endIndex - startIndex | |
val step = rotationStep%length | |
// Holds final encoded symbols. | |
val encoded = StringBuilder() | |
// 1. On each iteration we increment rotation by one, and subtracting it from the symbol's index to simulate that the | |
// ASCII array is rotating to the left side by @step character | |
var rotation = 0 | |
for (symbol in text) { | |
var index = symbol.toInt() - startIndex - rotation % length | |
// 2. negative value of the index means that the character for this index has ben moved to the end of the ASCII array | |
// by rotation, for that we add "length" to index's value to get the real rotated value. | |
if (index < 0) { | |
index += length | |
} | |
// 3. Get the character by index from the reversed ASCII table. "endIndex - index" to get the reverse value | |
encoded.append((endIndex - index).toChar()) | |
rotation += step | |
} | |
return encoded.toString() | |
} | |
// text to encrypt | |
// rotationStep is a key for decryption | |
fun decrypt(symbols: String, rotationStep:Int): String { | |
val startIndex = 32 // Start of the printable characters of the ASCII table | |
val endIndex = 126 // End of the printable characters of the ASCII table | |
val length = endIndex - startIndex | |
val step = rotationStep%length | |
// Holds final decoded symbols. | |
val decoded = StringBuilder() | |
var rotation = 0 | |
for (symbol in symbols) { | |
// simply reverse actions of the encode method. | |
var index = endIndex - symbol.toInt() + rotation % length | |
if (index >= length) { | |
index -= length | |
} | |
val digit = (startIndex+index).toChar() | |
decoded.append(digit) | |
rotation+=step | |
} | |
return decoded.toString() | |
} | |
} | |
fun main() { | |
printAlgorithm("This is a secure text") | |
printAlgorithm("0000000000000000000") | |
printAlgorithm("abcdefghijklmnopqrstuvwxyz") | |
printAlgorithm("123456789") | |
printAlgorithm(",.,,.;!@#$%^&*()(*&^%$#$%^&*") | |
// HB | |
printAlgorithm("01KZVPEBBLYXMREMRE166360602740.01104000-75.64247900111114") | |
// PERS | |
printAlgorithm("02KZVPEBBLNLVBLNJV166359992740.01104000-75.64247900199") | |
} | |
fun printAlgorithm(text: String,rotationStep:Int = 1) { | |
println(" text=\t $text length= ${text.length}") | |
val encoded = CCED01_ASCII.encrypt(text,rotationStep) | |
println("encoded v2=\t $encoded length= ${encoded.length}") | |
val decoded = CCED01_ASCII.decrypt(encoded,rotationStep) | |
println("decoded=\t $decoded length= ${decoded.length}") | |
if (text!=decoded){ | |
throw IllegalStateException("FAILURE: The text and decoded values are not the same") | |
} | |
println('\n') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment