Skip to content
Snippets Groups Projects
Commit c0bd3f6e authored by M33's avatar M33 :speech_balloon:
Browse files

add a full and light version

parent 962c6711
No related branches found
No related tags found
No related merge requests found
No preview for this file type
# Package # Package
version = "0.9.0" version = "0.9.4"
author = "m33" author = "m33"
description = "Library implementing a variation of Simple And Efficient Data Encryption Algorithm (INTERNATIONAL JOURNAL OF SCIENTIFIC & TECHNOLOGY RESEARCH VOLUME 8, ISSUE 12, DECEMBER 2019 ISSN 2277-8616)" description = "Library implementing a variation of Simple And Efficient Data Encryption Algorithm (INTERNATIONAL JOURNAL OF SCIENTIFIC & TECHNOLOGY RESEARCH VOLUME 8, ISSUE 12, DECEMBER 2019 ISSN 2277-8616)"
license = "MIT" license = "MIT"
......
...@@ -21,11 +21,17 @@ proc gen_iv*(random_data: string): string = ...@@ -21,11 +21,17 @@ proc gen_iv*(random_data: string): string =
# #
# First pass of string encryption, xor the secret key and initialization vector # First pass of string encryption, xor the secret key and initialization vector
# #
proc encrypt_stage1*(secret: string, iv: string): string = proc encrypt_stage1(secret: string, iv: string, len: int): string =
var hidden_str: string var hidden_str: string
var i = 0 var i = 0
while i < secret.len:
hidden_str.add(cast[char](cast[int32](secret[i]) xor cast[int32](iv[(i mod iv.len)]))) # xor on repeated secret text and iv up to cleartext length
while i < len:
hidden_str.add(cast[char](
cast[int32](
secret[i mod secret.len]) xor
cast[int32](
iv[(i mod iv.len)])))
i+=1 i+=1
return hidden_str return hidden_str
#endproc #endproc
...@@ -36,31 +42,68 @@ proc encrypt_stage1*(secret: string, iv: string): string = ...@@ -36,31 +42,68 @@ proc encrypt_stage1*(secret: string, iv: string): string =
# #
# First pass of string decryption, xor secret,initialization vector, then cypertext and the product # First pass of string decryption, xor secret,initialization vector, then cypertext and the product
# #
proc decrypt_stage1*(hidden: string, iv: string): string = proc decrypt_stage1(hidden: string, iv: string, len: int): string =
var product: string var product: string
var i = 0 var i = 0
while i < hidden.len:
product.add(cast[char](cast[int32](hidden[i]) xor cast[int32](iv[(i mod iv.len)]))) # xor on repeated ciphertext and iv up to cleartext length
while i < len:
product.add(cast[char](
cast[int32](
hidden[i mod hidden.len]) xor
cast[int32](
iv[(i mod iv.len)])))
i+=1 i+=1
return product return product
#endproc #endproc
#
# encrypt(text, secret, initialization vector, text length): string
#
# Simple encryption for text, using secret and a random initialization vector
# The lenght of cleartext message is needed, this will produce an encrypted message
# following the SAEDEA paper
#
proc encrypt*(text: string, secret: string, iv: string, len: int): string =
let intermediate = encrypt_stage1(secret, iv, len)
#return encode(encrypt_stage1(text, intermediate, text.len))
return encrypt_stage1(text, intermediate, len)
#endproc
# #
# encrypt(text, secret, initialization vector): string # encrypt(text, secret, initialization vector): string
# #
# Light version
# Simple encryption for text, using secret and a random initialization vector # Simple encryption for text, using secret and a random initialization vector
# #
proc encrypt*(text: string, secret: string, iv: string): string = proc encrypt*(text: string, secret: string, iv: string): string =
return encode(encrypt_stage1(text, encrypt_stage1(secret, iv))) let intermediate = encrypt_stage1(secret, iv, text.len)
#return encode(encrypt_stage1(text, intermediate, text.len))
return encrypt_stage1(text, intermediate, intermediate.len)
#endproc #endproc
#
# decrypt(hidden, secret, initialization vector, lenght of the cleartext message): string
#
# Simple decryption for ciphertext, using secret and a random initialization vector
# The original cleartext lenght is needed
#
proc decrypt*(hidden_str: string, secret: string, iv: string, len: int): string =
let intermediate = decrypt_stage1(secret, iv, len)
#return decrypt_stage1(decode(hidden_str), intermediate, len)
return decrypt_stage1(hidden_str, intermediate, len)
#endproc
# #
# decrypt(hidden, secret, initialization vector): string # decrypt(hidden, secret, initialization vector): string
# #
# Light version
# Simple decryption for ciphertext, using secret and a random initialization vector # Simple decryption for ciphertext, using secret and a random initialization vector
# #
proc decrypt*(hidden_str: string, secret: string, iv: string): string = proc decrypt*(hidden_str: string, secret: string, iv: string): string =
return decrypt_stage1(decode(hidden_str), decrypt_stage1(secret, iv)) let intermediate = decrypt_stage1(secret, iv, hidden_str.len)
#return decrypt_stage1(decode(hidden_str), intermediate, len)
return decrypt_stage1(hidden_str, intermediate, intermediate.len)
#endproc #endproc
File added
# This is just an example to get you started. You may wish to put all of your
# tests into a single file, or separate them into multiple `test1`, `test2`
# etc. files (better names are recommended, just make sure the name starts with
# the letter 't').
#
# To run these tests, simply execute `nimble test`. # To run these tests, simply execute `nimble test`.
import unittest import unittest
import strutils
import times import times
import libSAEDEA import libSAEDEA
var text = "This is a clear text message... 12 12 123 and the current time is:" & $getTime() proc cmpStrChars(s1: string, s2: string): bool =
var secret = "shared secret" if s1.len != s2.len:
var iv = gen_iv("true random data") return false
var encrypted = encrypt(text, secret, iv) for i in 0..s1.len-1:
var decrypted = decrypt(encrypted, secret, iv) if s1[i] != s2[i]:
echo "Secret:", secret return false
echo "IV:", iv return true
echo "Cleartext:", text
echo "Decrypted:", decrypted let text = "This is a clear text message... ABCDEF 12 12 123 1234 12345 123456 $*[]@!%ù 🤖😱🎰🔮📿💈⚗️🔭🔬 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod... And the current time is:" & $getTime()
echo "Encrypted:", encrypted let secret = "A shared secret"
check count(text, decrypted, false) == 1 let iv = gen_iv("some random data" & $getTime())
var encrypted = encrypt(text, secret, iv, text.len)
var decrypted = decrypt(encrypted, secret, iv, text.len)
var encrypted_light = encrypt(text, secret, iv)
var decrypted_light = decrypt(encrypted, secret, iv)
#echo "Secret:", secret
#echo "IV:", iv
#echo "Cleartext:", text
#echo "Decrypted:", decrypted
#echo "Encrypted:", encrypted
#echo "Decrypted_light:", decrypted_light
echo "Matching test"
check cmpStrChars(text, decrypted) == true
echo "Wrong secret test"
decrypted = decrypt(encrypted, "wrong secret", iv, text.len)
check cmpStrChars(text, decrypted) == false
echo "Wrong IV test"
decrypted = decrypt(encrypted, secret, "wrong iv", text.len)
check cmpStrChars(text, decrypted) == false
echo "Wrong length test"
decrypted = decrypt(encrypted, secret, iv, 987654)
check cmpStrChars(text, decrypted) == false
echo "All wrong test"
decrypted = decrypt(encrypted, "wrong value", "wrong value", 123456)
check cmpStrChars(text, decrypted) == false
echo "Matching test with light encryption"
check cmpStrChars(text, decrypted_light) == true
echo "Wrong secret test with light encryption"
decrypted_light = decrypt(encrypted, "wrong secret", iv)
check cmpStrChars(text, decrypted_light) == false
echo "Wrong IV test with light encryption"
decrypted_light = decrypt(encrypted, secret, "wrong iv")
check cmpStrChars(text, decrypted_light) == false
echo "All wrong test with light encryption"
decrypted_light = decrypt(encrypted, "wrong secret", "wrong iv")
check cmpStrChars(text, decrypted_light) == false
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment