IoT CAS Frameworks/IOT_Server/License_Framework/LicenseKeyGenerator/src/com/prokosha/licensekeygenerator/GA_CreateKeys1.java
static int XXXValidateKeys(GA_KEY_INFORMATION1 lpKeyInfo, int piErrCode) {
		// long lLicenses = 0l;

		// Get the two keys from the Key Object

		StringBuffer sTotalValueKey = new StringBuffer();
		sTotalValueKey.setLength(36);

		// Generate the Total Key by adding two keys

		String sTotal_Key = (lpKeyInfo.szKey1).toString()
				+ (lpKeyInfo.szKey2).toString();
		System.out.println("Total_Key :::" + sTotal_Key);
		System.out.println("Total_Key length :::" + sTotal_Key.length());
		for (int ch = 0; ch < sTotal_Key.length(); ch++) {
			char iChar = sTotal_Key.charAt(ch);
			// System.out.println(iChar);
			int intik = (int) iChar;
			// System.out.println("intik is ASCII values ::" + intik);
			if ((intik <= 47) || (intik >= 91 && intik <= 96)
					|| (intik >= 58 && intik <= 64) || (intik >= 123))
				continue;
			sTotalValueKey.setCharAt(ch, iChar);
		}
		System.out.println(" sTotalValueKey key is ...." + sTotalValueKey);

		// Get the first two byte of total Key

		String sTotalCRCString = sTotalValueKey.substring(0, 2);
		int iTotalCRCInt = 0;
		try {
			System.out.println("TotalCRCString::" + sTotalCRCString);
			iTotalCRCInt = Integer.parseInt(sTotalCRCString);
		} catch (Exception e1) {
			System.out
					.println("Exception Caught at parseInt Please check for keys : sTotalCRCString"
							+ sTotalCRCString);
			e1.printStackTrace();
		}

		// Saparate the other 30 byte of key and generate CRC for that key

		String sTotalCRCCheck = sTotalValueKey.substring(2,
				sTotalValueKey.length());
		StringBuffer szTotalCRCBuffer = new StringBuffer(sTotalCRCCheck);
		szTotalCRCBuffer.setLength(30);
		System.out.println("szTotalCRCBuffer is @@@@@" + szTotalCRCBuffer);
		int i = Encode_CRC(szTotalCRCBuffer);

		System.out.println(" FIRST CRC is..." + iTotalCRCInt
				+ "...CRC Count is..." + i);

		// Compare generated CRC with key CRC and if they are incorrect then
		// send error

		if (i != iTotalCRCInt) {
			System.out.println("CRC didn't match");
			return (N_FAILURE);
		} else {
			// Divide the generated key into two keys of 14 byte each

			String sKey1Index = sTotalCRCCheck.substring(0, 15);
			String sKey2Index = sTotalCRCCheck.substring(15,
					sTotalCRCCheck.length());

			// Get the first two byte of first key

			String sKey1CRCString = sKey1Index.substring(0, 2);
			System.out.println("Key Value at 617::::::" + sKey1CRCString);
			int iKey1CRCInt = Integer.parseInt(sKey1CRCString);

			// Generate the CRC for the other

			String sKey1CRCCheckString = sKey1Index.substring(2,
					sKey1Index.length());
			StringBuffer sKey1CRCCheckBuffer = new StringBuffer(
					sKey1CRCCheckString);

			// String crc_temp = sKey1CRCCheckBuffer.toString();

			/*
			 * for (int ii = 0; ii < crc_temp.length(); ii++) { char ccheck =
			 * crc_temp.charAt(ii); }
			 */

			int j = Encode_CRC(sKey1CRCCheckBuffer);
			// System.out.println(" First CRC is..."+j+"  for  "+sKey1CRCCheckBuffer);

			// System.out.println("Second CRC is..."+iKey1CRCInt+"...CRC Count is..."+j);

			// Compare the generated CRC with CRC of key

			if (j != iKey1CRCInt) {
				System.out.println(" First CRC is not correct");
				return (N_FAILURE);
			} else {
				// Get first two byte of second key

				String sKey2CRCString = sKey2Index.substring(0, 2);
				int iKey2CRCInt = Integer.parseInt(sKey2CRCString);

				// Generate the CRC of second key

				String sKey2CRCCheckString = sKey2Index.substring(2,
						sKey2Index.length());
				StringBuffer sKey2CRCCheckBuffer = new StringBuffer(
						sKey2CRCCheckString);

				int k = Encode_CRC(sKey2CRCCheckBuffer);

				// System.out.println("Third CRC is..."+iKey2CRCInt+"...CRC Count is..."+k);

				// Compare the two CRC's

				if (k != iKey2CRCInt) {
					System.out.println(" Second CRC is not correct");
					return (N_FAILURE);
				}

				// Get the concatedned string of two keys ( 26 byte string)

				String sEncryKey = sKey1CRCCheckString + sKey2CRCCheckString;

				StringBuffer sEncryKeyBuffer = new StringBuffer(sEncryKey);
				sEncryKeyBuffer.setLength(27);

				// Decrypt the string

				StringBuffer temp1Buffer = dec(sEncryKeyBuffer);

				// Original key will ge generated

				// System.out.println(" Original key is ...."+temp1Buffer);

				lpKeyInfo.szTotalKey = temp1Buffer;
			}
		}
		return 0;
	}
