问题:
我如何在下面进行迭代,以便检查密钥“ round_1”的存在,在下一个脚本运行时,应检查密钥“ round_2”的exenseense,以此类推。每次遇到密钥丢失时,都应创建密钥。
按预期,它正在与“ round_1”一起使用。
<?php
// Create array skeleton.
$array_skeleton = array_fill(1, 3, "");
print_r($array_skeleton);
// Populate the skeleton with random numbers, values [1 to 6].
foreach($array_skeleton as $key => $value) {
$populated_array[$key] = random_int(1, 6);
};
print_r($populated_array);
// Create empty array for purpose to become multidimensional array.
$scorecard = [];
// Check if [round_1] is missing, if so create [round_1] and populate it.
if(!array_key_exists("round_1", $scorecard)) {
echo "round_1 is missing, creating it";
$scorecard["round_1"] = $populated_array;
}
print_r($scorecard);
运行第一个脚本后,结果按预期工作正常:
(
[round_1] => Array
(
[1] => 3
[2] => 4
[3] => 1
)
)
运行第二个脚本后的预期结果: 注意!正确的是,由于它们是随机创建的,因此每轮值都不同。
(
[round_1] => Array
(
[1] => 3
[2] => 4
[3] => 1
)
[round_2] => Array
(
[1] => 1
[2] => 4
[3] => 2
)
)
答案 0 :(得分:0)
我认为您的整个代码可以简化:
第一个使用随机数创建数组的定义函数:
app:layout_constraintBottom_toBottomOf="parent"
第二,假设您的密钥是由“ round_ @ INT @”模式组成的,则可以
const crypto = require('crypto');
const fs = require("fs");
function generateKeyFiles() {
const keyPair = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: ''
}
});
fs.writeFileSync("public_key", keyPair.publicKey);
fs.writeFileSync("private_key", keyPair.privateKey);
}
// Encrypt a string given a public key file. Encode the result in base64.
function encryptString (plaintext, publicKeyFile) {
const publicKey = fs.readFileSync(publicKeyFile, "utf8");
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext));
return encrypted.toString("base64");
}
// Encrypt a string given a cipherText (in base64) and a private key file.
function decryptString (ciphertext, privateKeyFile) {
const privateKey = fs.readFileSync(privateKeyFile, "utf8");
const decrypted = crypto.privateDecrypt(privateKey, Buffer.from(ciphertext, "base64"));
return decrypted.toString("utf8");
}
generateKeyFiles();
const plainText = "I have spread my dreams under your feet. Tread softly because you tread on my dreams.";
const cipherText = encryptString(plainText, "./public_key");
console.log();
console.log("Plaintext:", plainText);
console.log();
console.log("Ciphertext: ", cipherText);
console.log();
console.log("Decrypted Text: ", decryptString(cipherText, "private_key"));
console.log();
现在做:
function createRandomNumberArray($numOfElem, $maxRange) {
for ($i = 0; $i < $numOfElem; $i++)
$res[] = random_int(1, $maxRange);
return $res;
}