/// <summary> /// Sign with only public key /// </summary> private static void CspSignPublic() { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.KeyPair = CryptoKey.CreateObject(rsa.Session, m_importKeyPublic) as CryptoKey; rsa.SignHash(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }, Cryptoki.MechanismType.SHA_1); } return; }
/// <summary> /// Sign without private key /// </summary> private static void CSPSignNoPrivate() { //DSACryptoServiceProvider dsaPriv = new DSACryptoServiceProvider(); //byte[] publicBlob = dsaPriv.ExportCspBlob(false); using (Session session = new Session("", MechanismType.DSA)) { CryptoKey key = CryptoKey.CreateObject(session, m_publicDsaKey) as CryptoKey; using (DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(key)) { //dsa.ImportCspBlob(publicBlob); dsa.SignData(new byte[] { 0, 1, 2, 3, 4, 5 }); } } }
public static MFTestResults Test_EncryptCsp(SymmetricAlgorithm csp1, SymmetricAlgorithm csp2, CryptokiAttribute[] keyTemplate) { bool testResult = false; try { string dataToEncrypt = "This is a simple message to be encrypted dfjdfh "; byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(dataToEncrypt); byte[] encData = null; byte[] newData = null; csp2.Key = CryptoKey.CreateObject(csp2.Session, keyTemplate) as CryptoKey; csp1.Key = CryptoKey.CreateObject(csp1.Session, keyTemplate) as CryptoKey; csp2.IV = csp1.IV; using (ICryptoTransform encr = csp1.CreateEncryptor()) { encData = encr.TransformFinalBlock(data, 0, data.Length); } using (ICryptoTransform decr = csp2.CreateDecryptor()) { newData = decr.TransformFinalBlock(encData, 0, encData.Length); } string res = new string(System.Text.UTF8Encoding.UTF8.GetChars(newData)); //Debug.Print(dataToEncrypt); //Debug.Print(res); testResult = string.Compare(dataToEncrypt, res) == 0; } catch (Exception ex) { Log.Exception("Unexpected Exception", ex); } return(testResult ? MFTestResults.Pass : MFTestResults.Fail); }