/*
	 * Method XXXValidateKeys This method accepts GA_KEY_INFORMATION structure
	 * and validate the two sets of keys While validating it checks the CRC byte
	 * of each key and also CRC byte of whole key
	 */
Method XXXValidateKeys This method accepts GA_KEY_INFORMATION structure
and validate the two sets of keys While validating it checks the CRC byte
of each key and also CRC byte of whole key
Method XXXValidateKeys This method accepts GA_KEY_INFORMATION structure and validate the two sets of keys While validating it checks the CRC byte of each key and also CRC byte of whole key
static int XXXValidateKeys(GA_KEY_INFORMATION1 lpKeyInfo, int piErrCode) {
		StringBuffer sTotalValueKey = new StringBuffer();
		sTotalValueKey.setLength(36);
		String sTotal_Key = (lpKeyInfo.szKey1).toString()
				+ (lpKeyInfo.szKey2).toString();
		System.out.println("Total_Key :::" + sTotal_Key);
		System.out.println("Total_Key length :::" + sTotal_Key.length());
		for (int ch = 0; ch < sTotal_Key.length(); ch++) {
			char iChar = sTotal_Key.charAt(ch);
			int intik = (int) iChar;
			if ((intik <= 47) || (intik >= 91 && intik <= 96)
					|| (intik >= 58 && intik <= 64) || (intik >= 123))
				continue;
			sTotalValueKey.setCharAt(ch, iChar);
		}
		System.out.println(" sTotalValueKey key is ...." + sTotalValueKey);
		String sTotalCRCString = sTotalValueKey.substring(0, 2);
		int iTotalCRCInt = 0;
		try {
			System.out.println("TotalCRCString::" + sTotalCRCString);
			iTotalCRCInt = Integer.parseInt(sTotalCRCString);
		} catch (Exception e1) {
			System.out
					.println("Exception Caught at parseInt Please check for keys : sTotalCRCString"
							+ sTotalCRCString);
			e1.printStackTrace();
		}
		String sTotalCRCCheck = sTotalValueKey.substring(2,
				sTotalValueKey.length());
		StringBuffer szTotalCRCBuffer = new StringBuffer(sTotalCRCCheck);
		szTotalCRCBuffer.setLength(30);
		System.out.println("szTotalCRCBuffer is @@@@@" + szTotalCRCBuffer);
		int i = Encode_CRC(szTotalCRCBuffer);
		System.out.println(" FIRST CRC is..." + iTotalCRCInt
				+ "...CRC Count is..." + i);
		if (i != iTotalCRCInt) {
			System.out.println("CRC didn't match");
			return (N_FAILURE);
		} else {
			String sKey1Index = sTotalCRCCheck.substring(0, 15);
			String sKey2Index = sTotalCRCCheck.substring(15,
					sTotalCRCCheck.length());
			String sKey1CRCString = sKey1Index.substring(0, 2);
			System.out.println("Key Value at 617::::::" + sKey1CRCString);
			int iKey1CRCInt = Integer.parseInt(sKey1CRCString);
			String sKey1CRCCheckString = sKey1Index.substring(2,
					sKey1Index.length());
			StringBuffer sKey1CRCCheckBuffer = new StringBuffer(
					sKey1CRCCheckString);
			/*
			 * for (int ii = 0; ii < crc_temp.length(); ii++) { char ccheck =
			 * crc_temp.charAt(ii); }
			 */
			int j = Encode_CRC(sKey1CRCCheckBuffer);
			if (j != iKey1CRCInt) {
				System.out.println(" First CRC is not correct");
				return (N_FAILURE);
			} else {
				String sKey2CRCString = sKey2Index.substring(0, 2);
				int iKey2CRCInt = Integer.parseInt(sKey2CRCString);
				String sKey2CRCCheckString = sKey2Index.substring(2,
						sKey2Index.length());
				StringBuffer sKey2CRCCheckBuffer = new StringBuffer(
						sKey2CRCCheckString);
				int k = Encode_CRC(sKey2CRCCheckBuffer);
				if (k != iKey2CRCInt) {
					System.out.println(" Second CRC is not correct");
					return (N_FAILURE);
				}
				String sEncryKey = sKey1CRCCheckString + sKey2CRCCheckString;
				StringBuffer sEncryKeyBuffer = new StringBuffer(sEncryKey);
				sEncryKeyBuffer.setLength(27);
				StringBuffer temp1Buffer = dec(sEncryKeyBuffer);
				lpKeyInfo.szTotalKey = temp1Buffer;
			}
		}
		return 0;
	}
static int XXXValidateKeys ( GA_KEY_INFORMATION1 lpKeyInfo , int piErrCode ) { StringBuffer sTotalValueKey = new StringBuffer ( ) ; sTotalValueKey . setLength ( 36 ) ; String sTotal_Key = ( lpKeyInfo . szKey1 ) . toString ( ) + ( lpKeyInfo . szKey2 ) . toString ( ) ; System . out . println ( "Total_Key :::" + sTotal_Key ) ; System . out . println ( "Total_Key length :::" + sTotal_Key . length ( ) ) ; for ( int ch = 0 ; ch < sTotal_Key . length ( ) ; ch ++ ) { char iChar = sTotal_Key . charAt ( ch ) ; int intik = ( int ) iChar ; if ( ( intik <= 47 ) || ( intik >= 91 && intik <= 96 ) || ( intik >= 58 && intik <= 64 ) || ( intik >= 123 ) ) continue ; sTotalValueKey . setCharAt ( ch , iChar ) ; } System . out . println ( " sTotalValueKey key is ...." + sTotalValueKey ) ; String sTotalCRCString = sTotalValueKey . substring ( 0 , 2 ) ; int iTotalCRCInt = 0 ; try { System . out . println ( "TotalCRCString::" + sTotalCRCString ) ; iTotalCRCInt = Integer . parseInt ( sTotalCRCString ) ; } catch ( Exception e1 ) { System . out . println ( "Exception Caught at parseInt Please check for keys : sTotalCRCString" + sTotalCRCString ) ; e1 . printStackTrace ( ) ; } String sTotalCRCCheck = sTotalValueKey . substring ( 2 , sTotalValueKey . length ( ) ) ; StringBuffer szTotalCRCBuffer = new StringBuffer ( sTotalCRCCheck ) ; szTotalCRCBuffer . setLength ( 30 ) ; System . out . println ( "szTotalCRCBuffer is @@@@@" + szTotalCRCBuffer ) ; int i = Encode_CRC ( szTotalCRCBuffer ) ; System . out . println ( " FIRST CRC is..." + iTotalCRCInt + "...CRC Count is..." + i ) ; if ( i != iTotalCRCInt ) { System . out . println ( "CRC didn't match" ) ; return ( N_FAILURE ) ; } else { String sKey1Index = sTotalCRCCheck . substring ( 0 , 15 ) ; String sKey2Index = sTotalCRCCheck . substring ( 15 , sTotalCRCCheck . length ( ) ) ; String sKey1CRCString = sKey1Index . substring ( 0 , 2 ) ; System . out . println ( "Key Value at 617::::::" + sKey1CRCString ) ; int iKey1CRCInt = Integer . parseInt ( sKey1CRCString ) ; String sKey1CRCCheckString = sKey1Index . substring ( 2 , sKey1Index . length ( ) ) ; StringBuffer sKey1CRCCheckBuffer = new StringBuffer ( sKey1CRCCheckString ) ; /*
			 * for (int ii = 0; ii < crc_temp.length(); ii++) { char ccheck =
			 * crc_temp.charAt(ii); }
			 */ int j = Encode_CRC ( sKey1CRCCheckBuffer ) ; if ( j != iKey1CRCInt ) { System . out . println ( " First CRC is not correct" ) ; return ( N_FAILURE ) ; } else { String sKey2CRCString = sKey2Index . substring ( 0 , 2 ) ; int iKey2CRCInt = Integer . parseInt ( sKey2CRCString ) ; String sKey2CRCCheckString = sKey2Index . substring ( 2 , sKey2Index . length ( ) ) ; StringBuffer sKey2CRCCheckBuffer = new StringBuffer ( sKey2CRCCheckString ) ; int k = Encode_CRC ( sKey2CRCCheckBuffer ) ; if ( k != iKey2CRCInt ) { System . out . println ( " Second CRC is not correct" ) ; return ( N_FAILURE ) ; } String sEncryKey = sKey1CRCCheckString + sKey2CRCCheckString ; StringBuffer sEncryKeyBuffer = new StringBuffer ( sEncryKey ) ; sEncryKeyBuffer . setLength ( 27 ) ; StringBuffer temp1Buffer = dec ( sEncryKeyBuffer ) ; lpKeyInfo . szTotalKey = temp1Buffer ; } } return 0 ; }
