好的,所以我已經梳理了互聯網以解決我的問題,我只能說我在R的工作方式上有點幼稚。爲什麼我的if語句存儲錯誤的數據類型?
以下是我的代碼,用於從系統時鐘生成公鑰和私鑰並使用它來嘗試解密加密的郵件的函數。這一點工作正常,但顯然,隨着它經歷不同的隨機世代,它回來了大量的垃圾和NULL數據。
我想通過使用grep來過濾掉它,並測試grep的結果是否爲1,解碼後的消息是否會被放入列表中。
問題是,無論我如何提出if語句,我的列表都與無意義條目和NULL條目混淆。 我試過了,!is.null,is.character。測試== 1.等等,但似乎沒有任何工作。這個列表根本沒有被填充,或者它被在if語句中運行的每個條目填充。
任何意見,將不勝感激。謝謝:)
編輯:好吧,原諒我,因爲這些是複製和粘貼工作,以提供清晰。第一個代碼是我用來加密消息的代碼。
require(gmp)
source("convert.R")
p <- nextprime(urand.bigz(size=51, seed=as.bigz(Sys.time())))
q <- nextprime(urand.bigz(size=50))
n <- p*q
finde <- function(phi) {
r <- floor(log(phi, base = 2))
y <- 0 # initialise
while(y != 1) {
e <- urand.bigz(nb = 1, size = r)
y <- gcd.bigz(e, phi)
}
return(e)
}
phi <- (p-1) * (q-1)
e <-finde(phi)
d <- inv.bigz(e, phi)
text1 <- c("I want to eat a baby panda with my bare teeth and hands. Just so I know there's something else in this world suffering more than myself, right now.")
m <- blocks(text1, n) # arguments are text1 (message) and n (public key)
u <- as.bigz((as.bigz(m, n)^e))
dput(u, file="codedmessage.R")
第二個是包含在「convert.R」源文件中的代碼:
blocks <- function(txt, n) {
x <- strtoi(charToRaw(txt), 16L)
ll <- length(x)
bl <- floor(log(n, base=256)) # block length (how large the blocks must be)
nb <- floor(ll/bl)
wp <- bl*nb
rem <- ll - wp
s <- as.bigz(vector(mode="numeric", length=0))
u <- 0
while(u < wp) {
total <- as.bigz(0)
for(i in 1:bl) {
total <- 256 * total + x[i+u]
}
u <- u + bl
s <- c(s, total)
}
if(rem > 0) {
total <- as.bigz(0)
for(i in 1:rem) {
total <- 256 * total + x[i + wp]
}
s <- c(s, total)
}
return(s)
}
words <- function(blocknum) {
w <- vector(mode="numeric", length=0)
wl <- blocknum
while(as.bigz(wl) > 0) {
rem <- as.bigz(wl) %% 256
w <- c(rem, w)
wl <- (as.bigz(wl) - as.bigz(rem))/256
}
return(w)
}
dectext <- function(listnum) {
len <- length(listnum)
newls <- as.integer(vector(mode="numeric", length=0))
for(i in 1:len) {
temp <- as.integer(words(listnum[i]))
newls <- c(newls, temp)
}
return(rawToChar(as.raw(newls)))
}
最後的最後一個代碼解密和編譯我遇到的問題列表功能。
finde <- function(phi) {
r <- floor(log(phi, base = 2))
y <- 0 # initialise
while(y != 1) {
e <- urand.bigz(nb = 1, size = r)
y <- gcd.bigz(e, phi)
}
return(e)
}
FindKey <- function(a, y) {
x <<- 1 #initialisation
decodedlist <<- list() #initialisation
while (x<7200) {
print(x)
print(a)
p <- nextprime(urand.bigz(size=51, seed=as.bigz(a)))
q <- nextprime(urand.bigz(size=50))
n <- p*q
phi <- (p-1) * (q-1)
phi
e <-finde(phi)
d <- inv.bigz(e, phi)
recieved<-dget(file=y)
v<-as.bigz(as.bigz(recieved, n)^d)
tryCatch({
decodetext<-dectext(v)
Decrypt<- capture.output(cat(decodetext))
print(Decrypt)
test <- grep("and", Decrypt)
if (!is.null(Decrypt)){
if (is.character(Decrypt)){
decodedlist[[x]] <<- Decrypt
}else{return}}else{return}
}, warning = function(war) {
return()
}, error = function(err){
return()
}, finally = {
x=x+1
a=a-1})
}
}
對不起它的長。但我真的不知道該怎麼辦:(
'nextprime'和'as.bigz'不會出現在基地R.哪個包(S)你正在用嗎?這是我的好奇心,與解決您的問題無關。 – shea
請提供一個可重複的例子。 –
也許Decrypt *包含* nulls without * being * null? – Acccumulation