The gray-scale encryption of image encryption: the principle and steps of a round of encryption algorithm based on the key × decryption key ≡ 1 mod gray level

Algorithm basis

  Given number ppp and numberGGG , consider the congruence
px ≡ 1 mod G px\equiv 1\text{ }\bmod Gpx1 modG
gcd ⁡ ( p , G ) ∣ 1 \left. \gcd \left( p,G \right) \right|1 gcd(p,G)1 gcd ⁡ ( p , G ) = 1 \gcd \left( p,G \right)=1 gcd(p,G)=1 time congruence is solvable, Remember the solution as
x ≡ q mod G x\equiv q\text{ }\bmod Gxq modG
∀ a ∈ Z \forall a\in \mathbb{Z} aZ,记 p a     m o d   G = b pa\text{ }\bmod G=b p a modG=b,即有
p a ≡ b     m o d   G pa\equiv b\text{ }\bmod G p ab modG
则有
q b ≡ q ( p a ) = ( p q ) a ≡ a     m o d   G qb\equiv q\left( pa \right)=\left( pq \right)a\equiv a\text{ }\bmod G qbq( p a )=( p q )aa modG

Modular gray level encryption algorithm

  Given a picture size M × NM\times NM×ImageII of N (unit: pixel)I , determine its gray level numberGGG (usually2 22 ofnnPower of n , for example2 22 of8 8The 8th power is 256).

  Key : p ∈ Z p \in \mathbb{Z}pZ , satisfygcd ⁡ (p, G) = 1 \gcd \left( p,G \right)=1gcd(p,G)=1

  解钥 q ∈ Z q \in \mathbb{Z} qZ , by solvingpq ≡ 1 mod G pq\equiv 1\text{ }\bmod Gp q1 modG find out.

Encryption process

  1. Put the plain text III is encrypted as ciphertextI 1 { {I}_{1}}I1 I 1 { {I}_{1}} I1Is also a size M × NM\times NM×N , the gray level isGGThe image of G , and the gray value of each pixelI 1 (i, j) { {I}_{1}}\left( i,j \right)I1(i,j)
    I 1 ( i , j ) = I ( i , j ) × p     m o d   G ,   i = 0 , 1 , ⋯   , M − 1 ,   j = 0 , 1 , ⋯   , N − 1 { {I}_{1}}\left( i,j \right)=I\left( i,j \right)\times p\text{ }\bmod G,\text{ }i=0,1,\cdots ,M-1,\text{ }j=0,1,\cdots ,N-1 I1(i,j)=I(i,j)×p modG, i=0,1,,M1, j=0,1,,N1

  2. Decrypt ciphertext I 1 { {I}_{1}}I1Get plaintext III , the gray value at each pixelI (i, j) I\left( i,j \right)I(i,j)
    I ( i , j ) = I 1 ( i , j ) × q     m o d   G ,   i = 0 , 1 , ⋯   , M − 1 ,   j = 0 , 1 , ⋯   , N − 1 I\left( i,j \right)={ {I}_{1}}\left( i,j \right)\times q\text{ }\bmod G,\text{ }i=0,1,\cdots ,M-1,\text{ }j=0,1,\cdots ,N-1 I(i,j)=I1(i,j)×q modG, i=0,1,,M1, j=0,1,,N1

  It can be seen from the above steps that this is an encryption in the image space domain, an encryption that only changes the gray value of the pixel , and the decrypted image is no different from the original image . Since the key and the decryption key are generally not equal, they are also a kind of public key encryption .

Matlab code (with gray level of 2 8 = 256 { {2}^{8}}=25628=2 5 6 images as an example)

I = imread('待读入的图像路径+名称');  % 读入图像
G = 256;  % 确定灰度级,默认图像是8位的,即灰度级是2^8=256
p = 5;  % 确定密钥

% 检验密钥p与灰度级G的最大公因数是否为1
if gcd(p, G) ~= 1
    error('密钥p与灰度级G的最大公因数不是1,不存在解钥');
end

% 展示原图像
figure, imshow(I);

% 将图像I进行加密得到密文I1, 并可视化
I1 = mod( double(I) * p, G );
figure, imshow( uint8(I1) );

% 利用简单的循环迭代找出解钥q
% 因为gcd(p, 256) = 1, 因此p一定是奇数。
% 又由于pq mod256 = 1, 因此q也一定是奇数。
q = 1;
while mod( p*q, G ) ~= 1
    q = q + 2;
end

% 将密文I1解密得到I2, 并可视化
I2 = mod( I1 * q, G );
figure, imshow( uint8(I2) );

% 比较原图像I 与 解密得到的I2 是否一致(即该算法是否有损)
% 使用isequal(), 该函数返回真值当且仅当两个矩阵相同索引处的值均相等
if isequal( double(I), I2 )
    disp('解密得到的图像与原图像一致');
else
    disp('解密得到的图像与原图像不一致');
end

Guess you like

Origin blog.csdn.net/qq_44261017/article/details/109696233