Method XXXValidateKeys This method accepts GA_KEY_INFORMATION structure
 and validate the two sets of keys While validating it checks the CRC byte
 of each key and also CRC byte of whole key
Method XXXValidateKeys This method accepts GA_KEY_INFORMATION structure and validate the two sets of keys While validating it checks the CRC byte of each key and also CRC byte of whole key
// long lLicenses = 0l; // Get the two keys from the Key Object // Generate the Total Key by adding two keys // System.out.println(iChar); // System.out.println("intik is ASCII values ::" + intik); // Get the first two byte of total Key // Saparate the other 30 byte of key and generate CRC for that key // Compare generated CRC with key CRC and if they are incorrect then // send error // Divide the generated key into two keys of 14 byte each // Get the first two byte of first key // Generate the CRC for the other // String crc_temp = sKey1CRCCheckBuffer.toString(); // System.out.println(" First CRC is..."+j+"  for  "+sKey1CRCCheckBuffer); // System.out.println("Second CRC is..."+iKey1CRCInt+"...CRC Count is..."+j); // Compare the generated CRC with CRC of key // Get first two byte of second key // Generate the CRC of second key // System.out.println("Third CRC is..."+iKey2CRCInt+"...CRC Count is..."+k); // Compare the two CRC's // Get the concatedned string of two keys ( 26 byte string) // Decrypt the string // Original key will ge generated // System.out.println(" Original key is ...."+temp1Buffer);