Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% First step: Necessary things
- close all;
- clear all;
- clc;
- %% RSA encryption
- %% Encryption & Decryption Key
- e = 23;
- d = 47;
- n = 143;
- %%step: Taking input
- %(1)---------------Input as random number----------------------
- %noOfSymbol = 1000;
- %symbols = randi(1,noOfSymbol,256);
- %(2)---------------Input as Text----------------------
- temp = 'Enter your input: ';
- text=input(temp,'s');
- text2 = text
- text3 = text
- symbols = double(text);
- %% RSA Encryption
- b = symbols;
- for i = 1:1:(e - 1);
- symbols = symbols .* b;
- symbols = mod(symbols, n);
- end
- %% step: Converting decimal to binary
- symbolsToBitMapping = de2bi(symbols,8,'left-msb');
- totalBits = numel(symbolsToBitMapping);
- inReshapedBits = reshape(symbolsToBitMapping,1,totalBits);
- %% step: Zero Padding
- M=4;
- bps = log2(M);
- remainder = rem(totalBits,bps);
- if(remainder==0)
- userDataPadding = inReshapedBits;
- else
- paddedData = zeros(1, bps-remainder);
- userDataPadding = [inReshapedBits,paddedData];
- end
- % numel(reshapedBits)
- % numel(userDataPadding)
- %% step: Modulation
- bitToSymbolMapping = reshape(userDataPadding,numel(userDataPadding)/bps,bps);
- decimalSymbol = bi2de(bitToSymbolMapping,'left-msb');
- modulatedSymbol = pskmod(decimalSymbol,M);
- %% step: Channel
- SN = [];
- BR = [];
- for snr = 0:12
- SN = [SN,snr]
- noisySymbols = awgn(modulatedSymbol,snr,'measured');
- %% Seventh step: Demodulation
- demodulatedSymbols = pskdemod(noisySymbols,M);
- decimalToBinary = de2bi(demodulatedSymbols,'left-msb');
- %% Eight step: Remove Zero Padding
- outReshapeBits = reshape(decimalToBinary,1,numel(decimalToBinary));
- outReshapeBits = outReshapeBits(1:totalBits);
- %numel(outReshapeBits)
- %% Ninth step: Calculate BER
- [numberOfError, BER] = biterr(inReshapedBits,outReshapeBits);
- BR = [BR, BER];
- end
- %% step: Original symbols/text
- outReshapeBits = reshape(outReshapeBits,numel(outReshapeBits)/8,8);
- decimalSymbols = bi2de(outReshapeBits,'left-msb');
- c = decimalSymbols
- %% Decryption
- for i = 1:1:(d-1);
- decimalSymbols = decimalSymbols .* c;
- decimalSymbols = mod(decimalSymbols, n);
- end
- text = char(decimalSymbols)';
- %% No Encryption
- %% Second step: Taking input
- %(1)---------------Input as random number----------------------
- %noOfSymbol = 1000;
- %symbols = randi(1,noOfSymbol,256);
- %(2)---------------Input as Text----------------------
- symbols = double(text2);
- %% Third step: Converting decimal to binary
- symbolsToBitMapping = de2bi(symbols,8,'left-msb');
- totalBits = numel(symbolsToBitMapping);
- inReshapedBits = reshape(symbolsToBitMapping,1,totalBits);
- %% Fourth step: Zero Padding
- M=4;
- bps = log2(M);
- remainder = rem(totalBits,bps);
- if(remainder==0)
- userDataPadding = inReshapedBits;
- else
- paddedData = zeros(1, bps-remainder);
- userDataPadding = [inReshapedBits,paddedData];
- end
- % numel(reshapedBits)
- % numel(userDataPadding)
- %% Fifth step: Modulation
- bitToSymbolMapping = reshape(userDataPadding,numel(userDataPadding)/bps,bps);
- decimalSymbol = bi2de(bitToSymbolMapping,'left-msb');
- modulatedSymbol = pskmod(decimalSymbol,M);
- %% Sixth step: Channel
- SN1 = [];
- BR1 = [];
- for snr = 0:12
- SN1 = [SN1,snr]
- noisySymbols = awgn(modulatedSymbol,snr,'measured');
- %% Seventh step: Demodulation
- demodulatedSymbols = pskdemod(noisySymbols,M);
- decimalToBinary = de2bi(demodulatedSymbols,'left-msb');
- %% Eight step: Remove Zero Padding
- outReshapeBits = reshape(decimalToBinary,1,numel(decimalToBinary));
- outReshapeBits = outReshapeBits(1:totalBits);
- %numel(outReshapeBits)
- %% Ninth step: Calculate BER
- [numberOfError, BER1] = biterr(inReshapedBits,outReshapeBits);
- BR1 = [BR1, BER1];
- end
- %% Tenth step: Original symbols/text
- outReshapeBits = reshape(outReshapeBits,numel(outReshapeBits)/8,8);
- decimalSymbols = bi2de(outReshapeBits,'left-msb');
- c = decimalSymbols
- text = char(decimalSymbols)';
- %% Ceaser Cipher Encrypion
- %% Second step: Taking input
- %(1)---------------Input as random number----------------------
- %noOfSymbol = 1000;
- %symbols = randi(1,noOfSymbol,256);
- %(2)---------------Input as Text----------------------
- symbols = double(text3);
- %% Third step: Converting decimal to binary
- symbolsToBitMapping = de2bi(symbols,8,'left-msb');
- totalBits = numel(symbolsToBitMapping);
- inReshapedBits = reshape(symbolsToBitMapping,1,totalBits);
- %% Fourth step: Zero Padding
- M=4;
- bps = log2(M);
- remainder = rem(totalBits,bps);
- if(remainder==0)
- userDataPadding = inReshapedBits;
- else
- paddedData = zeros(1, bps-remainder);
- userDataPadding = [inReshapedBits,paddedData];
- end
- % numel(reshapedBits)
- % numel(userDataPadding)
- %% Fifth step: Modulation
- bitToSymbolMapping = reshape(userDataPadding,numel(userDataPadding)/bps,bps);
- decimalSymbol = bi2de(bitToSymbolMapping,'left-msb');
- modulatedSymbol = pskmod(decimalSymbol,M);
- %% Sixth step: Channel
- SN3 = [];
- BR3 = [];
- for snr = 0:12
- SN3 = [SN3,snr]
- noisySymbols = awgn(modulatedSymbol,snr,'measured');
- %% Seventh step: Demodulation
- demodulatedSymbols = pskdemod(noisySymbols,M);
- decimalToBinary = de2bi(demodulatedSymbols,'left-msb');
- %% Eight step: Remove Zero Padding
- outReshapeBits = reshape(decimalToBinary,1,numel(decimalToBinary));
- outReshapeBits = outReshapeBits(1:totalBits);
- %numel(outReshapeBits)
- %% Ninth step: Calculate BER
- [numberOfError, BER3] = biterr(inReshapedBits,outReshapeBits);
- BR3 = [BR3, BER3];
- end
- %% Tenth step: Original symbols/text
- outReshapeBits = reshape(outReshapeBits,numel(outReshapeBits)/8,8);
- decimalSymbols = bi2de(outReshapeBits,'left-msb');
- c = decimalSymbols
- text = char(decimalSymbols)';
- %% SNR vs BER plot
- plotHandle=plot(SN1,BR1,'k-o');
- set(plotHandle,'LineWidth',1.5);
- hold on;
- plotHandle=plot(SN,BR,'r-o');
- set(plotHandle,'LineWidth',1.5);
- plotHandle=plot(SN3,BR3,'g-o');
- set(plotHandle,'LineWidth',1.5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement