Module

Crypto


Description

Contains hashing methods for use with cryptography.

Methods

Name

Parameters

Returns

Shared

AESDecrypt

publicKey As String, data As MemoryBlock, blockMode As Crypto = BlockModes.CBC, initializationVector As MemoryBlock

MemoryBlock

AESEncrypt

publicKey As String, data As MemoryBlock, blockMode As Crypto = BlockModes.CBC, initializationVector As MemoryBlock

MemoryBlock

BERDecodePrivateKey

privateKey As String

MemoryBlock

BERDecodePublicKey

publicKey As String

MemoryBlock

BlowFishDecrypt

publicKey As String, data As MemoryBlock, blockMode As Crypto = BlockModes.CBC, initializationVector As MemoryBlock

MemoryBlock

BlowFishEncrypt

publicKey As String, data As MemoryBlock, blockMode As Crypto = BlockModes.CBC, initializationVector As MemoryBlock

MemoryBlock

DEREncodePrivateKey

privateKey As String

MemoryBlock

DEREncodePublicKey

publicKey As String

MemoryBlock

ED25519Sign

privateKey As String, data As MemoryBlock

MemoryBlock

GenerateRandomBytes

ByteCount As Integer

MemoryBlock

Hash

data As MemoryBlock, hashAlgorithm As Crypto.HashAlgorithms

MemoryBlock

HMAC

key As String, data As MemoryBlock, hashAlgorithm As Crypto.HashAlgorithms

MemoryBlock

MD5

data As MemoryBlock

MemoryBlock

PBKDF2

salt As String, data As MemoryBlock, iterations As Integer, desiredHashLength As Integer, hashAlgorithm As Crypto.HashAlgorithms

MemoryBlock

RSADecrypt

Data As MemoryBlock, PrivateKey As String

MemoryBlock

RSAEncrypt

Data As MemoryBlock, PublicKey As String

MemoryBlock

RSAGenerateKeyPair

Bits As Integer, ByRef PrivateKey As String, ByRef PublicKey As String

Boolean

RSASign

Data As MemoryBlock, PrivateKey As String, Optional mode As Crypto.RSASignModes = Crypto.RSASignModes.SHA1

MemoryBlock

RSAVerifyKey

Key As String

Boolean

RSAVerifySignature

Data As MemoryBlock, Signature As MemoryBlock, PublicKey As String

Boolean

SHA1

data As MemoryBlock

MemoryBlock

SHA2_256

data As MemoryBlock

MemoryBlock

SHA2_512

data As MemoryBlock

MemoryBlock

SHA3_256

data As MemoryBlock

MemoryBlock

SHA3_512

data As MemoryBlock

MemoryBlock

TwoFishDecrypt

publicKey As String, data As MemoryBlock, blockMode As Crypto.BlockModes = BlockModes.CBC, initializationVector As MemoryBlock

MemoryBlock

TwoFishEncrypt

publicKey As String, data As MemoryBlock, blockMode As Crypto.BlockModes = BlockModes.CBC, initializationVector As MemoryBlock

MemoryBlock

Enumerations

Crypto.BlockModes

BlockModes

The block modes for use with BlowFish encryption and decryption.

Enum

Description

CBC

An initialization vector-based encryption scheme, the mode is secure as a probabilistic encryption scheme, achieving indistinguishability from random bits, assuming a random initialization vector.

ECB

A blockcipher, the mode enciphers messages that are a multiple of n bits by separately enciphering each n-bit piece.

Crypto.HashAlgorithms

RSASignModes

The sign modes to use with the RSASign method.

Enum

Description

SHA1

SHA1 algorithm (160-bit (20-byte) hash value)

SHA2_256

SHA256 algorithm (SHA-2 with 256 bit digest)

SHA2_512

SHA512 algorithm (SHA-2 with 512 bit digest)

Method descriptions


Crypto.AESDecrypt

AESDecrypt(publicKey As String, data As MemoryBlock, blockMode As Crypto = BlockModes.CBC, initializationVector As MemoryBlock) As MemoryBlock

Decrypts the data using the specified key.

Important

This method is not currently supported for Android.


Crypto.AESEncrypt

AESEncrypt(publicKey As String, data As MemoryBlock, blockMode As Crypto = BlockModes.CBC, initializationVector As MemoryBlock) As MemoryBlock

Encrypts the data using the specified key.

An initialization vector (IV) is a block of bits that is used by several modes to randomize the encryption and hence to produce distinct ciphertexts even if the same plaintext is encrypted multiple times, without the need for a slower re-keying process.

Important

This method is not currently supported for Android.


Crypto.BERDecodePrivateKey

BERDecodePrivateKey(privateKey As String) As MemoryBlock

Decodes a private key for interoperability with the BER encoding for use by other libraries.

For more information, see: http://www.cryptopp.com/wiki/Keys_and_Formats#BER_and_DER_Encoding

Important

This method is not currently supported for Android.


Crypto.BERDecodePublicKey

BERDecodePublicKey(publicKey As String) As MemoryBlock

Decodes a public key for interoperability with the BER encoding for use by other libraries.

For more information, see: http://www.cryptopp.com/wiki/Keys_and_Formats#BER_and_DER_Encoding

Important

This method is not currently supported for Android.


Crypto.BlowFishDecrypt

BlowFishDecrypt(publicKey As String, data As MemoryBlock, blockMode As Crypto = BlockModes.CBC, initializationVector As MemoryBlock) As MemoryBlock

Decrypts the data using the specified key.

Important

This method is not currently supported for Android.


Crypto.BlowFishEncrypt

BlowFishEncrypt(publicKey As String, data As MemoryBlock, blockMode As Crypto = BlockModes.CBC, initializationVector As MemoryBlock) As MemoryBlock

Encrypts the data using the specified key.

Important

This method is not currently supported for Android.


Crypto.DEREncodePrivateKey

DEREncodePrivateKey(privateKey As String) As MemoryBlock

Encodes a private key for interoperability with the DER encoding for use by other libraries.

For more information, see: http://www.cryptopp.com/wiki/Keys_and_Formats#BER_and_DER_Encoding

Important

This method is not currently supported for Android.


Crypto.DEREncodePublicKey

DEREncodePublicKey(publicKey As String) As MemoryBlock

Encodes a public key for interoperability with the DER encoding for use by other libraries.

For more information, see: http://www.cryptopp.com/wiki/Keys_and_Formats#BER_and_DER_Encoding

Important

This method is not currently supported for Android.


Crypto.ED25519Sign

ED25519Sign(privateKey As String, data As MemoryBlock) As MemoryBlock

Encodes the data with the privatekey using ED25519 and returns it as a MemoryBlock.


Crypto.GenerateRandomBytes

GenerateRandomBytes(ByteCount As Integer) As MemoryBlock

Generates a random block of data.


Crypto.Hash

Hash(data As MemoryBlock, hashAlgorithm As Crypto.HashAlgorithms) As MemoryBlock

Creates a hash value for the data using the specified hashAlgorithm.

See the Crypto enumeration for values that can be used specify the type of Hash.

This code calculates a hash for a password using SHA512:

Var hash As String
hash = Crypto.Hash("YourPasswordSentence", Crypto.HashAlgorithms.SHA512)

Crypto.HMAC

HMAC(key As String, data As MemoryBlock, hashAlgorithm As Crypto.HashAlgorithms) As MemoryBlock

Creates the hash-based message authentication code using the data and the supplied key value and the supplied hashAlgorithm.

The key value is applied to the data before generating the hash.

See HMAC on Wikipedia.

Use the Crypto enumeration with the Hash method to specify the type of hash. It has these values:

  • MD5

  • SHA1

  • SHA2_256

  • SHA2_512

Warning

HMAC does not support CRC32, SHA3_256 and SHA3_512 modes. Using them will raise an UnsupportedOperationException.

This example creates a hash using HMAC and SHA512:

Var hash As String
hash = Crypto.HMAC("SaltValue", "YourPasswordSentence", Crypto.HashAlgorithms.SHA512)

Crypto.MD5

MD5(data As MemoryBlock) As MemoryBlock

Returns the MD5 message-digest value of the data.

Works the same as the MD5 function.

Var md5Value As MemoryBlock
md5Value = Crypto.MD5("SomeStringData")

Var hexValue As String = EncodeHex(md5Value)
' hexValue = 1E7984567DBC8AF6A48102C5E71A08E9

This above is equivalent to calling the Hash method with MD5 as the algorithm:

Var md5Value As MemoryBlock
md5Value = Crypto.Hash("SomeStringData", Crypto.Algorithm.MD5)

Var hexValue As String = EncodeHex(md5Value)
' hexValue = 1E7984567DBC8AF6A48102C5E71A08E9

Crypto.PBKDF2

PBKDF2(salt As String, data As MemoryBlock, iterations As Integer, desiredHashLength As Integer, hashAlgorithm As Crypto.hashAlgorithm) As MemoryBlock

Returns the PBKDF2 hash value of the data, first applying the salt value and using the specified hashAlgorithm. The iteration parameter is the number of loops that the hash algorithm does. The desiredHashLength parameter lets you specify the number of bytes that you want the resulting hash to be. 16 or 32 bytes are commonly used.

PBKDF2 is a "slow", i.e. deliberately processing intensive, algorithm for generating hash values. Slow is relative, for generating a single hash value it is plenty fast. The benefit of a slow algorithm is that it is impractical for hackers to generate hash tables using it because it would take too long to generate the thousands of hashes for commonly used values.

Use a higher value for iterations' to further slow the hash creation.

Refer to PBKDF2 on Wikipedia.

Use the Crypto enumeration with the Hash method to specify the type of hash. It has these values:

  • MD5

  • SHA1

  • SHA2_256

  • SHA2_512

Warning

PBKDF2 does not support CRC32, SHA3_256 and SHA3_512 modes. Using them will raise an UnsupportedOperationException.

This example generates a 32-byte hash using PBKDF2 for the salt and data, iterating 100 times:

Var hash As MemoryBlock
hash = Crypto.PBKDF2("SaltValue", "YourPasswordSentence", 100, 32, Crypto.HashAlgorithms.SHA512)
Var hashValue As String = EncodeHex(hash)
' FB8F64E66A72517AC7CA70145640B1932A986A7A7DDE7EC79602F8CE94DE0F8A

Crypto.RSADecrypt

RSADecrypt(Data As MemoryBlock, PrivateKey As String) As MemoryBlock

Decrypts data using the specified key, which is either a private or public key.

Decrypt a message:

Var privateKey As String
Var publicKey As String

If Crypto.RSAGenerateKeyPair( 1024, privateKey, publicKey ) Then
  ' 1024-bit private and public keys were generated

  Const kMessage = "this is a test"

  Var msg As New MemoryBlock(14)
  msg.StringValue(0, 14) = kMessage

  ' Encrypt msg using the publicKey
  Var encryptedData As MemoryBlock = Crypto.RSAEncrypt( msg, publicKey )

  If encryptedData <> Nil Then
    MessageBox("Successfully encrypted.")

    ' Now decrypt
    Var decryptedData As MemoryBlock = Crypto.RSADecrypt( encryptedData, privateKey )
    MessageBox("DecryptedData=" + decryptedData.StringValue(0, 14))
  End If
End If

Crypto.RSAEncrypt

RSAEncrypt(Data As MemoryBlock, PublicKey As String) As MemoryBlock

Encrypts the data using the specified PublicKey.

Attempting to encrypt using a PrivateKey raises a CryptoException.

Encrypt some text:

Var privateKey As String
Var publicKey As String

If Crypto.RSAGenerateKeyPair( 1024, privateKey, publicKey ) Then
  ' 1024-bit private and public keys were generated

  Const kMessage = "this is a test"

  Var msg As New MemoryBlock(14)
  msg.StringValue(0, 14) = kMessage

  ' Encrypt msg using the publicKey
  Var encryptedData As MemoryBlock = Crypto.RSAEncrypt( msg, publicKey )

  If encryptedData <> Nil Then
    MessageBox("Successfully encrypted.")
  End If
End If

Crypto.RSAGenerateKeyPair

RSAGenerateKeyPair(Bits As Integer, ByRef PrivateKey As String, ByRef PublicKey As String) As Boolean

Generates a private and public key pair that is hex encoded. Returns True on success, False if failed. Bits is usually 1024 or 2048.

Generate 1024-bit private and public keys:

Var privateKey As String
Var publicKey As String

If Crypto.RSAGenerateKeyPair( 1024, privateKey, publicKey ) Then
  ' 1024-bit private and public keys were generated
End If

Crypto.RSASign

RSASign(Data As MemoryBlock, PrivateKey As String) As MemoryBlock

Signs the data block using the specified PrivateKey using PKCS v1.5 with SHA1.

Sign a message:

Var privateKey As String
Var publicKey As String

If Crypto.RSAGenerateKeyPair( 1024, privateKey, publicKey ) Then
  ' 1024-bit private and public keys were generated

  Var msg As String = "this is a test"

  Var signature As MemoryBlock = Crypto.RSASign( msg, privateKey )
  If signature <> Nil Then
    ' msg was successfully signed
  End If
End If

Crypto.RSAVerifyKey

RSAVerifyKey(Key As String) As Boolean

Attempts to validate the specified key.

Verifies the generated public key:

Var privateKey As String
Var publicKey As String

If Crypto.RSAGenerateKeyPair( 1024, privateKey, publicKey ) Then
  ' 1024-bit private and public keys were generated

  If Crypto.RSAVerifyKey(publicKey) Then
    ' publicKey is valid
  End If

End If

Verification fails on a "fake" key:

If Crypto.RSAVerifyKey("not a real key!") Then
  MessageBox("Valid!")
Else
  MessageBox("Not valid!")
End If

Crypto.RSAVerifySignature

RSAVerifySignature(Data As MemoryBlock, Signature As MemoryBlock, PublicKey As String) As Boolean

Verifies the data using the specified signature and key using PKCS v1.5 with SHA1. Returns True if the signature is verified, False if it is not.

Verify a message:

Var privateKey As String
Var publicKey As String

If Crypto.RSAGenerateKeyPair(1024, privateKey, publicKey) Then
  ' 1024-bit private and public keys were generated

  Var msg As String = "this is a test"

  Var signature As MemoryBlock = Crypto.RSASign(msg, privateKey)
  If signature <> Nil Then
    ' msg was successfully signed

    ' Verify signature
    If Crypto.RSAVerifySignature(msg, signature, publicKey) Then
      ' signature is valid
    End If

  End If
End If

Crypto.SHA1

SHA1(data As MemoryBlock) As MemoryBlock

Returns the SHA1 hash value of the data.

Refer to SHA1 on Wikipedia.

This is equivalent of calling the Hash method with SHA1 as the algorithm:

Var encryptedValue As String
encryptedValue = Crypto.Hash("DataToEncrypt", Crypto.HashAlgorithms.SHA1)

Crypto.SHA2_256

SHA2_256(data As MemoryBlock) As MemoryBlock

Returns the SHA256 hash value of the data.

Refer to SHA256 on Wikipedia.

This is equivalent of calling the Hash method with SHA256 as the algorithm:

Var encryptedValue As String
encryptedValue = Crypto.Hash("DataToEncrypt", Crypto.HashAlgorithms.SHA256)

Crypto.SHA2_512

SHA2_512(data As MemoryBlock) As MemoryBlock

Returns the SHA512 hash value of the data.

Refer to SHA512 on Wikipedia.

This is equivalent of calling the Hash method with SHA512 as the algorithm:

Var encryptedValue As String
encryptedValue = Crypto.Hash("DataToEncrypt", Crypto.HashAlgorithms.SHA512)

Crypto.SHA3_256

SHA3_256(data As MemoryBlock) As MemoryBlock

Returns the SHA3_256 hash value of the data.

Refer to SHA-3 on Wikipedia.

This is equivalent of calling the Hash method with SHA3_256 as the algorithm:

Var encryptedValue As String
encryptedValue = Crypto.Hash("DataToEncrypt", Crypto.HashAlgorithms.SHA3_256)

Crypto.SHA3_512

SHA3_512(data As MemoryBlock) As MemoryBlock

Returns the SHA3_512 hash value of the data.

Refer to SHA-3 on Wikipedia.

This is equivalent of calling the Hash method with SHA3_512 as the algorithm:

Var encryptedValue As String
encryptedValue = Crypto.Hash("DataToEncrypt", Crypto.HashAlgorithms.SHA3_512)

Crypto.TwoFishDecrypt

TwoFishDecrypt(publicKey As String, data As MemoryBlock, blockMode As Crypto.BlockModes = BlockModes.CBC, initializationVector As MemoryBlock) As MemoryBlock

Decrypts the data using the specified key.


Crypto.TwoFishEncrypt

TwoFishEncrypt(publicKey As String, data As MemoryBlock, blockMode As Crypto.BlockModes = BlockModes.CBC, initializationVector As MemoryBlock) As MemoryBlock

Encrypts the data using the specified key.

Important

This method is not currently supported for Android.

Notes

Uses Crypto ++ Library 5.6.5.

Use the HashAlgorithms enumeration with the Hash method to specify the type of hash.

Should any Crypto function fail, a CryptoException will be raised.

Compatibility

All project types on all supported operating systems.

See also

CryptoException class.