Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 9.54 KB | None | 0 0
  1. %% First step: Necessary things
  2. close all;
  3. clear all;
  4. clc;
  5.  
  6.  
  7. %% RSA encryption
  8.  
  9. %% Encryption & Decryption Key
  10. e = 23;
  11. d = 47;
  12. n = 143;
  13.                 %%step: Taking input
  14.                         %(1)---------------Input as random number----------------------
  15.                         %noOfSymbol = 1000;
  16.                         %symbols = randi(1,noOfSymbol,256);
  17.                        
  18.                         %(2)---------------Input as Text----------------------
  19.                         temp = 'Enter your input: ';
  20.                         text=input(temp,'s');
  21.                         text2 = text
  22.                         text3 = text
  23.                         symbols = double(text);
  24.                        
  25.                         %% RSA Encryption
  26.                         b = symbols;
  27.                         for i = 1:1:(e - 1);
  28.                             symbols = symbols .* b;
  29.                             symbols = mod(symbols, n);
  30.                         end
  31.                 %% step: Converting decimal to binary                
  32.                 symbolsToBitMapping = de2bi(symbols,8,'left-msb');
  33.                 totalBits = numel(symbolsToBitMapping);
  34.                 inReshapedBits = reshape(symbolsToBitMapping,1,totalBits);
  35.  
  36.                 %% step: Zero Padding
  37.                 M=4;
  38.                 bps = log2(M);
  39.                 remainder = rem(totalBits,bps);
  40.                 if(remainder==0)
  41.                    userDataPadding = inReshapedBits;
  42.                 else
  43.                    paddedData = zeros(1, bps-remainder);
  44.                    userDataPadding = [inReshapedBits,paddedData];
  45.                 end
  46.                                % numel(reshapedBits)
  47.                                % numel(userDataPadding)
  48.  
  49.                 %% step: Modulation
  50.                 bitToSymbolMapping = reshape(userDataPadding,numel(userDataPadding)/bps,bps);
  51.                 decimalSymbol = bi2de(bitToSymbolMapping,'left-msb');
  52.                 modulatedSymbol = pskmod(decimalSymbol,M);
  53.  
  54.       %% step: Channel
  55.       SN = [];
  56.       BR = [];
  57.       for snr = 0:12
  58.           SN = [SN,snr]
  59.                 noisySymbols = awgn(modulatedSymbol,snr,'measured');
  60.                 %% Seventh step: Demodulation
  61.                 demodulatedSymbols = pskdemod(noisySymbols,M);
  62.                 decimalToBinary = de2bi(demodulatedSymbols,'left-msb');
  63.  
  64.                 %% Eight step: Remove Zero Padding
  65.                 outReshapeBits = reshape(decimalToBinary,1,numel(decimalToBinary));
  66.                 outReshapeBits = outReshapeBits(1:totalBits);                
  67.                                 %numel(outReshapeBits)
  68.                                
  69.                 %% Ninth step: Calculate BER
  70.                 [numberOfError, BER] = biterr(inReshapedBits,outReshapeBits);
  71.           BR = [BR, BER];
  72.      end
  73.      
  74.                 %% step: Original symbols/text
  75.                 outReshapeBits = reshape(outReshapeBits,numel(outReshapeBits)/8,8);
  76.                
  77.                         decimalSymbols = bi2de(outReshapeBits,'left-msb');
  78.                         c = decimalSymbols
  79.                        
  80.                         %% Decryption
  81.                         for i = 1:1:(d-1);
  82.                             decimalSymbols = decimalSymbols .* c;
  83.                             decimalSymbols = mod(decimalSymbols, n);
  84.                         end
  85.                         text = char(decimalSymbols)';
  86.                        
  87.                        
  88.                          
  89.                        
  90.                      %% No Encryption  
  91.                        
  92.                        
  93.                         %% Second step: Taking input
  94.                         %(1)---------------Input as random number----------------------
  95.                         %noOfSymbol = 1000;
  96.                         %symbols = randi(1,noOfSymbol,256);
  97.                        
  98.                         %(2)---------------Input as Text----------------------
  99.                         symbols = double(text2);
  100.                        
  101.                        
  102.                 %% Third step: Converting decimal to binary                
  103.                 symbolsToBitMapping = de2bi(symbols,8,'left-msb');
  104.                 totalBits = numel(symbolsToBitMapping);
  105.                 inReshapedBits = reshape(symbolsToBitMapping,1,totalBits);
  106.  
  107.                 %% Fourth step: Zero Padding
  108.                 M=4;
  109.                 bps = log2(M);
  110.                 remainder = rem(totalBits,bps);
  111.                 if(remainder==0)
  112.                    userDataPadding = inReshapedBits;
  113.                 else
  114.                    paddedData = zeros(1, bps-remainder);
  115.                    userDataPadding = [inReshapedBits,paddedData];
  116.                 end
  117.                                % numel(reshapedBits)
  118.                                % numel(userDataPadding)
  119.  
  120.                 %% Fifth step: Modulation
  121.                 bitToSymbolMapping = reshape(userDataPadding,numel(userDataPadding)/bps,bps);
  122.                 decimalSymbol = bi2de(bitToSymbolMapping,'left-msb');
  123.                 modulatedSymbol = pskmod(decimalSymbol,M);
  124.  
  125.       %% Sixth step: Channel
  126.       SN1 = [];
  127.       BR1 = [];
  128.       for snr = 0:12
  129.           SN1 = [SN1,snr]
  130.                 noisySymbols = awgn(modulatedSymbol,snr,'measured');
  131.                 %% Seventh step: Demodulation
  132.                 demodulatedSymbols = pskdemod(noisySymbols,M);
  133.                 decimalToBinary = de2bi(demodulatedSymbols,'left-msb');
  134.  
  135.                 %% Eight step: Remove Zero Padding
  136.                 outReshapeBits = reshape(decimalToBinary,1,numel(decimalToBinary));
  137.                 outReshapeBits = outReshapeBits(1:totalBits);                
  138.                                 %numel(outReshapeBits)
  139.                                
  140.                 %% Ninth step: Calculate BER
  141.                 [numberOfError, BER1] = biterr(inReshapedBits,outReshapeBits);
  142.           BR1 = [BR1, BER1];
  143.      end
  144.      
  145.                 %% Tenth step: Original symbols/text
  146.                 outReshapeBits = reshape(outReshapeBits,numel(outReshapeBits)/8,8);
  147.                
  148.                         decimalSymbols = bi2de(outReshapeBits,'left-msb');
  149.                         c = decimalSymbols
  150.                         text = char(decimalSymbols)';
  151.                        
  152.                        
  153.                        
  154.                        
  155.                        
  156.                        
  157.                        
  158.                        
  159.                       %% Ceaser Cipher Encrypion
  160.                        
  161.                        
  162.                        
  163.                        
  164.                          %% Second step: Taking input
  165.                         %(1)---------------Input as random number----------------------
  166.                         %noOfSymbol = 1000;
  167.                         %symbols = randi(1,noOfSymbol,256);
  168.                        
  169.                         %(2)---------------Input as Text----------------------
  170.                         symbols = double(text3);
  171.                        
  172.                        
  173.                 %% Third step: Converting decimal to binary                
  174.                 symbolsToBitMapping = de2bi(symbols,8,'left-msb');
  175.                 totalBits = numel(symbolsToBitMapping);
  176.                 inReshapedBits = reshape(symbolsToBitMapping,1,totalBits);
  177.  
  178.                 %% Fourth step: Zero Padding
  179.                 M=4;
  180.                 bps = log2(M);
  181.                 remainder = rem(totalBits,bps);
  182.                 if(remainder==0)
  183.                    userDataPadding = inReshapedBits;
  184.                 else
  185.                    paddedData = zeros(1, bps-remainder);
  186.                    userDataPadding = [inReshapedBits,paddedData];
  187.                 end
  188.                                % numel(reshapedBits)
  189.                                % numel(userDataPadding)
  190.  
  191.                 %% Fifth step: Modulation
  192.                 bitToSymbolMapping = reshape(userDataPadding,numel(userDataPadding)/bps,bps);
  193.                 decimalSymbol = bi2de(bitToSymbolMapping,'left-msb');
  194.                 modulatedSymbol = pskmod(decimalSymbol,M);
  195.  
  196.       %% Sixth step: Channel
  197.       SN3 = [];
  198.       BR3 = [];
  199.       for snr = 0:12
  200.           SN3 = [SN3,snr]
  201.                 noisySymbols = awgn(modulatedSymbol,snr,'measured');
  202.                 %% Seventh step: Demodulation
  203.                 demodulatedSymbols = pskdemod(noisySymbols,M);
  204.                 decimalToBinary = de2bi(demodulatedSymbols,'left-msb');
  205.  
  206.                 %% Eight step: Remove Zero Padding
  207.                 outReshapeBits = reshape(decimalToBinary,1,numel(decimalToBinary));
  208.                 outReshapeBits = outReshapeBits(1:totalBits);                
  209.                                 %numel(outReshapeBits)
  210.                                
  211.                 %% Ninth step: Calculate BER
  212.                 [numberOfError, BER3] = biterr(inReshapedBits,outReshapeBits);
  213.           BR3 = [BR3, BER3];
  214.      end
  215.      
  216.                 %% Tenth step: Original symbols/text
  217.                 outReshapeBits = reshape(outReshapeBits,numel(outReshapeBits)/8,8);
  218.                
  219.                         decimalSymbols = bi2de(outReshapeBits,'left-msb');
  220.                         c = decimalSymbols
  221.                         text = char(decimalSymbols)';
  222.                        
  223.      
  224.     %% SNR vs BER plot
  225.     plotHandle=plot(SN1,BR1,'k-o');
  226.     set(plotHandle,'LineWidth',1.5);
  227.     hold on;
  228.     plotHandle=plot(SN,BR,'r-o');
  229.     set(plotHandle,'LineWidth',1.5);
  230.     plotHandle=plot(SN3,BR3,'g-o');
  231.     set(plotHandle,'LineWidth',1.5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement