Main Content

encryptingURL

R2026b

Create URL to write or read encrypted file

Since R2026b

    Description

    url = encryptingURL(filepath,passwordID) returns a URL to the file that filepath specifies. Use this URL in a MATLAB® I/O function to write or read AES-256-GCM encrypted data with the secret specified by passwordID.

    Examples

    collapse all

    Write to an encrypted log file using the secret MyApp.MyPassword.

    At the MATLAB command prompt, create the secret using the setSecret function.

    setSecret("MyApp.MyPassword")

    Use the Secret Prompt dialog box to set the password value and add the secret to your MATLAB vault.

    Create an encrypting URL to the log file. When you write text to the file using the generated URL, the write operation uses encrypted data. You must set the password value before calling encryptingURL.

    % Write and read encrypted text.
    logURL = encryptingURL(fullfile(pwd,"log.txt"), "MyApp.MyPassword");
    fid = fopen(logURL, "w");
    fwrite(fid, "This is a test log.");
    fclose(fid);
    logText = fileread(logURL)

    Write and read an encrypted JPG image file using the secret MyApp.MyPassword.

    At the MATLAB command prompt, create the secret using the setSecret function.

    setSecret("MyApp.MyPassword")

    Use the Secret Prompt dialog box to set the password value and add the secret to your MATLAB vault.

    Create image data using imread.

    imageData = imread(which("peppers.png"));

    Write the image data to a new encrypted image file using imwrite and the encrypting URL. You can also specify the password as a secretID object.

    imageURL = encryptingURL(fullfile(pwd,"peppers.png"), secretID("MyApp.MyPassword"));
    imwrite(imageData, imageURL, "jpg");
    imshow(imread(imageURL));

    Input Arguments

    collapse all

    File path, specified as a string scalar or character vector. The path can point to a local or a remote location. The path must be an absolute path or a relative path to an existing file on the MATLAB path.

    If you specify an absolute path to a file that does not exist, write operations using the encrypted file path create the file.

    Example: filepath="D:\Work\myapp.log"

    Data Types: char | string

    Secret name of the password used to generate the encryption key, specified as a string scalar, character vector, or secretID object. The secret value must be at least 8 characters.

    Example: passwordID="mypassword"

    Output Arguments

    collapse all

    File path URL, which you can use in MATLAB I/O functions to write or read encrypted data.

    Example: "aes-gcm+file:///D:/test/peppers.png?passwordID=MyApp.MyPassword"

    Tips

    • Some MATLAB I/O functions, such as imread and imwrite, create unencrypted temporary files that can be exposed during runtime. Functions that do not create temporary files include:

      • load, save

      • fopen, fread, fwrite, fclose

      • fileread

      • audioread, audiowrite

      • parquetread, parquetwrite

      • xmlread, xmlwrite

    • If the secret value changes after encryptingURL is called, call encryptingURL again to update the encryption key.

    • The syntax dir(encryptingURL(...)) is not supported.

    Version History

    Introduced in R2026b