menu driven application that takes file (must function with binary or text) as input, stores via chosen cryptographic method, then retrieves securely if appropriate password/key is made available. in matlab

To create a menu-driven application in MATLAB that can encrypt and decrypt files using a chosen cryptographic method, and provide security by asking for a password/key, we can use the following code:

main.m
while true
    % Display menu options
    fprintf('Select an option:\n');
    fprintf('1. Encrypt file\n');
    fprintf('2. Decrypt file\n');
    fprintf('3. Exit\n');
    
    % Get user input for menu selection
    option = input('Enter option number: ');
    
    if option == 1
        % Get filename to encrypt
        filename = input('Enter filename to encrypt (including extension): ', 's');
        
        % Get encryption method
        fprintf('Select an encryption method:\n');
        fprintf('1. AES\n');
        fprintf('2. DES\n');
        method = input('Enter method number: ');

        % Get encryption key
        key = input('Enter encryption key: ', 's');
        
        % Read file contents
        fileID = fopen(filename, 'r');
        data = fread(fileID, '*uint8');
        fclose(fileID);

        % Encrypt data using chosen method
        switch method
            case 1  % AES
                data_encrypted = AESencrypt(data, key);
            case 2  % DES
                data_encrypted = DESencrypt(data, key);
            otherwise
                fprintf('Invalid method selected.\n');
                continue
        end
        
        % Write encrypted data to file
        fileID = fopen([filename, '.enc'], 'w');
        fwrite(fileID, data_encrypted);
        fclose(fileID);
        
        % Display success message
        fprintf('File has been encrypted and saved as %s.\n', [filename, '.enc']);

    elseif option == 2
        % Get filename to decrypt
        filename = input('Enter filename to decrypt (including extension): ', 's');
        
        % Get decryption method
        fprintf('Select a decryption method:\n');
        fprintf('1. AES\n');
        fprintf('2. DES\n');
        method = input('Enter method number: ');

        % Get decryption key
        key = input('Enter decryption key: ', 's');
        
        % Read file contents
        fileID = fopen(filename, 'r');
        data_encrypted = fread(fileID, '*uint8');
        fclose(fileID);

        % Decrypt data using chosen method
        switch method
            case 1  % AES
                data_decrypted = AESdecrypt(data_encrypted, key);
            case 2  % DES
                data_decrypted = DESdecrypt(data_encrypted, key);
            otherwise
                fprintf('Invalid method selected.\n');
                continue
        end
        
        % Write decrypted data to file
        fileID = fopen([filename(1:end-4), '_decrypted', filename(end-3:end)], 'w');
        fwrite(fileID, data_decrypted);
        fclose(fileID);
        
        % Display success message
        fprintf('File has been decrypted and saved as %s.\n', [filename(1:end-4), '_decrypted', filename(end-3:end)]);

    elseif option == 3
        % Exit the program
        fprintf('Exiting program.\n');
        break
        
    else
        % Invalid input
        fprintf('Invalid option selected.\n');
    end
    
end
2959 chars
96 lines

This code uses fwrite and fread functions for file I/O operations. You can use AESencrypt, AESdecrypt, DESencrypt, and DESdecrypt functions, which can be found in various MATLAB toolboxes or online, for encryption and decryption of data using AES and DES encryption methods.

Note that this code does not provide very robust security, as the password/key is entered as plain text and the encrypted file is saved in the same directory as the original file. For stronger security, you should use more advanced encryption methods and store the encrypted file in a more secure location.

related categories

gistlibby LogSnag