Introduction to RSA image encryption

The acquisition, transmission and processing of image data have spread to every corner of the digital era, and the security of image is becoming increasingly serious, especially in military, commercial and medical fields. RSA is a publicly used encryption algorithm that uses asymmetric encryption. Below, based on the study of image encryption algorithm, the author applies RSA algorithm to image encryption technology, focuses on algorithm optimization, key generation, key distribution and file transfer, and proposes a complete set of digital image encryption and decryption solutions.

1 Basic principle of RSA The RSA algorithm uses an asymmetric cryptographic encryption system. During the whole encryption process, the key and the algorithm are separated independently, which ensures that the key can be allocated more efficiently. It adopts the difficulty of large prime factor decomposition to ensure the security. At present, there is still no good solution to the problem of large prime factor decomposition. The basic steps of RSA encryption are as follows: Step1 select two large prime numbers a and b, and calculate the product of the two numbers m=a×b. Step2 select large integer encryption key d and ensure that d is mutually quality with (a-1)×(b-1). Step3 find the decryption key e,e×d=1mod(a-1)×(b-1). Step4 encrypt plaintext P into ciphertext C(C=Pdmod m), and decrypt ciphertext C into plaintext P(P=Cemod m). It can be seen from the above steps that only m and D can not calculate the decryption key E. It can be seen that anyone can encrypt plaintext P, but can decrypt ciphertext C only after obtaining authorization.

2 RSA image encryption process Design RSA image encryption process design is to read the image first and convert it into hexadecimal data stream; Then, the key required by RSA algorithm is generated, and the key and the image are power-multiplied and modular operation to generate decimal data. Finally, the data is converted into a string data stream for saving. Among them, the most important is the generation of key [, which determines the final image encryption effect. The generation process of key includes four parts: automatic generation and storage of large prime numbers, arithmetic operation of large prime numbers, power module and multiplication module operation of large numbers, and automatic generation of prime numbers. The specific encryption process is shown in Figure 1.

3 RSA image encryption function designAccording to the RSA image encryption process, image encryption functions are designed, including RSA image encryption, decryption and key generation. The system function modules are shown in Figure 2. 1) Digital image encryption. The digital image is read by byte stream and converted into hexadecimal stream. RSA algorithm is used to encrypt the hexadecimal stream, and the encrypted data is converted into text output. 2) Digital image decryption. After loading the encrypted image file, the key is used to decrypt it and restore the encrypted image.Figure 1 FLOW chart of RSA image encryptionFigure 2 System function module Figure 3) Encryption and decryption preview In the process of digital image encryption and decryption, ensure the visualization of the digital image to be encrypted and decrypted. 4) Automatic generation of key. The RSA algorithm requires two large primes to be entered by users. This algorithm automatically generates large primes and keys to reduce user operations and ensure that the number of primes used for keys is large enough. 5) Key length setting. Users can set the key length according to different situations and flexibly control the encryption and decryption speed. 6) Key file export. Export the key file as a file to ensure the security of the transfer or stored procedure. 7) Key file import. Read the exported key file and identify it. It can identify the key file exported by the software and check the content to ensure the security of the imported file. 8) Key input. During decryption, you can manually enter the encryption key to ensure the security of decryption. 9) Document printing. The realization of the key file printing, can better save the private key. 10) File transfer. Encrypt files and images after the direct transmission, convenient and fast to better ensure security.

4 Implementation of key technologies of RSA image encryption 4.1 Large number storage of RSA Key RSA algorithm to generate a key requires two large prime numbers 21024 or greater to be safe [7]. But in programming languages, unsigned int stores up to two bytes, far less than the RSA security key. The author designs a linear array of elements to store large prime numbers and solve the problem of storing large prime numbers in programming. We define two unsigned integers z and n to control the number of storage units. Z is the number of units allocated space. If the size of a large prime exceeds the pre-defined size of the unsigned array,z will increase as the number grows. N indicates the number of units occupied by large primes. Each large prime number can be up to 232* the number of occupied cells to satisfy the various RSA operations.

