EncryptData Method  

Encrypt the contents of the specified string or byte array.


object.EncryptData( InputData, OutputData [, Password ])


A string or byte array that contains the data to be encrypted.
A string or byte array which will contain the encrypted data when the method returns. This parameter must be passed by reference. When specifying a Byte array, you must ensure the buffer is large enough to contain all of the decrypted data.
An optional parameter that specifies the password that was used to encrypt the data. If this parameter is omitted, the value of the Password property will be used.

Return Value

This method returns the number of bytes of encrypted data copied into the output buffer. If the input buffer is a zero-length string or an empty array, the method will return zero. If an error occurs, the method will return -1. Check the value of the LastError property to determine the specific error that has occurred.


The EncryptData method encrypts the contents of a string or byte array using a 256-bit AES (Advanced Encryption Standard) algorithm and stores the encrypted data in the specified output file. The password (or passphrase) provided by the caller is used to generate a SHA-256 hash value which is used as part of the encryption process. The identical password is required to decrypt the data using the DecryptData method.

Although it is not required for your application to use a password to encrypt the data, it is recommended. If no password is specified, any other application that uses this control will be able to decrypt the data. Passwords are case-sensitive and must match exactly, including the use of any spaces.


If the input buffer is a String type, the output buffer must also be a String. The text will be automatically encoded as UTF-8 and the encrypted data will be returned as a base64 encoded string. If the input buffer is a Byte array, the output buffer must also be a Byte array. Never attempt to encrypt or decrypt binary data using String variables. You must always use a Byte array for binary data.

If you wish to encrypt and decrypt the contents of a file, use the EncryptFile and DecryptFile methods.

This method uses the Microsoft CryptoAPI and the RSA AES cryptographic provider. This provider may not be available in some languages, countries or regions. The availability of this provider may also be constrained by cryptography export restrictions imposed by the United States or other countries. If the required cryptographic provider is not available, the method will fail.

See Also

CompressFile Method, DecryptData Method, ExpandData Method, ExpandFile Method