public override byte [] ComputeHash(byte [] buffer) { //TODO this isnt working on SP //IntPtr prov = OpenNETCF.Security.Cryptography.NativeMethods.Context.AcquireContext(); //byte [] baKey = (byte []) key.Clone(); //reversed below //IntPtr ipKey = OpenNETCF.Security.Cryptography.NativeMethods.Key.ImportSessionKey(prov, Calg.TRIP_DES, baKey, true); //hash = OpenNETCF.Security.Cryptography.NativeMethods.Hash.ComputeKeyedHash(prov, CalgHash.MAC, buffer, ipKey); //OpenNETCF.Security.Cryptography.NativeMethods.Key.DestroyKey(ipKey); //OpenNETCF.Security.Cryptography.NativeMethods.Context.ReleaseContext(prov); //return hash; //http://www.itl.nist.gov/fipspubs/fip81.htm - Appendix F int blocks = buffer.Length / 8; int rem = buffer.Length % 8; if (rem != 0) { blocks = blocks + 1; } byte [] padBuffer = new byte[blocks * 8]; Array.Copy(buffer, 0, padBuffer, 0, buffer.Length); //this leaves 0's at the end TripleDESCryptoServiceProvider tdcsp = new TripleDESCryptoServiceProvider(); tdcsp.Key = this.key; tdcsp.IV = new byte[8]; TripleDesNoPadding tdnp = new TripleDesNoPadding(tdcsp); byte [] cipher = tdnp.Encrypt(padBuffer); //#if NET_1_0 - from Mono // add an empty (zeros) block for MAC padding //byte[] emptyBlock = new byte [blockSize]; //result = enc.TransformFinalBlock (emptyBlock, 0, blockSize); hash = new byte[8]; Array.Copy(cipher, cipher.Length - 8, hash, 0, 8); return(hash); }
public static void Protect(byte[] userData, MemoryProtectionScope scope) { //IntPtr ip = Mem.CryptMemAlloc(memLen); //if(ip == IntPtr.Zero) // throw new Exception("memory not allocated"); //Marshal.Copy(userData, 0, ip, userData.Length); int memLen = userData.Length; if (memLen % 16 != 0) { throw new Exception("message length must be divisible by 16"); } byte [] randKey; if (scope == MemoryProtectionScope.SameProcess) { randKey = Cipher.UnprotectData(protectedKey); } else //sameLong, crossProcess { randKey = Cipher.UnprotectData(protectedKeyCross); } DESCryptoServiceProvider dcsp = new DESCryptoServiceProvider(); dcsp.Key = randKey; TripleDesNoPadding tdnp = new TripleDesNoPadding(dcsp); byte [] cipher = tdnp.Encrypt(userData); for (int i = 0; i < cipher.Length; i++) { userData[i] = cipher[i]; } //userData = Cipher.ProtectData(userData); //byte [] retBa = Cipher.ProtectData(userData); //Array.Clear(userData, 0, userData.Length); //userData = retBa; }
public static void Unprotect(byte[] encryptedData, MemoryProtectionScope scope) { //if(ip == IntPtr.Zero) // throw new Exception("memory not re-allocated"); //Mem.CryptMemFree(ip); int memLen = encryptedData.Length; if (memLen % 16 != 0) { throw new Exception("message length must be divisible by 16"); } byte [] randKey; if (scope == MemoryProtectionScope.SameProcess) { randKey = Cipher.UnprotectData(protectedKey); } else //sameLong, crossProcess { randKey = Cipher.UnprotectData(protectedKeyCross); } DESCryptoServiceProvider dcsp = new DESCryptoServiceProvider(); dcsp.Key = randKey; TripleDesNoPadding tdnp = new TripleDesNoPadding(dcsp); byte [] clear = tdnp.Decrypt(encryptedData); for (int i = 0; i < clear.Length; i++) { encryptedData[i] = clear[i]; } //encryptedData = Cipher.UnprotectData(encryptedData); //byte [] retBa = Cipher.UnprotectData(encryptedData); //Array.Clear(encryptedData, 0, encryptedData.Length); //encryptedData = retBa; }