MAC 3DES
[Hashing Home][Home]
MACs can be used to authenticate a message, as a key is required to determine the hash value. With this the sender computes the hash with a secret key, and sends to the receiver, where the receive then calculates the hash of the message, and uses the secret key. If the hash received is the same as the computed one, the message has not been tampered with. The key size can be 8, 16 or 24 bytes. In this case, 24 bytes are used, and produces a hash of 8 bytes (64 bits), using the TripleDES encryption method.
The results are then:
|
Sample Code
using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Crypto.Paddings; public void checkMac3Des(string message, bool hex, string k) { byte[] data = null; if (message == null) return; int blocksize = 24; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); if (hex == false) data = encoding.GetBytes(message); else data = StringToByteArray(message); byte[] key_byte = new byte[blocksize]; byte [] temp = System.Text.Encoding.ASCII.GetBytes(k); for (int val = 0; val < temp.Length; val++) key_byte[val] = temp[val]; MACTripleDES mac = new MACTripleDES(key_byte); try { byte[] res = mac.ComputeHash(data); mac3des = Global.ByteToString(res); } catch (Exception ex) { mac3des = ex.Message; return; } }