## Cryptography with .Net = Cryptography with .Net = ~-<>-~ <> == Overview == This page contains examples of the most common crypting tasks. It is probably the easiest way to find out about how to use Cryptography with .Net. For a complete reference see Wikipedia. == Math == The mathematics involved in cryptography are not difficult to grasp. It is the implementation which is more difficult, specially with languages that don't offer extended precision arithmetic.<
> .Net offers a series of classes to handle cryptography. They reside in System.Security.Cryptography which is in System.Core.dll == APL Code == Here is a namespace containing a few utilities and a HASH function: === Hash funtion === {{{ :Namespace CryptoTools ∇ r←DNetCrypto ⍝ This is the location of the cryptography libraries r←'System.Security.Cryptography,\Program Files' ⍝ change for 64b OS r,←'\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll' ∇ ∇ value←hash string;str;⎕USING ⍝ Return hash value of the string given as arg :If isChar str←,string str←⎕UCS str ⍝ turn characters into numbers for the hash fn {}⎕DR str ⍝ kludge for V12 to ensure str is small int :EndIf ⎕USING←DNetCrypto value←(⎕NEW SHA256Managed).ComputeHash⊂str ⍝ also SHA1/384/512, CRC32 & MD5CryptoServiceProvider ∇ if←/⍨ ⋄ UTF8←'UTF-8'∘⎕ucs ⋄ isChar←{∨/0 2=10|⎕DR 1/⍵} UCSN←{~isChar ⍵:⍵ ⋄ v{⍺}⎕dr v←UTF8 ⍵} ⍝ ⎕DR forces demotion (for V12) :EndNamespace }}} The hash function (here SHA256, but it could be another) can be applied to any string: {{{ CryptoTools.hash ⊃,/⎕src CryptoTools 165 157 223 218 183 249 78 22… }}} === Symmetric encryption === The following class will handle symmetric cryptography: {{{ :Class CodeSymmetric :include CryptoTools M2MS←{8÷⍨⍵[2]+0,s×⍳(-/2↑⍵)÷1⌈s←¯1↑⍵} ⍝ valid Key sizes [min-max] ∇ boa provider;choices;msg;n :Access public :Implements constructor ⍝ Data Encryption Standard (DES) supports a 64 bit key only ⍝ Rivest Cipher 2 provider supports keys from 40 to 128* bits ⍝ Rijndael (also known as AES) provider supports keys 128/192/256* ⍝ TripleDES provider (also known as 3DES) supports keys of 128/192* choices←'DES' 'RC2' 'Rijndael' 'TripleDES' msg←'Invalid provider; choose one of',⍕choices msg ⎕SIGNAL 99 if(⍴choices)> Vector also has been known to have articles on cryptography. ---- CategoryDotNet - CategoryDyalogDotNet - CategoryDyalogExamplesDotNet