public class

SCSecurityFacade

extends Object
java.lang.Object
   ↳ com.c2call.sdk.pub.facade.SCSecurityFacade

Class Overview

This singleton provides a low-level access API to manage security certificates and to en-/decrypt secure messages and media files.

Summary

Public Methods
String decrypt(Context ctx, String message)
Convenient method which calls decrypt(Context, PrivateKey, String) with the default message taken from the string resource R.string.sc_encrypted_message
String decrypt(Context ctx, PrivateKey prvKey, String message)
Decrypts a message with a given private key.
String decrypt(Context ctx, String message, String defMessage)
Tries to decrypt the given message.
boolean deleteAndUpdateKeypair(Context ctx)
Deletes a previously generated key pair for the current user.
boolean deleteKeyPair(Context ctx)
Deletes a previously generated key pair from local storage.
String encrypt(Context ctx, String message, String keyBase64)
Same as encrypt(Context, String, PublicKey), but wit a Base64 encoded public key.@return
String encrypt(Context ctx, String message, PublicKey key)
Encrypts a message with the given public key.
String encryptFile(Context ctx, String path, String keyBase64, String userid)
Convenient method for encrypt(Context, String, PublicKey) with a Base64 encoded public key.@return
String encryptFile(Context ctx, String path, PublicKey key, String userid)
Encrypts a file with the given public key
KeyPair generateKeypair(Context ctx, boolean forceRenew)
Convenient method to generate and store a new keypair for the current user.
String getFingerprint(int maxLength)
Gets a SHA-1 fingerprint of the locally stored key pair.
KeyPair getKeyPair()
Gets the current user's key pair which is only stored on the local device.
PrivateKey getPrivateKey()
Convenient method to get only the private key part to decrypt messages.
int importKeyFile(Activity ctx, String uri, String password)
static SCSecurityFacade instance()
Get the singleton instance of this class
boolean isEncrypted(String message)
Checks whether the message is encrypted
boolean isPrivateKeyValid(Context ctx)
Checks if the the locally stored private key for this user matches the public key on our server.
boolean isSecureMessagingEnabled(Context ctx)
Checks if the string resource R.string.sc_enable_secure_messaging is "true".
boolean setAndUpdateKeypair(Context ctx, KeyPair kp)
Sets a new key pair for the current user.
void setKeyPair(Context ctx, KeyPair kp)
Saves e new key pair for the currently logged in user to the local storage.
String toBase64(Key key)
Generates a Base64 encoded String for the given key
String tryDecrypt(Context ctx, PrivateKey key, String message, String def)
String tryDecrypt(Context ctx, String message, String def)
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public String decrypt (Context ctx, String message)

Convenient method which calls decrypt(Context, PrivateKey, String) with the default message taken from the string resource R.string.sc_encrypted_message

Parameters
ctx the context to use.
message the message to decrypt
Returns
  • the decrypted message

public String decrypt (Context ctx, PrivateKey prvKey, String message)

Decrypts a message with a given private key. Usually you can use the convenient method decrypt(Context, String) which uses the locally stored private key.@return

Parameters
ctx the context to use
prvKey the private key to use for decryption
message the message to decrypt
Throws
NoSuchPaddingException
IOException
BadPaddingException
IllegalBlockSizeException
InvalidKeyException
BadPaddingException
IOException
IllegalBlockSizeException
InvalidKeyException
NoSuchPaddingException

public String decrypt (Context ctx, String message, String defMessage)

Tries to decrypt the given message. If the decryption fails then it was likely encrypted with a public key, that does not match the locally stored one and the message will return the given default message.

Parameters
ctx the context to use
message the message to decrypt
defMessage a default message to return if the decryption fails due to a wrong key
Returns
  • the decrypted message

public boolean deleteAndUpdateKeypair (Context ctx)

Deletes a previously generated key pair for the current user. This method will remove the public key from the user's profile and will also remove the private key from local storage. Must not be called on main-thread

Parameters
ctx the context to use
Returns
  • true on success.

public boolean deleteKeyPair (Context ctx)

Deletes a previously generated key pair from local storage. Note that this method does not update the user's profile. You should use deleteAndUpdateKeypair(Context) for this.

Parameters
ctx the context to use
Returns
  • true on success

public String encrypt (Context ctx, String message, String keyBase64)

Same as encrypt(Context, String, PublicKey), but wit a Base64 encoded public key.@return

Parameters
message the message to encrypt
keyBase64 the public key to use for encryption
Throws
NoSuchPaddingException
ShortBufferException
NoSuchAlgorithmException
IllegalBlockSizeException
BadPaddingException
NoSuchProviderException
InvalidKeyException
IOException
InvalidKeySpecException
BadPaddingException
IOException
IllegalBlockSizeException
InvalidKeyException
NoSuchAlgorithmException
NoSuchPaddingException
NoSuchProviderException
ShortBufferException

public String encrypt (Context ctx, String message, PublicKey key)

Encrypts a message with the given public key.

Parameters
message the message to encrypt
key the public key to use for encryption
Returns
  • The encrypted message
Throws
NoSuchPaddingException
ShortBufferException
NoSuchAlgorithmException
IllegalBlockSizeException
BadPaddingException
NoSuchProviderException
InvalidKeyException
IOException
BadPaddingException
IllegalBlockSizeException
InvalidKeyException
NoSuchAlgorithmException
NoSuchPaddingException
NoSuchProviderException
ShortBufferException

public String encryptFile (Context ctx, String path, String keyBase64, String userid)

Convenient method for encrypt(Context, String, PublicKey) with a Base64 encoded public key.@return

Parameters
ctx the context to use
path the path to the file to encrypt
keyBase64 the Base64 encoded public key to use for encryption
userid the userid of the recipient of the file
Throws
Exception

public String encryptFile (Context ctx, String path, PublicKey key, String userid)

Encrypts a file with the given public key

Parameters
ctx the context to use
path the path to the file to encrypt
key the key to user for encryption
userid the userid of the recipient of the file.
Returns
  • the absolute path of the encrypted file.
Throws
Exception

public KeyPair generateKeypair (Context ctx, boolean forceRenew)

Convenient method to generate and store a new keypair for the current user. This method will also update the profile. So all previously generated keys (on other devices) will become invalid. You have to export this new key to the other devices; otherwise you will not be able to decrypt messages there, anymore. Must not be called on main-thread.

Parameters
ctx the context to use
forceRenew if true, then a new key pair will be generated even if their is already one. Otherwise the existing one will be used.
Returns
  • the new keypair for the current user. If

    forceRenew

    was false then this may be an old one if such existed.

public String getFingerprint (int maxLength)

Gets a SHA-1 fingerprint of the locally stored key pair. The returned string shows the fingerprint's bytes as colon-separated hex values. E.g. A0:B1:C2:D3:E4...

Parameters
maxLength the maximum length of (fingerprint-) bytes to show. You can set this to -1 to get the full SHA-1 value
Returns
  • a SHA-1 fingerprint of the stored key pair.

public KeyPair getKeyPair ()

Gets the current user's key pair which is only stored on the local device. Note: If a user generated a key pair on another device,too, then this method returns different keys for the same user on both devices. So you may want to implement some kind of key import/export for this scenario. Otherwise only the last generated key will be used to encrypt messages, and only that device will be able to decrypt the messages

Returns
  • Current key of the logged in user from local storage.

public PrivateKey getPrivateKey ()

Convenient method to get only the private key part to decrypt messages.

Returns
  • The private key part of the security key pair.

public int importKeyFile (Activity ctx, String uri, String password)

public static SCSecurityFacade instance ()

Get the singleton instance of this class

public boolean isEncrypted (String message)

Checks whether the message is encrypted

Parameters
message the message to check.
Returns
  • true if the message is encrypted, false otherwise.

public boolean isPrivateKeyValid (Context ctx)

Checks if the the locally stored private key for this user matches the public key on our server. If it returns falls then this typically means that the user has generated a new keypair on another device. You should import/export that key or generate a new one to use secure messaging on this device.

Returns
  • true if the local private key matches the public key on our server

public boolean isSecureMessagingEnabled (Context ctx)

Checks if the string resource R.string.sc_enable_secure_messaging is "true". If yes, then the default GUI will show secure messaging relevant elements, otherwise these are hidden from the user.

Returns
  • true if secure messaging is enabled in the default GUI

public boolean setAndUpdateKeypair (Context ctx, KeyPair kp)

Sets a new key pair for the current user. This method will update the public key of the user's profile and will also add the private key to the local storage. Must not be called on main-thread

Parameters
ctx the context to use
Returns
  • true on success.

public void setKeyPair (Context ctx, KeyPair kp)

Saves e new key pair for the currently logged in user to the local storage. Note that this method does not update the user's profile. You should use setAndUpdateKeypair(Context, KeyPair) for this.

Parameters
ctx the context to use.
kp the new key pair for the user

public String toBase64 (Key key)

Generates a Base64 encoded String for the given key

Parameters
key the key to encode
Returns
  • a Base64 encoded String for the given key.

public String tryDecrypt (Context ctx, PrivateKey key, String message, String def)

public String tryDecrypt (Context ctx, String message, String def)