Visual C++ Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

C++ Examples

CkString
Byte Array
Bounced Mail
Bz2
Character Encoding
CSV
DH Key Exchange
DKIM/DomainKeys
Digital Certificates
Digital Signatures
DSA
Email
Email Object
Encryption
FTP
HTML Conversion
HTTP
IMAP
MHT / HTML Email
POP3
RSA
S/MIME
SMTP
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
Tar
Upload
XML
XMP
Zip Compression


More Examples...
Amazon S3
NTLM
FileAccess
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
Bzip2
LZW

 

 

 

 

 

 

 

 

Match Java JCE AES Encryption Results (ECB mode)

Downloads:

MS Windows Visual C/C++ Libraries
Linux/CentOS C/C++ Libraries
MAC OS X C/C++ Libraries
Solaris C/C++ Libraries
C++ Builder Libraries
FreeBSD C++ Libraries
HP-UX C++ Libraries
BlackBerry QNX C++ Libraries

Shows sample code in C++ that matches Java JCE encryption results for 128-bit AES encryption using ECB mode.

#include <CkCrypt2.h>
#include <CkString.h>

void ChilkatSample(void)
    {
    CkCrypt2 crypt;

    // Any string argument automatically begins the 30-day trial.
    bool success;
    success = crypt.UnlockComponent("30-day trial");
    if (success != true) {
        printf("Crypt component unlock failed\n");
        return;
    }

    CkString password;
    password = "secretPassphrase";

    // Use the Digest MD5 algorithm to get a 16-byte binary secret key.
    crypt.put_HashAlgorithm("md5");
    crypt.put_EncodingMode("hex");
    CkString secretKeyHex;
    secretKeyHex = crypt.hashStringENC(password);

    crypt.put_CryptAlgorithm("aes");
    crypt.put_CipherMode("ecb");
    crypt.put_KeyLength(128);
    crypt.SetEncodedKey(secretKeyHex,"hex");

    CkString text;
    text = "The quick brown fox jumped over the lazy dog.";
    CkString encText;
    encText = crypt.encryptStringENC(text);

    // Displays:
    // C960892077D566258D38DF4BBAE8548E6827BF8C27A9D2EE
    // 6F563FC75003F8F6A05B54CFD28CDC8833BD073F7FC6D031
    printf("%s\n",encText);

    // This Java (JCE) code produces the same results:
    // String password = "secretPassphrase";
    // 
    // MessageDigest md = MessageDigest.getInstance("MD5");
    // md.update(password.getBytes("UTF-8"), 0, password.length());
    // byte[] rawKey = md.digest();
    // 
    // String text = "The quick brown fox jumped over the lazy dog.";
    // 
    // SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
    // Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    // cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    // 
    // byte[] encrypted = cipher.doFinal(text.getBytes("UTF-8"));
    // 
    // System.out.println(toHex(encrypted));

    // This is the toHex method in Java:
    // public static String toHex (byte buf[]) {
    // 
    // StringBuffer strbuf = new StringBuffer(buf.length * 2);
    // int i;
    // 
    // for (i = 0; i < buf.length; i++) {
    // if (((int) buf[i] & 0xff) < 0x10)
    // strbuf.append("0");
    // 
    // strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    // }
    // 
    // return strbuf.toString();
    // }

    }

© 2000-2010 Chilkat Software, Inc. All Rights Reserved.

Mail Component · .NET Mail Component · XML Parser