Encryption Lua上的Lite加密
我通过http发送短视频数据,我需要一个针对二进制数据(视频)(带或不带密钥)的快速加密/解密算法,而不改变数据的大小。我用一个键做了一些排列算法,但我在原始数据的末尾添加了任何数据。这不是一个好主意,因为数据的大小发生了变化。编码器和解码器都必须包含此PRNG实现。Encryption Lua上的Lite加密,encryption,lua,Encryption,Lua,我通过http发送短视频数据,我需要一个针对二进制数据(视频)(带或不带密钥)的快速加密/解密算法,而不改变数据的大小。我用一个键做了一些排列算法,但我在原始数据的末尾添加了任何数据。这不是一个好主意,因为数据的大小发生了变化。编码器和解码器都必须包含此PRNG实现。 您可以更改secret\u key.*常量,但编码器和解码器上的常量必须相同 local set_seed, get_next_pseudo_random_byte do -- pseudo-random number g
您可以更改
secret\u key.*
常量,但编码器和解码器上的常量必须相同
local set_seed, get_next_pseudo_random_byte
do
-- pseudo-random number generator
-- Good statistical properties of PRN sequence:
-- uniformity,
-- long period of 255 * 2^45 (approximately 2^53),
-- unpredictability
-- Compatible with Lua 5.1, 5.2, 5.3, LuaJIT
-- all parameters in PRNG formula are derived from these 57 secret bits:
local secret_key_6 = 58 -- 6-bit arbitrary integer (0..63)
local secret_key_7 = 110 -- 7-bit arbitrary integer (0..127)
local secret_key_44 = 3580861008710 -- 44-bit arbitrary integer (0..17592186044415)
local floor = math.floor
local function primitive_root_257(idx)
-- returns primitive root modulo 257 (one of 128 existing roots, idx = 0..127)
local g, m, d = 1, 128, 2 * idx + 1
repeat
g, m, d = g * g * (d >= m and 3 or 1) % 257, m / 2, d % m
until m < 1
return g
end
local param_mul_8 = primitive_root_257(secret_key_7)
local param_mul_45 = secret_key_6 * 4 + 1
local param_add_45 = secret_key_44 * 2 + 1
-- state of PRNG (53 bits in total)
local state_45 = 0 -- from 0 to (2^45-1)
local state_8 = 2 -- from 2 to 256
function set_seed(seed_53)
-- set 53-bit integer as current seed
-- (seed is initially set to 0 when program starts)
state_45 = seed_53 % 35184372088832
state_8 = seed_53 % 255 + 2
end
local function get_random_32()
-- returns pseudorandom 32-bit integer (0..4294967295)
-- A linear congruential generator having full period of 2^45
state_45 = (state_45 * param_mul_45 + param_add_45) % 35184372088832
-- Lehmer RNG having period of 256
repeat
state_8 = state_8 * param_mul_8 % 257
until state_8 ~= 1 -- skip one value to reduce period from 256 to 255
-- (we need it to be coprime with 2^45)
-- Idea taken from PCG:
-- shift and rotate "state_45" by varying number of bits to get 32-bit result
local r = state_8 % 32
local n = floor(state_45 / 2^(13 - (state_8 - r) / 32)) % 2^32 / 2^r
return floor(n % 1 * 2^32) + floor(n)
end
local prev_values = {}
function get_next_pseudo_random_byte()
if #prev_values == 0 then
local rnd = get_random_32() -- value 0..4294967295
local low_16 = rnd % 65536
local high_16 = (rnd - low_16) / 65536
local b1 = low_16 % 256
local b2 = (low_16 - b1) / 256
local b3 = high_16 % 256
local b4 = (high_16 - b3) / 256
prev_values = {b1, b2, b3, b4}
end
return table.remove(prev_values)
end
end
在解码器端-几乎相同的代码,但使用减法而不是加法
-- prepare for decryption
set_seed(file_id)
-- The main loop - process file byte-by-byte
-- read next byte of encrypted file
local next_encoded_byte = ... -- 0..255
-- decode byte
local decoded_byte = (next_encoded_byte - get_next_pseudo_random_byte()) % 256
-- write decoded byte to decrypted file
...
该算法应该是加密强大的吗?@EgorSkriptunoff不。它不应该是强大的。你忘了问问题,你的文章太宽泛,离题了。使用websearch查找加密算法。@Pigget我正在查找算法,但没有找到请参见
-- prepare for decryption
set_seed(file_id)
-- The main loop - process file byte-by-byte
-- read next byte of encrypted file
local next_encoded_byte = ... -- 0..255
-- decode byte
local decoded_byte = (next_encoded_byte - get_next_pseudo_random_byte()) % 256
-- write decoded byte to decrypted file
...