public void AES_ECB_decrypt(ref AES_ctx ctx, ref uint[] buf) { // The next function call decrypts the PlainText with the Key using AES algorithm. uint k = 0; Array.Clear(state_t.state_t, 0, 16); for (int j = 0; j < 4 && k < buf.Length; j++) { for (int i = 0; i < 4 && k < buf.Length; i++, k++) { state_t.state_t[j, i] = buf[k]; } } InvCipher(ref state_t, ref ctx.RoundKey); }
void AES_ctx_set_iv(ref AES_ctx ctx, ref uint[] iv) { Array.Copy(ctx.Iv, iv, AES_BLOCKLEN); }
void AES_init_ctx_iv(ref AES_ctx ctx, ref uint[] key, ref uint[] iv) { KeyExpansion(ref ctx, ref key); Array.Copy(iv, ctx.Iv, AES_BLOCKLEN); }
public void AES_init_ctx(ref AES_ctx ctx, ref uint[] key) { KeyExpansion(ref ctx, ref key); }
// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. private void KeyExpansion(ref AES_ctx RoundKey, ref uint[] Key) { uint i, j, k; uint[] tempa = new uint[4]; // Used for the column/row operations // The first round key is the key itself. for (i = 0; i < Nk; ++i) { RoundKey.RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; RoundKey.RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; RoundKey.RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; RoundKey.RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; } // All other round keys are found from the previous round keys. for (i = Nk; i < Nb * (Nr + 1); ++i) { { k = (i - 1) * 4; tempa[0] = RoundKey.RoundKey[k + 0]; tempa[1] = RoundKey.RoundKey[k + 1]; tempa[2] = RoundKey.RoundKey[k + 2]; tempa[3] = RoundKey.RoundKey[k + 3]; } if (i % Nk == 0) { // This function shifts the 4 bytes in a word to the left once. // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] // Function RotWord() { k = tempa[0]; tempa[0] = tempa[1]; tempa[1] = tempa[2]; tempa[2] = tempa[3]; tempa[3] = k; } // SubWord() is a function that takes a four-byte input word and // applies the S-box to each of the four bytes to produce an output word. // Function Subword() { tempa[0] = getSBoxValue(tempa[0]); tempa[1] = getSBoxValue(tempa[1]); tempa[2] = getSBoxValue(tempa[2]); tempa[3] = getSBoxValue(tempa[3]); } tempa[0] = tempa[0] ^ Rcon[i / Nk]; } #if AES256 if (i % Nk == 4) { // Function Subword() { tempa[0] = getSBoxValue(tempa[0]); tempa[1] = getSBoxValue(tempa[1]); tempa[2] = getSBoxValue(tempa[2]); tempa[3] = getSBoxValue(tempa[3]); } } #endif j = i * 4; k = (i - Nk) * 4; RoundKey.RoundKey[j + 0] = RoundKey.RoundKey[k + 0] ^ tempa[0]; RoundKey.RoundKey[j + 1] = RoundKey.RoundKey[k + 1] ^ tempa[1]; RoundKey.RoundKey[j + 2] = RoundKey.RoundKey[k + 2] ^ tempa[2]; RoundKey.RoundKey[j + 3] = RoundKey.RoundKey[k + 3] ^ tempa[3]; } }
public void EncryptFile(string inputFilename, string outputFilename, string firmwareRevisionFilename, string newKey) { Aes aes = new Aes(); if (!File.Exists(inputFilename)) { Console.WriteLine("{0} {1}", inputFilename, " file does not exist!"); return; } if (File.Exists(outputFilename)) { File.Delete(outputFilename); } if (firmwareRevisionFilename != "") { string revisionName = ""; revisionName = GetVersionNumberFromFile(firmwareRevisionFilename); if (outputFilename.Contains(".crx")) { outputFilename = outputFilename.Insert(outputFilename.IndexOf(".crx"), "_" + revisionName + "_"); } else { Console.WriteLine("The output file name extension is not valid. The extension should end in \".crx\""); return; } } FileStream fsOut = File.Create(outputFilename); FileStream fsIn = File.OpenRead(inputFilename); StreamReader sr = new StreamReader(fsIn); ulong fileSize = (ulong)fsIn.Length; // todo: final filesize. Needs to be in increments of 16 bytes. ulong finalRemainder = (16 - (fileSize % 16)); // find how many bytes left then subtract from 16 to get value to add to current file size. fileSize += finalRemainder; // add how many bytes to current file size UInt16 lastKeys = (UInt16)(fileSize); // now get the low 2 byte value from fize size. // Create your own 128 bit key. This should be the same key as the decryption key uint[] key = new uint[16] { 0xA4, 0xDD, 0xE2, 0x16, 0x63, 0x51, 0x4A, 0x4D, 0xB1, 0xAB, 0x0E, 0x5D, 0x2C, 0xE2, 0xD7, 0xB7 }; // new guid (A4DDE216-6351-4A4D-B1AB-0E5D2CE2D7B7) if (!newKey.Equals("")) // if a GUID was included then replace default GUID. Also I do not have error checking implemeted so an invalid GUID will more than likely cause a crash. { newKey = newKey.Replace("-", ""); // some GUId may have dashes, so this will remove them. byte[] aKey = StrToByteArray(newKey); Array.Copy(aKey, key, key.Length); } // changing last two bytes of key depending on file size. Doing this will make the encryption key random for each hex file. key[14] = (uint)lastKeys >> 8; key[15] = (uint)lastKeys & 0xff; string getLine = string.Empty; string temp_line = string.Empty; AES_ctx ctx = new AES_ctx(); aes.AES_init_ctx(ref ctx, ref key); char[] charArray = new char[fileSize]; //read all chars from file into array for (ulong i = 0; i < fileSize; i++) { charArray[i] = (char)sr.Read(); } uint[] tempArray = new uint[16]; // new array ulong charCounter = 0; while (charCounter < fileSize) { Array.Clear(tempArray, 0, tempArray.Length); Array.Copy(charArray, (int)charCounter, tempArray, 0, 16); aes.AES_ECB_encrypt(ref ctx, ref tempArray); byte[] byteArray = new byte[16]; uint k = 0; for (int j = 0; j < 4; j++) { for (uint i = 0; i < 4; i++, k++) { byteArray[k] = (byte)aes.state_t.state_t[j, i]; } } // write to the new file fsOut.Write(byteArray, 0, byteArray.Length); charCounter += 16; } fsIn.Close(); fsOut.Close(); Console.WriteLine(Path.GetFileName(inputFilename) + " has been encrypted successfully! New filename: " + Path.GetFileName(outputFilename)); }
public void DecryptFile(string inputFilename, string outputFilename, string newKey) { Aes aes = new Aes(); if (!File.Exists(inputFilename)) { Console.WriteLine("{0} {1}", inputFilename, " file does not exist!"); return; } if (File.Exists(outputFilename)) { File.Delete(outputFilename); } FileStream fsOut = File.Create(outputFilename); FileStream fsIn = File.OpenRead(inputFilename); StreamReader sr = new StreamReader(fsIn); ulong fileSize = (ulong)fsIn.Length; string myFile = string.Empty; // Create your own 128 bit key. This should be the same as the encryption key uint[] key = new uint[16] { 0xA4, 0xDD, 0xE2, 0x16, 0x63, 0x51, 0x4A, 0x4D, 0xB1, 0xAB, 0x0E, 0x5D, 0x2C, 0xE2, 0xD7, 0xB7 }; // new guid (A4DDE216-6351-4A4D-B1AB-0E5D2CE2D7B7) if (!newKey.Equals("")) { newKey = newKey.Replace("-", ""); byte[] aKey = StrToByteArray(newKey); Array.Copy(aKey, key, key.Length); } UInt16 lastKeys = (UInt16)(fileSize); key[14] = (uint)lastKeys >> 8; key[15] = (uint)lastKeys & 0xff; // changing last two bytes of key depending on file size. //aes.Phex(ref key, 0); byte[] fileBytes = new byte[fsIn.Length]; int numBytesToRead = (int)fsIn.Length; int numBytesRead = 0; while (numBytesToRead > 0) { // Read may return anything from 0 to numBytesToRead. int n = fsIn.Read(fileBytes, numBytesRead, numBytesToRead); // Break when the end of the file is reached. if (n == 0) { break; } numBytesRead += n; numBytesToRead -= n; } numBytesToRead = fileBytes.Length; // reload size fsIn.Close(); // done with file AES_ctx ctx = new AES_ctx(); aes.AES_init_ctx(ref ctx, ref key); numBytesRead = 0; while (numBytesRead < numBytesToRead) { uint[] tempArray = new uint[16]; // new temp array Array.Copy(fileBytes, numBytesRead, tempArray, 0, 16); // copy 16 bytes at a time aes.AES_ECB_decrypt(ref ctx, ref tempArray); // decrypt 16 bytes byte[] byteArray = new byte[16]; Array.Clear(byteArray, 0, byteArray.Length); uint k = 0; for (int j = 0; j < 4; j++) { for (uint i = 0; i < 4; i++, k++) { byteArray[k] = (byte)((char)aes.state_t.state_t[j, i]); } } myFile += Encoding.Default.GetString(byteArray); // keep adding to string numBytesRead += 16; // inc the read pointer } byte myByte = 0; numBytesRead = 0; // the counter for the file size foreach (char c in myFile) { myByte = (byte)Convert.ToByte(c); if (myByte <= 127) // check if ascii char 0-127 { fsOut.WriteByte(myByte); // write whole string to file numBytesRead += 1; } if (numBytesRead == numBytesToRead) { break; } } Console.WriteLine("The filesize: " + numBytesRead); // this is what we need to do for the Firmware updater app to send to bootloader. fsOut.Close(); // close file Console.WriteLine(Path.GetFileName(inputFilename) + " has been decrypted successfully! New filename: " + Path.GetFileName(outputFilename)); }