c# - How to encrypt decrypt string using MACTripleDES? -


how encrypt , decrypt string using mactripledes in c#? there difference between mactripledes , tripledes?

mactripledes uses cbc-mac. cbc-mac uses cbc mode after padding message zeros. specified in withdrawn fips 113 specification (daa). last block kept:

cbc-mac

this means each , every block of plaintext data before cannot retrieved. is, unless know plaintext of last blocks, in case can xor last block, retrieve previous ciphertext, , calculate plaintext decryption.

tripledes in cbc mode on other hand outputs blocks of ciphertext, before using vector next block of plaintext.

using system; using system.security.cryptography;  namespace stackoverflow {     public class mactripledestest     {         public static void main(string[] args)         {             // example key             byte[] key = new byte[24];             (int = 0; < key.length; i++)             {                 key[i] = (byte) i;             }              // uses cbc mac 0 initialization vector , 0 padding             mactripledes mactdes = new mactripledes(key);             byte[] result = mactdes.computehash(new byte[] { 0x01, 0x02, 0x03, 0x04 });              tripledes tdes = new tripledescryptoserviceprovider();             tdes.key = key;             tdes.mode = ciphermode.ecb;             tdes.padding = paddingmode.none;             icryptotransform tf = tdes.createdecryptor();             byte[] pt = tf.transformfinalblock(result, 0, tdes.blocksize / 8);             console.writeline(bitconverter.tostring(pt));         }     } } 

which result in:

01-02-03-04-00-00-00-00 

cbc-mac rather insecure:

  • when used cbc mode encryption , same key fails completely
  • it not deliver full security dynamically sized messages
  • the 3des block size rather small

use either aes-cmac, hmac or authenticated encryption instead.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -