// Decryption Routine: Base64 -> AES -> Zlib private static byte[] Decrypt(uint uVer, uint uLen, uint uLenCompressed, uint dwBufferFlag, byte[] pSrc) { byte[] bits = BitConverter.GetBytes(dwBufferFlag); if (!((bits[3] & 1) != 0)) { byte[] aKey; byte[] aIV; // Get the AES Key/IV for transformation CipherKeys.GetKeyAndIV(uVer, uLenCompressed, out aKey, out aIV); // Decode the base64 encoded string pSrc = Convert.FromBase64String(Encoding.UTF8.GetString(pSrc)); // Decrypt the AES encrypted block AESCipher pCipher = new AESCipher(aKey, aIV); pCipher.TransformBlock(pSrc, 0, uLen, pSrc, 0); } else { // Decrypt the XOR encrypted block pSrc = EncryptXOR(uVer, pSrc, uLen, uLenCompressed); } if (bits[0] != 0) { return(ZlibStream.UncompressBuffer(pSrc)); } return(pSrc); }
private static byte[] EncryptXOR(uint uVer, byte[] pSrc, uint uLen, uint uLenCompressed) { byte[] aKey; CipherKeys.GetXORKey(uVer, out aKey); uint uBlock = uLen >> 2; uint uBlockOffset = 0; int nKeyOffset = 0; if (uBlock != 0) { while (uBlockOffset < uBlock) { /* * _begin: * mov eax, [ebp+pKey] * mov eax, [eax+edx*4] * xor [ebx+ecx*4], eax * inc edx * inc ecx * and edx, 1FFh * cmp ecx, esi * jb _begin * _end: * mov eax, [ebp+uLen] */ uint pBlockData = BitConverter.ToUInt32(pSrc, (int)(4 * uBlockOffset)) ^ BitConverter.ToUInt32(aKey, 4 * nKeyOffset); Buffer.BlockCopy(BitConverter.GetBytes(pBlockData), 0, pSrc, (int)(4 * uBlockOffset), sizeof(uint)); nKeyOffset = ((ushort)nKeyOffset + 1) & 0x1FF; uBlockOffset++; } } uBlock = (uLen & 3); if (uBlock != 0) { int nStart = (int)(4 * uBlockOffset); uBlockOffset = 0; nKeyOffset = 0; while (uBlockOffset < uBlock) { pSrc[nStart + uBlockOffset++] ^= (byte)(aKey[nKeyOffset]); nKeyOffset = ((ushort)nKeyOffset + 1) & 0x7FF; } } return(pSrc); }
internal static string Hmac(string plainText, CipherKeys keys) { var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(plainText)); var sessionKeyBytes = GetSessionKeyBytes(keys); using (var hmacsha256 = new HMACSHA256(sessionKeyBytes)) { byte[] data = Encoding.UTF8.GetBytes(base64String); byte[] hashmessage = hmacsha256.ComputeHash(data); return(Convert.ToBase64String(hashmessage)); } }
internal static byte[] GetRekBytes(string rek, CipherKeys keys) { byte[] dataToDecrypt = Convert.FromBase64String(rek); AesManaged tdes = new AesManaged { KeySize = 256, BlockSize = 128, Key = GetSessionKeyBytes(keys), Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform decrypt = tdes.CreateDecryptor(); byte[] deCipher = decrypt.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length); tdes.Clear(); return(deCipher); }
//track return status api public static Response TrackReturnStatus(string username, string authToken, CipherKeys keys, string gstin, string returnPeriod, string returnType) { var queryString = new NameValueCollection { { "action", "RETTRACK" }, { "gstin", gstin }, { "fy", returnPeriod }, { "type", returnType } }; var handler = new RequestHandler(); handler.UserRequest.Keys = keys; handler.UserRequest.Header.Username = username; handler.UserRequest.Header.AuthToken = authToken; var url = UrlHandler.Route(accessGroup.commonapi, version.v1_0, modName.returns); return(handler.DecryptGetResponse(url, queryString)); }
// Encryption Routine: Zlib -> AES -> Base64 public static byte[] Encrypt(uint uVer, byte[] pSrc, uint dwBufferFlag, out uint uLen, out uint uLenCompressed, out uint uLenEncoded) { byte[] bits = BitConverter.GetBytes(dwBufferFlag); byte[] pEncrypted; if (bits[0] != 0) { pEncrypted = ZlibStream.CompressBuffer(pSrc); } else { pEncrypted = new byte[pSrc.Length]; Buffer.BlockCopy(pSrc, 0, pEncrypted, 0, pSrc.Length); } uLen = (uint)pSrc.Length; uLenCompressed = (uint)pEncrypted.Length; if (!((bits[3] & 1) != 0)) { byte[] aKey; byte[] aIV; // Get the AES Key/IV for transformation CipherKeys.GetKeyAndIV(uVer, uLenCompressed, out aKey, out aIV); // Perform AES block encryption AESCipher pCipher = new AESCipher(aKey, aIV); pCipher.TransformBlock(pEncrypted, 0, uLen, pEncrypted, 0); // Encode the encrypted data into a base64 encoded string pEncrypted = Encoding.UTF8.GetBytes(Convert.ToBase64String(pEncrypted)); } else { // Perform XOR block encryption pEncrypted = EncryptXOR(uVer, pEncrypted, uLen, uLenCompressed); } uLenEncoded = (uint)pEncrypted.Length; return(pEncrypted); }
private static byte[] EncryptXor(PackVersion version, byte[] src, uint size, uint sizeCompressed) { CipherKeys.GetXorKey(version, out byte[] key); uint uBlock = size >> 2; uint uBlockOffset = 0; int nKeyOffset = 0; if (uBlock != 0) { while (uBlockOffset < uBlock) { uint pBlockData = BitConverter.ToUInt32(src, (int)(4 * uBlockOffset)) ^ BitConverter.ToUInt32(key, 4 * nKeyOffset); Buffer.BlockCopy(BitConverter.GetBytes(pBlockData), 0, src, (int)(4 * uBlockOffset), sizeof(uint)); nKeyOffset = ((ushort)nKeyOffset + 1) & 0x1FF; uBlockOffset++; } } uBlock = (size & 3); if (uBlock != 0) { int nStart = (int)(4 * uBlockOffset); uBlockOffset = 0; nKeyOffset = 0; while (uBlockOffset < uBlock) { src[nStart + uBlockOffset++] ^= (byte)(key[nKeyOffset]); nKeyOffset = ((ushort)nKeyOffset + 1) & 0x7FF; } } return(src); }
// Decryption Routine: Base64 -> AES -> Zlib private static byte[] Decrypt(PackVersion version, uint size, uint sizeCompressed, Encryption flag, byte[] src) { if (flag.HasFlag(Encryption.Aes)) { // Get the AES Key/IV for transformation CipherKeys.GetKeyAndIV(version, sizeCompressed, out byte[] key, out byte[] iv); // Decode the base64 encoded string src = Convert.FromBase64String(Encoding.UTF8.GetString(src)); // Decrypt the AES encrypted block AESCipher pCipher = new AESCipher(key, iv); pCipher.TransformBlock(src, 0, size, src, 0); } else if (flag.HasFlag(Encryption.Xor)) { // Decrypt the XOR encrypted block src = EncryptXor(version, src, size, sizeCompressed); } return(flag.HasFlag(Encryption.Zlib) ? ZlibStream.UncompressBuffer(src) : src); }
// Encryption Routine: Zlib -> AES -> Base64 public static byte[] Encrypt(PackVersion version, byte[] src, Encryption flag, out uint size, out uint sizeCompressed, out uint sizeEncoded) { byte[] pEncrypted; if (flag.HasFlag(Encryption.Zlib)) { pEncrypted = ZlibStream.CompressBuffer(src); } else { pEncrypted = new byte[src.Length]; Buffer.BlockCopy(src, 0, pEncrypted, 0, src.Length); } size = (uint)src.Length; sizeCompressed = (uint)pEncrypted.Length; if (flag.HasFlag(Encryption.Aes)) { // Get the AES Key/IV for transformation CipherKeys.GetKeyAndIV(version, sizeCompressed, out byte[] key, out byte[] iv); // Perform AES block encryption var pCipher = new AESCipher(key, iv); pCipher.TransformBlock(pEncrypted, 0, size, pEncrypted, 0); // Encode the encrypted data into a base64 encoded string pEncrypted = Encoding.UTF8.GetBytes(Convert.ToBase64String(pEncrypted)); } else if (flag.HasFlag(Encryption.Xor)) { // Perform XOR block encryption pEncrypted = EncryptXor(version, pEncrypted, size, sizeCompressed); } sizeEncoded = (uint)pEncrypted.Length; return(pEncrypted); }
internal static string EncryptTextWithSessionKey(string plainText, CipherKeys keys) { var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(plainText)); byte[] dataToEncrypt = Encoding.UTF8.GetBytes(base64String); AesManaged tdes = new AesManaged { KeySize = 256, BlockSize = 128, Key = GetSessionKeyBytes(keys), Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform crypt = tdes.CreateEncryptor(); byte[] cipher = crypt.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length); tdes.Clear(); return(Convert.ToBase64String(cipher, 0, cipher.Length)); }
internal static string DecryptResponseData(string dataToDecrypt, string rek, CipherKeys keys) { byte[] dataToDecryptBytes = Convert.FromBase64String(dataToDecrypt); AesManaged tdes = new AesManaged { KeySize = 256, BlockSize = 128, Key = GetRekBytes(rek, keys), Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform decrypt = tdes.CreateDecryptor(); byte[] deCipher = decrypt.TransformFinalBlock(dataToDecryptBytes, 0, dataToDecryptBytes.Length); tdes.Clear(); var deCipherData = Encoding.UTF8.GetString(deCipher); var json = Encoding.UTF8.GetString(Convert.FromBase64String(deCipherData)); return(json); }