4.2 Large Number Operation of RSA KeysAs the number of large primes generated exceeds 21024, the original data calculation method is no longer applicable. On the basis of large number storage, the method of inter-class derivation and association is adopted to realize large number operation. Define flex_unit to derive the vlong_value class to realize the new operation function, associate the original vlong_value class to the new class vLONG, and realize the operator overload in the new vLONG class. This kind of operation is the calculation of numbers according to a certain number system, multiplication, division and mod are also in accordance with the principle of vertical operation. The core code of addition operation is as follows: 4.3 Optimization of power module and multiplication module operation of large prime numbersPower mode operation is the most important calculation in RSA algorithm, which mainly determines the generation of the final public and private keys, and directly determines the performance of RSA algorithm. According to the properties of multiplicative modulus, power modulus operation is transformed into multiplicative modulus operation, and the realization idea is exponential constant bisection. The specific process is shown in Figure 3.

Modulo multiplication can improve the operation speed. Generally, for binary integer N above thousands, the operation speed of modulo calculation by ordinary division is very slow [9]. The author uses Montgomery algorithm to optimize the power multiplication operation to improve the encryption speed. The improved idea is to select the base Y(Y=2k) interprime with module M,m is odd and meets 2K-1 ≤m< 2K, and then select Y-1(0<Y-1<m) and M ‘(0< M’ <m), so that YY-1-mm ‘=1. Then choose any integer n(0≤ N <Ym), and finally realize the fast algorithm of modulus multiplication NY-1mod m to improve the encryption operation speed.

4.4 Automatic generation and optimization of prime numbers in the algorithmThe author uses the Eratosthenes screening method to optimize the prime number screening. The optimization strategy is to obtain the modulo of each small prime factor to obtain the corresponding position of the minimum multiple of A in the prime number search range in B [], and continue to move a position later until all the multiples of A in the search range are found. After a similar operation for all small prime factors, the position markers b[r] whose multiples are in the search range are all marked 0. The flow is shown in Figure 4.FIG. 3 Power mode conversion multiplication operation flow chartThe author uses Fermat’s little theorem to optimize the prime number test. The transformation strategy is to select an integer P, which is required to be interprime with A, and satisfy the relationship of PA-1mod If a=1, the input large integer A satisfies the relationship and may not be a prime number, P needs to be changed many times to complete the test. After the test, there is a high probability that the number will be prime.

Two, some source code

clc
clear all
close all

%% Initialization
Test=0; % for right key encryption
% Test=1; % for key sensitivity test
addpath functions
Images_Path='Images\Orignal\';
fname={'Lena'.'baboon'.'cameraman'.'panda'.'contact_lens'.'checkerboard'.'Black1'.'Onion'.'Football'}; % filename
ext='.jpg';
fid=1; % file ID 1 for lena
IS =256; % Image size
Data=imread(strcat(Images_Path,fname{fid},ext));
if (size(Data,3) = =3)
    Data=rgb2gray(Data);
end
Data=imresize(Data,[IS IS]); % Image Size

[row,col]=size(Data);
[Data,padding]=Scalling(Data,8);
Data_binary=convert2bin(Data);

hex_key = 'AAAAAAAAAAAAAAAA';
[bin_key] = Hex2Bin( hex_key );
[K1,K2,K3,K4,K5]=SF_Key_Gen(bin_key);

orignal_msg=[];
encrypt_msg=[];
decrypt_msg=[];

%% Encryption Process
for kk=1:2
  for i=1:size(Data_binary,1)
    orignal=Data_binary(i,:);
    tic
    [cipher]=SF_Encrypt(orignal,K1,K2,K3,K4,K5);
    encryption_time(i)=toc;
    tK1=[K1(1:8),orignal(1:8)]; tK2=orignal(9:24); tK3=orignal(25:40); tK4=orignal(41:56); tK5=[orignal(57:64),K5(9:16)]; K1=tK1; K2=tK2; K3=tK3; K4=tK4; K5=tK5; encrypt_msg(:,i)=Binary2Dec(cipher); cipher_data(i,:)=double(cipher);
    if(kk<2)
    Data_binary(i,:)=cipher_data(i,:);
    end
  end
function O = P(I)
if(I==logical([0 0 0 0]))
        O = logical([0 0 1 1]); end
if(I==logical([0 0 0 1]))
        O = logical([1 1 1 1]); end
if(I==logical([0 0 1 0]))
        O = logical([1 1 1 0]); end
if(I==logical([0 0 1 1]))
        O = logical([0 0 0 0]); end
if(I==logical([0 1 0 0]))
        O = logical([0 1 0 1]); end
if(I==logical([0 1 0 1]))
        O = logical([0 1 0 0]); end
if(I==logical([0 1 1 0]))
        O = logical([1 0 1 1]); end  
if(I==logical([0 1 1 1]))
        O = logical([1 1 0 0]); end  
if(I==logical([1 0 0 0]))
        O = logical([1 1 0 1]); end  
if(I==logical([1 0 0 1]))
        O = logical([1 0 1 0]); end
if(I==logical([1 0 1 0]))
        O = logical([1 0 0 1]); end  
if(I==logical([1 0 1 1]))
        O = logical([0 1 1 0]); end   
if(I==logical([1 1 0 0]))
        O = logical([0 1 1 1]); end  
if(I==logical([1 1 0 1]))
        O = logical([1 0 0 0]); end  
if(I==logical([1 1 1 0]))
        O = logical([0 0 1 0]); end  
if(I==logical([1 1 1 1]))
        O = logical([0 0 0 1]); end  
end
function O = Q(I)
if(I==logical([0 0 0 0]))
        O = logical([1 0 0 1]); end
if(I==logical([0 0 0 1]))
        O = logical([1 1 1 0]); end
if(I==logical([0 0 1 0]))
        O = logical([0 1 0 1]); end
if(I==logical([0 0 1 1]))
        O = logical([0 1 1 0]); end
if(I==logical([0 1 0 0]))
        O = logical([1 0 1 0]); end
if(I==logical([0 1 0 1]))
        O = logical([0 0 1 0]); end
if(I==logical([0 1 1 0]))
        O = logical([0 0 1 1]); end
if(I==logical([0 1 1 1]))
        O = logical([1 1 0 0]); end
if(I==logical([1 0 0 0]))
        O = logical([1 1 1 1]); end
if(I==logical([1 0 0 1]))
        O = logical([0 0 0 0]); end
if(I==logical([1 0 1 0]))
        O = logical([0 1 0 0]); end
if(I==logical([1 0 1 1]))
        O = logical([1 1 0 1]); end
if(I==logical([1 1 0 0]))
        O = logical([0 1 1 1]); end
if(I==logical([1 1 0 1]))
        O = logical([1 0 1 1]); end
if(I==logical([1 1 1 0]))
        O = logical([0 0 0 1]); end
if(I==logical([1 1 1 1]))
        O = logical([1 0 0 0]); end
end
Copy the code

3. Operation results

Matlab version and references

1 matlab version 2014A

2 Reference [1] CAI Limei. MATLAB Image Processing — Theory, Algorithm and Case Analysis [M]. Tsinghua University Press, 2020. [2] Yang Dan, ZHAO Haibin, LONG Zhe. Examples of MATLAB Image Processing In detail [M]. Tsinghua University Press, 2013. [3] Zhou Pin. MATLAB Image Processing and Graphical User Interface Design [M]. Tsinghua University Press, 2013. [4] LIU Chenglong. Proficient in MATLAB Image Processing [M]. Tsinghua University Press, 2015.