Skip to content

Instantly share code, notes, and snippets.

@soend
Created September 16, 2011 10:33
Show Gist options
  • Save soend/1221799 to your computer and use it in GitHub Desktop.
Save soend/1221799 to your computer and use it in GitHub Desktop.
var plainText = sha1_left16;
var cipherText = new byte[plainText.Length];
// The first 8 bytes must be the IV, then the data to be encrypted.
// get ready to encrypt
byte[] _params = new byte[8 + plainText.Length]; // 8 bytes IV vector
Array.Copy(cipherText, 0, _params, 8, plainText.Length);
// If the length of data to be encrypted by the CBC mechanism does not fit into one of the
// above PARAMS structures, the developer must produce their own byte array with the
// following layout. The first 8 bytes must be the IV, then the data to be encrypted. To
// use this array, the pParameter in the CK_MECHANISM structure must be a pointer to
// this array and the parameterLen is the length of the IV (must be 8 bytes) plus the
// length of the provided data, which must be a multiple of 8 bytes.
CK_MECHANISM m = new CK_MECHANISM();
m.mechanism = (uint)CKM.DES3_CBC;
IntPtr unmanagedPointer = Marshal.AllocHGlobal(_params.Length);
Marshal.Copy(_params, 0, unmanagedPointer, _params.Length);
m.pParameter = unmanagedPointer;
m.ulParameterLen = (uint) _params.Length;
aSession.EncryptInit(m, sec);
byte[] encData = aSession.Encrypt(plainText);
aSession.DecryptInit(m, sec);
byte[] data2 = aSession.Decrypt(encData.Take(16).ToArray());
Console.WriteLine((data2.ToHexString()));
aSession.FindObjectsFinal();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment