Android™
Android™
Manually Duplicate SetSecretKeyViaPassword
Demonstrates how to duplicate the password string to binary secret key computation of SetSecretKeyViaPassword.This is a cryptographically weak way of generating a secret key from a password. The SetSecretKeyViaPassword method is deprecated and should not be used.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkCrypt2 crypt = new CkCrypt2();
// The password string is transformed to a binary secret key by computing the
// MD5 digest (of the utf-8 password) to obtain 16 bytes.
// If the KeyLength is greater than 16 bytes, then the MD5 digest of the Base64 encoding
// of the utf-8 password is added. A max of 32 bytes of key material is generated, and
// this is truncated to the actual KeyLength required.
crypt.put_CryptAlgorithm("aes");
crypt.put_KeyLength(256);
String password = "this is my password";
crypt.SetSecretKeyViaPassword(password);
// Examine the resulting SecretKey in hex:
Log.i(TAG, "Computed Secret Key = " + crypt.getEncodedKey("hex"));
// Now perform the same computation manually:
CkStringBuilder sb = new CkStringBuilder();
crypt.put_HashAlgorithm("md5");
crypt.put_Charset("utf-8");
crypt.put_EncodingMode("hex");
sb.Append(crypt.hashStringENC(password));
String passwordBase64 = crypt.encodeString(password,"utf-8","base64");
sb.Append(crypt.hashStringENC(passwordBase64));
Log.i(TAG, "Manually Computed = " + sb.getAsString());
// The output is:
// Computed Secret Key = 210D53992DFF432EC1B1A9698AF9DA16C7E90518F90E24828F78EC9E0A413B36
// Manually Computed = 210D53992DFF432EC1B1A9698AF9DA16C7E90518F90E24828F78EC9E0A413B36
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}