EXTAPI void secure_aes_cbc_init(void)
{
  //  To arrive at a 128-bit session key for AES encryption, Bob and Alice should
  //  both transform the raw shared secret using a hash algorithm that produces
  //  the size of session key desired.   MD5 produces a 16-byte (128-bit) result, so
  //  this is a good choice for 128-bit AES.

  //  Here's how you would use Chilkat Crypt (a separate Chilkat component) to
  //  produce the session key:
  crypt = CkCrypt2_Create();
  success = CkCrypt2_UnlockComponent(crypt, "Anything for 30-day trial.");
  if (success != TRUE) {
		ALOGD("%s",CkCrypt2_lastErrorText(crypt));
		CkDh_Dispose(dhAlice);
		CkCrypt2_Dispose(crypt);
		return;
  }

  CkCrypt2_putEncodingMode(crypt, "hex");
  CkCrypt2_putHashAlgorithm(crypt, "md5");

  sessionKey = CkCrypt2_hashStringENC(crypt, kAlice);
  ALOGD("128-bit Session Key:");
  ALOGD("%s", sessionKey);

  //  Encrypt something...
  CkCrypt2_putCryptAlgorithm(crypt, "aes");
  CkCrypt2_putKeyLength(crypt, 128);
  CkCrypt2_putCipherMode(crypt, "cbc");

  //  Use an IV that is the MD5 hash of the session key...
  iv = CkCrypt2_hashStringENC(crypt, sessionKey);

  //  AES uses a 16-byte IV:
  ALOGD("Initialization Vector:");
  ALOGD("%s", iv);

  CkCrypt2_SetEncodedKey(crypt,sessionKey, "hex");
  CkCrypt2_SetEncodedIV(crypt,iv, "hex");

  //  Encrypt some text:
  CkCrypt2_putEncodingMode(crypt, "base64");
}
예제 #2
0
int encryptString(char* result, char* password)
{
    HCkCrypt2 crypt;
    BOOL success;
    const char * hexKey;
    const char * text = result;
    const char * encText;
    const char * decryptedText;

    crypt = CkCrypt2_Create();

    success = CkCrypt2_UnlockComponent(crypt,"Anything for 30-day trial");
    if (success != TRUE) {
        printf("Crypt component unlock failed\n");
        return 0;
    }

    CkCrypt2_putCryptAlgorithm(crypt,"aes");
    CkCrypt2_putCipherMode(crypt,"cbc");
    CkCrypt2_putKeyLength(crypt,256);

    //  Generate a binary secret key from a password string
    //  of any length.  For 128-bit encryption, GenEncodedSecretKey
    //  generates the MD5 hash of the password and returns it
    //  in the encoded form requested.  The 2nd param can be
    //  "hex", "base64", "url", "quoted-printable", etc.

    hexKey = CkCrypt2_genEncodedSecretKey(crypt,password,"hex");
    CkCrypt2_SetEncodedKey(crypt,hexKey,"hex");

    CkCrypt2_putEncodingMode(crypt,"base64");

    //  Encrypt a string and return the binary encrypted data
    //  in a base-64 encoded string.

    encText = CkCrypt2_encryptStringENC(crypt,result);
    //printf("%s\n",encText);
    char tmpStr[1000];
    strcpy(result, encText);	

    CkCrypt2_Dispose(crypt);
}