Lua encryption algorythm - encryption

So I am working on a encryption algorythm in lua called S22. I have got the encryption part working but the decryption is a lot harder. Any ideas?
My encryption algorythm:
function S22Encrypt(MSG, bit)
local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
local finalstring = ""
local count = 0
MSG:gsub(".", function(c)
for i,v in pairs(resources) do
count = count + 1
if v == c then
finalstring = finalstring .. count * bit /69
count = 0
end
end
end)
return finalstring
end
My attempt at decryption:
function S22Decrypt(MSG, bit)
local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
local finalstring = ""
local count = 0
MSG:gsub(".", function(c)
for i,v in pairs(resources) do
count = count + 1
if v == c then
local nmr = count / bit *69
local count2 = 0
for a,b in pairs(resources) do
count2 = count2 + 1
if count2 == nmr then
finalstring = finalstring .. count2 / bit * 69
end
end
count = 0
end
end
end)
return finalstring
end

Decrypting is done by trying every possible value of count and comparing the corresponding code:
function S22Decrypt(MSG, bit)
local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
local finalstring = ""
local count = 0
local resource_idx = 0
while MSG ~= "" do
count = count + 1
if count > 1000 then
return nil, "Can not decrypt"
end
resource_idx = resource_idx + 1
if resource_idx > #resources then
resource_idx = resource_idx - #resources
finalstring = finalstring .. " "
end
local code = "" .. count * bit /69
if MSG:sub(1, #code) == code then
MSG = MSG:sub(#code + 1)
finalstring = finalstring .. resources[resource_idx]
count = #resources - resource_idx
resource_idx = 0
end
end
return finalstring
end
Please note that if you are encrypting on one system and decrypting on another then Lua version of encrypting script and Lua version of decrypting script must be either both Lua 5.3+ or both Lua 5.2-
For example, the word "hello" encrypted on Lua 5.1 will not be decryptable on Lua 5.4 (and vice versa) due to integer-to-string differences (42 vs 42.0)

Related

How to replace the wild card characters with sampled characters in R

I have the following sequence:
s0 <- "KDRH?THLA???RT?HLAK"
The wild card character there is indicated by ?.
What I want to do is to replace that character by sampled character from this vector:
AADict <- c("A", "R", "N", "D", "C", "E", "Q", "G", "H",
"I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V")
Since s0 has 5 wild cards ?, I would sample from AADict:
set.seed(1)
nof_wildcard <- 5
tolower(sample(AADict, nof_wildcard, TRUE))
Which gives [1] "d" "q" "a" "r" "l"
Hence the expected result is:
KDRH?THLA???RT?HLAK
KDRHdTHLAqarRTlHLAK
So the placement of the sampled character must be exactly in the same position as ?, but the order of the character is not important.
e.g. this answer is also acceptable: KDRHqTHLAdlaRTrHLAK.
How can I achieve that with R?
The other example are:
s1 <- "FKDHKHIDVKDRHRTHLAK????RTRHLAK"
s2 <- "FKHIDVKDRHRTRHLAK??????????"
One approach is to replace the "?" characters 'one at a time' using a loop, e.g.
s0 <- "KDRH?THLA???RT?HLAK"
AADict <- c("A", "R", "N", "D", "C", "E", "Q", "G", "H",
"I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V")
s0
#> [1] "KDRH?THLA???RT?HLAK"
repeat{s0 <- sub("\\?", sample(tolower(AADict), 1), s0); if(grepl("\\?", s0) == FALSE) break}
s0
#> [1] "KDRHtTHLAidwRTyHLAK"
s1 <- "FKDHKHIDVKDRHRTHLAK????RTRHLAK"
repeat{s1 <- sub("\\?", sample(tolower(AADict), 1), s1); if(grepl("\\?", s1) == FALSE) break}
s1
#> [1] "FKDHKHIDVKDRHRTHLAKrstaRTRHLAK"
s2 <- "FKHIDVKDRHRTRHLAK??????????"
repeat{s2 <- sub("\\?", sample(tolower(AADict), 1), s2); if(grepl("\\?", s2) == FALSE) break}
s2
#> [1] "FKHIDVKDRHRTRHLAKdvcfmheiqn"
Another approach which can also allow for sampling without replacement:
s0 <- "KDRH?THLA???RT?HLAK"
AADict <- c("A", "R", "N", "D", "C", "E", "Q", "G", "H",
"I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V")
matches <- gregexpr("\\?", s0)
regmatches(s0, matches) <- lapply(lengths(matches), sample, x = tolower(AADict), replace = FALSE)
s0
#> [1] "KDRHdTHLAlanRTiHLAK"
Created on 2022-10-22 by the reprex package (v2.0.1)
You could split your string in single characters which makes it easy to replace the wildcard without the need of a loop (was my first approach):
replace_wc <- function(x, dict) {
x <- strsplit(x, split = "")[[1]]
ix <- grepl("\\?", x)
x[ix] <- sample(dict, sum(ix), replace = TRUE)
return(paste0(x, collapse = ""))
}
s0 <- "KDRH?THLA???RT?HLAK"
AADict <- c(
"A", "R", "N", "D", "C", "E", "Q", "G", "H",
"I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V"
)
set.seed(1)
replace_wc(s0, tolower(AADict))
#> [1] "KDRHdTHLAqarRTlHLAK"
Here is a vectorized function to replace the "?" characters in a vector of strings.
fun <- function(x, dict = AADict) {
dict <- tolower(dict)
inx <- gregexpr("\\?", x)
sapply(seq_along(x), \(j) {
for(i in inx[[j]]) {
substr(x[j], i, i) <- sample(dict, 1L)
}
x[j]
})
}
AADict <- c("A", "R", "N", "D", "C", "E", "Q", "G", "H",
"I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V")
s0 <- "KDRH?THLA???RT?HLAK"
s1 <- "FKDHKHIDVKDRHRTHLAK????RTRHLAK"
s2 <- "FKHIDVKDRHRTRHLAK??????????"
fun(s0)
#> [1] "KDRHsTHLAwppRTwHLAK"
fun(s1)
#> [1] "FKDHKHIDVKDRHRTHLAKyfqfRTRHLAK"
fun(s2)
#> [1] "FKHIDVKDRHRTRHLAKnsfehqwmkv"
fun(c(s0, s1, s2))
#> [1] "KDRHiTHLAdssRTgHLAK" "FKDHKHIDVKDRHRTHLAKcdivRTRHLAK"
#> [3] "FKHIDVKDRHRTRHLAKfrpafwpnif"
Created on 2022-10-22 with reprex v2.0.2

Karate DSL Filter array using include inside a function

I am trying to build a function to filter users names which do not have an "alphanumeric" char as first letter.
I call this Java method first to get all the alphanumeric chars
public static String[] alphanumericAlphabet() {
String[] AlphabetWithDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
return AlphabetWithDigits;
}
Then this in my test:
def alphabet = alphanumericAlphabet()
def filterUsersCall = function(user) { return alphabet.includes(user.givenName[0]) }
def filteredUsers = karate.filter(usersList, filterUsersCall)
The second lines returns an error saying that "includes" is not a function.
How can I fix this ?
Thanks
Maybe #regex is better https://github.com/intuit/karate#fuzzy-matching
But here you go:
* def letters = [ 'A', 'B', 'C' ]
* def isValid = function(x){ return letters.contains(x.substring(0,1)) }
* def data = [ 'XXX', 'BBB', 'AAA' ]
* def temp = karate.filter(data, isValid)

How can I generate a user password in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I want to generate a passwort for user in R. Until now I'm using Excel and the following VB-Script. How canIi transform the functionality in an appropriate R script. Thank you very much.
myArr = Array("", 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", _
"C", "D", "E", "F", "G", "H", "J", "K", "L", "M", _
"N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", _
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", _
"!", "§", "$", "%", "&", "(", ")", "*")
intArr = UBound(myArr)
intA = Application.InputBox("Wieviele Passwörter sollen erstellt werden?", "PasswortGenerator", 10, , , , , 1)
If Not TypeName(intA) = "Boolean" Then
Randomize
intC = ActiveCell.Column
intZ = ActiveCell.Row
For intZ = intZ To intZ + intA 'Anzahl Passwörter
For intP = 1 To 8 'Anzahl Stellen des Passwortes
strP = strP & myArr(Int(intArr * Rnd + 1))
Next intP
If Application.WorksheetFunction.CountIf(ActiveCell.EntireColumn, strP) = 0 Then
ActiveSheet.Cells(intZ, intC).Value = strP
End If
strP = ""
Next intZ
End If
End Sub
Thank you very much.
OK, from bellow I'm trying to set up a function that will generate a password for each employee (mitarbeiter). I wnat to add a new variable 'passwort' in the function with the generated password for each employee. So, thanks again for your help.
genPsw <- function(num, len=8) {
# Vorgaben für die Passwortkonventionen festlegen
myArr <- c("", 2, 3, 4, 5, 6, 7, 8, 9, "A", "B",
"C", "D", "E", "F", "G", "H", "J", "K", "L", "M",
"N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"!", "§", "$", "%", "&", "(", ")", "*")
# replicate is a wrapper for the common use of sapply for repeated evaluation of an expression
# (which will usually involve random number generation).
replicate(num, paste(sample(myArr, size=len, replace=T), collapse=""))
# Lanege von dataframe mitarbeiter ermitteln
dim_mitarbeiter <- nrow(mitarbeiter)
for(i in 1:dim_mitarbeiter) {
# Random Number Generation
set.seed(i)
# Generate Passwort
mitarbeiter$passwort <- genPsw(i)
}
}
genPsw <- function(num, len=8) {
myArr <- c("", 2, 3, 4, 5, 6, 7, 8, 9, "A", "B",
"C", "D", "E", "F", "G", "H", "J", "K", "L", "M",
"N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"!", "§", "$", "%", "&", "(", ")", "*")
replicate(num, paste(sample(myArr, size=len, replace=T), collapse=""))
}
set.seed(1)
genPsw(3)
# [1] "JRf§E§&l" "j5ECnSsa" "p*St%Fk9"

Replacement with vectors

I have a vector with all consonants and I want every single consonant to be replaced with a "C" in a given data frame. Assume my data frame is x below:
x <- c("abacate", "papel", "importante")
v <- c("a", "e", "i", "o", "u")
c <- c("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z")
find <- c
replace <- "C"
found <- match(x, find)
ifelse(is.na(found), x, replace[found])
This is not working. Could anybody tell me what the problem is and how I can fix it?
Thanks
Regular expressions (gsub) are far more flexible in general, but for that particular problem you can also use the chartr function which will run faster:
old <- c("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
"p", "q", "r", "s", "t", "v", "w", "x", "y", "z")
new <- rep("C", length(old))
chartr(paste(old, collapse = ""),
paste(new, collapse = ""), x)
Use gsub to replace the letters in a character vector:
c <- c("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z")
consonants = paste(c("[", c, "]"), collapse="")
replaced = gsub(consonants, "C", x)
consonants becomes a regular expression, [bcdfghjklmnpqrstvwxyz], that means "any letter inside the brackets."
One of the reasons your code wasn't working is that match doesn't look for strings within other strings, it only looks for exact matches. For example:
> match(c("a", "b"), "a")
[1] 1 NA
> match(c("a", "b"), "apple")
[1] NA NA

How can I partition a vector?

How can I build a function
slice(x, n)
which would return a list of vectors where each vector except maybe the last has size n, i.e.
slice(letters, 10)
would return
list(c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
c("k", "l", "m", "n", "o", "p", "q", "r", "s", "t"),
c("u", "v", "w", "x", "y", "z"))
?
slice<-function(x,n) {
N<-length(x);
lapply(seq(1,N,n),function(i) x[i:min(i+n-1,N)])
}
You can use the split function:
split(letters, as.integer((seq_along(letters) - 1) / 10))
If you want to make this into a new function:
slice <- function(x, n) split(x, as.integer((seq_along(x) - 1) / n))
slice(letters, 10)

Resources