RSA BSAFE Crypto-C

Cryptographic Components for C

Search

Converting Data Between Binary and ASCII

This sample demonstrates how to encode binary data to ASCII and decode ASCII-encoded data. If data is in binary format, but required in ASCII format, or vice versa, Crypto-C provides functions to encode and decode according to the RFC 1113 standard.

/* $Id: encdec.c,v 1.6 2004/12/03 02:08:34 sparki Exp $ */
/*
 * Copyright (C) 1998-2004 RSA Security Inc.
 *
 * This file shall only be used to demonstrate how to interface to an
 * RSA Security Inc. licensed development product.
 *
 * You have a royalty-free right to use, reproduce and distribute this
 * demonstration file, provided that you agree that RSA Security Inc.
 * has no warranty, implied or otherwise, or liability for this
 * demonstration file (including any modified version).  This software
 * is provided "as is" without warranties or representations of any
 * kind. RSA Security disclaims all conditions and warranties, statutory
 * and otherwise, both express and implied, with respect to the software,
 * its quality and performance, including but not limited to, all
 * implied warranties of merchantability, fitness for a particular
 * purpose, title and noninfringement of third party rights. Without
 * limiting the foregoing, RSA Security does not warrant that the
 * software is error-free or that errors in the product will be
 * corrected. You agree that RSA Security shall not be liable for any
 * direct, indirect, incidental, special, consequential, punitive or
 * other damages whatsoever resulting from your use of this software
 * or any modified version.
 *
 *
 */

/*  This program will convert data between Binary and ASCII.  */
     
#include "bsafe.h"
#include "demoutil.h"  /* in samples/common/include */
#include "bsfutil.h"   /* in samples/common/include */

#define NUMBER_OF_RANDOM_BYTES 64

#ifdef CRYPTOC_APP
#define MAIN encdecMain
#else
#define MAIN main
#endif

int MAIN(int argc, char *argv[])
{
  B_ALGORITHM_OBJ randomAlgorithm = (B_ALGORITHM_OBJ)NULL_PTR;
  B_ALGORITHM_OBJ asciiEncoder = (B_ALGORITHM_OBJ)NULL_PTR;
  B_ALGORITHM_OBJ asciiDecoder = (B_ALGORITHM_OBJ)NULL_PTR;

  unsigned char *binaryData = NULL_PTR;
  unsigned int binaryDataLen = NUMBER_OF_RANDOM_BYTES;

  unsigned char *asciiEncoding = NULL_PTR;
  unsigned int asciiEncodingLen;
  unsigned int asciiEncodingLenUpdate;
  unsigned int asciiEncodingLenFinal;
  unsigned int asciiEncodingLenTotal;

  unsigned char *binaryDecoding = NULL_PTR;
  unsigned int binaryDecodingLen;
  unsigned int binaryDecodingLenUpdate;
  unsigned int binaryDecodingLenFinal;
  unsigned int binaryDecodingLenTotal;
  
  int status;

  do {
    /* The RSA_* demo code utilities are described in
       common/include/demoutil.h.  This procedure simply checks the
       command-line arguments for input or output options. */
    if ((status = RSA_SetOptions (argc, argv)) != 0)
      break;
    
    RSA_PrintMessage ("Encoding-Decoding Algorithm\n");
    RSA_PrintMessage ("===========================\n");

    RSA_PrintMessage ("  First, generate some random bytes to be encoded.\n");

    /*  Initialize a random algorithm object using a procedure described in
        samples/common/include/bsfutil.h  */
    if ((status = RSA_CreateRandomAlgorithmObject (&randomAlgorithm)) != 0)
      break;

    binaryData = T_malloc (binaryDataLen);
    if (binaryData == NULL_PTR) {
      status = RSA_DEMO_E_ALLOC;
      break;
    }

    if ((status = B_GenerateRandomBytes (randomAlgorithm, binaryData,
                                         binaryDataLen,
                                         (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    RSA_PrintBuf ("Random Binary Data", binaryData, binaryDataLen);

    RSA_PrintMessage ("Now, ASCII-encode the binary data...\n");
    
    /*  Create algorithm object */
    if ((status = B_CreateAlgorithmObject (&asciiEncoder)) != 0)
      break;

    /*  Set algorithm object to AI_RFC1113Recode */
    if ((status = B_SetAlgorithmInfo (asciiEncoder, AI_RFC1113Recode,
                                      NULL_PTR)) != 0)
      break;

    /*  Init */
    if ((status = B_EncodeInit (asciiEncoder)) != 0)
      break;

    /*  Update & Final -- Encoding stage  */
    asciiEncodingLen = binaryDataLen * 2;
    asciiEncoding = T_malloc (asciiEncodingLen);
    if (asciiEncoding == NULL_PTR) {
      status = RSA_DEMO_E_ALLOC;
      break;
    }

    if ((status = B_EncodeUpdate (asciiEncoder, asciiEncoding,
                                  &asciiEncodingLenUpdate, asciiEncodingLen,
                                  binaryData, binaryDataLen)) != 0)
      break;

    if ((status = B_EncodeFinal
                    (asciiEncoder, asciiEncoding + asciiEncodingLenUpdate,
                     &asciiEncodingLenFinal,
                     asciiEncodingLen - asciiEncodingLenUpdate)) != 0)
      break;

    asciiEncodingLenTotal = asciiEncodingLenUpdate + asciiEncodingLenFinal;
    
    RSA_PrintMessage ("The ASCII-encoded data:\n");
    asciiEncoding[asciiEncodingLenTotal] = 0;
    
    RSA_PrintMessage ("%s\n", asciiEncoding);

    RSA_PrintMessage ("\nNow, decode the ASCII-encoded data...\n");

    if ((status = B_CreateAlgorithmObject (&asciiDecoder)) != 0)
      break;

    if ((status = B_SetAlgorithmInfo (asciiDecoder, AI_RFC1113Recode,
                                      NULL_PTR)) != 0)
      break;

    if ((status = B_DecodeInit (asciiDecoder)) != 0)
      break;

    binaryDecodingLen = asciiEncodingLenTotal;
    binaryDecoding = T_malloc (binaryDecodingLen);
    if (binaryDecoding == NULL_PTR) {
      status = RSA_DEMO_E_ALLOC;
      break;
    }
    
    if ((status = B_DecodeUpdate (asciiDecoder, binaryDecoding,
                                  &binaryDecodingLenUpdate, binaryDecodingLen,
                                  asciiEncoding, asciiEncodingLenTotal)) != 0)
      break;

    if ((status = B_DecodeFinal
                    (asciiDecoder, binaryDecoding + binaryDecodingLenUpdate,
                     &binaryDecodingLenFinal,
                     binaryDecodingLen - binaryDecodingLenUpdate)) != 0)
      break;

    binaryDecodingLenTotal = binaryDecodingLenUpdate + binaryDecodingLenFinal;
    
    RSA_PrintBuf ("The Binary-decoded data", binaryDecoding,
                  binaryDecodingLenTotal);

    if (binaryDecodingLenTotal != binaryDataLen) {
      status = RSA_DEMO_E_INFO_DOES_NOT_VERIFY;      
      break;
    }

    if (T_memcmp (binaryData, binaryDecoding, binaryDecodingLenTotal) != 0) {
      status = RSA_DEMO_E_INFO_DOES_NOT_VERIFY;      
      break;
    }

    RSA_PrintMessage ("The binary-decoded data matches the original ");
    RSA_PrintMessage ("binary data.\n");    
  } while (0);
        
  if (status != 0)
    RSA_PrintError ("encdec", status);
 
  /*  Destroy  */
  B_DestroyAlgorithmObject (&randomAlgorithm);
  B_DestroyAlgorithmObject (&asciiEncoder);           
  B_DestroyAlgorithmObject (&asciiDecoder);

  /*  Free up any memory allocated  */
  if (binaryData != NULL_PTR)
    T_free (binaryData);

  if (asciiEncoding != NULL_PTR)
    T_free (asciiEncoding);

  if (binaryDecoding != NULL_PTR)
    T_free (binaryDecoding);

  return (status);
} /*  end main  */

Copyright (c) 1999-2005 RSA Security Inc. All rights reserved. 068-001001-6210-001-000 - 6.2.1