Platforms to show: All Mac Windows Linux Cross-Platform

Back to CCCryptorMBS class.

CCCryptorMBS.Crypt(Operation as Integer, Algorithm as Integer, Options as Integer, key as Ptr, KeyLength as UInt64, IV as Ptr, DataIn as Ptr, DataInLength as UInt64, DataOut as Ptr, DataOutAvailable as UInt64, byref DataOutMoved as UInt64) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Encryption and Hash MBS MacOSX Plugin 11.3 ✅ Yes ❌ No ❌ No ✅ Yes All
Stateless, one-shot encrypt or decrypt operation.
Example
dim KeyString as string = "Hello12312345678"
dim InputString as string = "Hello World!1234"

dim InputMemory as MemoryBlock = InputString
dim KeyMemory as MemoryBlock = KeyString
dim IV as ptr = nil // default to zeros
dim OutputMemory as new MemoryBlock(100)
dim Size as UInt64 = 0

dim e as Integer = CCCryptorMBS.Crypt(CCCryptorMBS.kCCEncrypt, CCCryptorMBS.kCCAlgorithmAES128, 0, KeyMemory, KeyMemory.size, IV, InputMemory, InputMemory.size, OutputMemory, OutputMemory.size, size)

if e = CCCryptorMBS.kCCSuccess then
dim result as string = OutputMemory.StringValue(0, size)
MsgBox EncodingToHexMBS(result)
else
MsgBox "Error: "+str(e)
end if

This basically performs a sequence of Constructor, Update, Final and Destructor.

Parameters:
OperationDefines the basic operation: kCCEncrypt or kCCDecrypt.
AlgorithmDefines the encryption algorithm.
OptionsA word of flags defining options. See kCCOption constants.
KeyRaw key material, length keyLength bytes.
keyLengthLength of key material. Must be appropriate for the select algorithm. Some algorithms may provide for varying key lengths.
IVInitialization vector, optional. Used for Cipher Block Chaining (CBC) mode. If present, must be the same length as the selected algorithm's block size. If CBC mode is selected (by the absence of any mode bits in the options flags) and no IV is present, a nil (all zeroes) IV will be used. This is ignored if ECB mode is used or if a stream cipher algorithm is selected.
dataInData to encrypt or decrypt, length dataInLength bytes.
dataInLengthLength of data to encrypt or decrypt.
dataOutResult is written here. Allocated by caller. Encryption and decryption can be performed "in-place", with the same buffer used for input and output.
dataOutAvailableThe size of the dataOut buffer in bytes.
dataOutMovedOn successful return, the number of bytes written to dataOut. If kCCBufferTooSmall is returned as a result of insufficient buffer space being provided, the required buffer space is returned here.

Possible errors in lasterror:
kCCBufferTooSmall indicates insufficent space in the dataOut buffer. In this case, the dataOutMoved parameter will indicate the size of the buffer needed to complete the operation. The operation can be retried with minimal runtime penalty.
kCCAlignmentError indicates that dataInLength was not properly aligned. This can only be returned for block ciphers, and then only when decrypting or when encrypting with block with padding disabled.
kCCDecodeErrorIndicates improperly formatted ciphertext or a "wrong key" error; occurs only during decrypt operations.

See also:

CCCryptorMBS.Crypt(Operation as Integer, Algorithm as Integer, Options as Integer, key as string, IV as Ptr, DataIn as string, byref DataOut as string) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Encryption and Hash MBS MacOSX Plugin 11.3 ✅ Yes ❌ No ❌ No ✅ Yes All
Stateless, one-shot encrypt or decrypt operation.
Example
dim KeyString as string = "Hello12312345678"
dim InputString as string = "Hello World!1234"
dim IV as ptr = nil // default to zeros
dim Output as string = ""

dim e as Integer = CCCryptorMBS.Crypt(CCCryptorMBS.kCCEncrypt, CCCryptorMBS.kCCAlgorithmAES128, 0, KeyString, IV, InputString, Output)

if e = CCCryptorMBS.kCCSuccess then
MsgBox EncodingToHexMBS(Output)
else
MsgBox "Error: "+str(e)
end if

This basically performs a sequence of Constructor, Update, Final and Destructor. This is the convenience function for Xojo with using strings instead of data pointers.

Parameters:
OperationDefines the basic operation: kCCEncrypt or kCCDecrypt.
AlgorithmDefines the encryption algorithm.
OptionsA word of flags defining options. See kCCOption constants.
KeyRaw Key.
IVInitialization vector, optional. Used for Cipher Block Chaining (CBC) mode. If present, must be the same length as the selected algorithm's block size. If CBC mode is selected (by the absence of any mode bits in the options flags) and no IV is present, a nil (all zeroes) IV will be used. This is ignored if ECB mode is used or if a stream cipher algorithm is selected.
dataInData to encrypt or decrypt.
dataOutResult is written here.

Possible errors in lasterror:
kCCBufferTooSmall indicates insufficent space in the dataOut buffer. In this case, the dataOutMoved parameter will indicate the size of the buffer needed to complete the operation. The operation can be retried with minimal runtime penalty.
kCCAlignmentError indicates that dataInLength was not properly aligned. This can only be returned for block ciphers, and then only when decrypting or when encrypting with block with padding disabled.
kCCDecodeErrorIndicates improperly formatted ciphertext or a "wrong key" error; occurs only during decrypt operations.

See also:

CCCryptorMBS.RandomGenerateBytes(data as MemoryBlock) as integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Encryption and Hash MBS MacOSX Plugin 21.4 ✅ Yes ❌ No ❌ No ✅ Yes All
Return random bytes in a buffer allocated by the caller.
Example
Dim m As New MemoryBlock(16)

Dim r As Integer = CCCryptorMBS.RandomGenerateBytes(m)

If r = CCCryptorMBS.kCCSuccess Then
MessageBox EncodeHex(m)
Else
MessageBox "Error " + Str(r)
End If

The PRNG returns cryptographically strong random bits suitable for use as cryptographic keys, IVs, nonces etc.

Return kCCSuccess on success.

The items on this page are in the following plugins: MBS MacOSX Plugin.


The biggest plugin in space...