My MATLAB code is showing blank plots instead of encrypted and decrypted images. Why? [Help]

8 ビュー (過去 30 日間)
clc, clear all, close all
% Load the image
originalImage = imread('input_image.jpg');
% Convert the image to grayscale if it's not already grayscale
if size(originalImage, 3) > 1
originalImage = rgb2gray(originalImage);
end
% Display the original image
figure;
subplot(1, 2, 1);
imshow(originalImage);
title('Original Image');
% Reshape the image into a row vector
imageVector = reshape(originalImage, 1, []);
% Define the S-Box (replace with your own S-Box values)
%SBox = [10 5 2 7 14 11 12 9 3 15 0 6 4 13 1 8];
%Assumed Sbox
SBox = [125 204 103 23 177 74 59 153 214 73 31 57 149 48 54 56 70 226 165 67 178 184 49 100 93 174 101 150 163 42 44 240 164 168 147 126 228 139 6 219 1 5 60 51 9 76 182 130 183 194 197 213 118 241 16 124 129 189 136 242 105 232 188 234 43 152 252 97 245 99 144 225 53 236 215 200 187 146 78 71 61 94 166 77 79 161 62 98 171 253 137 238 230 68 8 109 210 243 113 167 179 83 75 95 46 32 4 212 209 132 14 135 244 148 227 186 33 88 207 248 141 138 26 231 47 20 2 158 96 122 134 218 202 13 111 220 65 117 107 151 81 91 69 116 86 206 64 27 89 66 80 201 72 185 119 106 37 237 38 192 195 191 29 35 145 198 85 193 25 211 34 246 173 55 131 19 58 108 169 63 239 175 120 3 12 233 205 155 41 84 123 229 255 110 254 222 112 40 235 90 208 102 172 30 82 128 28 250 11 249 142 143 18 24 203 45 87 160 127 15 10 121 196 39 176 190 199 36 21 115 216 104 7 180 114 17 224 52 133 157 162 247 170 50 0 223 140 156 22 217 92 159 221 154 251 181];
% Encrypt the image by applying the S-Box
encryptedVector = SBox(imageVector + 1);
% Reshape the encrypted vector back into an image
encryptedImage = reshape(encryptedVector, size(originalImage));
% Display the encrypted image
subplot(1, 2, 2);
imshow(encryptedImage);
title('Encrypted Image');
% Decrypt the image by applying the inverse of the S-Box
decryptedVector = zeros(size(encryptedVector));
for i = 1:length(encryptedVector)
decryptedVector(i) = find(SBox == encryptedVector(i)) - 1;
end
% Reshape the decrypted vector back into an image
decryptedImage = reshape(decryptedVector, size(originalImage));
% Display the decrypted image
figure;
imshow(decryptedImage);
title('Decrypted Image');

採用された回答

Les Beckham
Les Beckham 2023 年 5 月 10 日
You need to convert encryptedImage and decryptedImage back to uint8.
This line is converting to double because SBox is double.
encryptedVector = SBox(imageVector + 1);
% Load the image
originalImage = imread('input_image.jpg');
% Convert the image to grayscale if it's not already grayscale
if size(originalImage, 3) > 1
originalImage = rgb2gray(originalImage);
end
% Display the original image
figure;
subplot(1, 2, 1);
imshow(originalImage);
title('Original Image');
% Reshape the image into a row vector
imageVector = reshape(originalImage, 1, []);
% Define the S-Box (replace with your own S-Box values)
%SBox = [10 5 2 7 14 11 12 9 3 15 0 6 4 13 1 8];
%Assumed Sbox
SBox = [125 204 103 23 177 74 59 153 214 73 31 57 149 48 54 56 70 226 165 67 178 184 49 100 93 174 101 150 163 42 44 240 164 168 147 126 228 139 6 219 1 5 60 51 9 76 182 130 183 194 197 213 118 241 16 124 129 189 136 242 105 232 188 234 43 152 252 97 245 99 144 225 53 236 215 200 187 146 78 71 61 94 166 77 79 161 62 98 171 253 137 238 230 68 8 109 210 243 113 167 179 83 75 95 46 32 4 212 209 132 14 135 244 148 227 186 33 88 207 248 141 138 26 231 47 20 2 158 96 122 134 218 202 13 111 220 65 117 107 151 81 91 69 116 86 206 64 27 89 66 80 201 72 185 119 106 37 237 38 192 195 191 29 35 145 198 85 193 25 211 34 246 173 55 131 19 58 108 169 63 239 175 120 3 12 233 205 155 41 84 123 229 255 110 254 222 112 40 235 90 208 102 172 30 82 128 28 250 11 249 142 143 18 24 203 45 87 160 127 15 10 121 196 39 176 190 199 36 21 115 216 104 7 180 114 17 224 52 133 157 162 247 170 50 0 223 140 156 22 217 92 159 221 154 251 181];
% Encrypt the image by applying the S-Box
encryptedVector = SBox(imageVector + 1);
% Reshape the encrypted vector back into an image
encryptedImage = uint8(reshape(encryptedVector, size(originalImage)));
% Display the encrypted image
subplot(1, 2, 2);
imshow(encryptedImage);
title('Encrypted Image');
% Decrypt the image by applying the inverse of the S-Box
decryptedVector = zeros(size(encryptedVector));
for i = 1:length(encryptedVector)
decryptedVector(i) = find(SBox == encryptedVector(i)) - 1;
end
% Reshape the decrypted vector back into an image
decryptedImage = uint8(reshape(decryptedVector, size(originalImage)));
% Display the decrypted image
figure;
imshow(decryptedImage);
title('Decrypted Image');
whos
Name Size Bytes Class Attributes SBox 1x256 2048 double ans 1x40 80 char cmdout 1x33 66 char decryptedImage 256x256 65536 uint8 decryptedVector 1x65536 524288 double encryptedImage 256x256 65536 uint8 encryptedVector 1x65536 524288 double i 1x1 8 double imageVector 1x65536 65536 uint8 originalImage 256x256 65536 uint8

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by