public override bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature) { bool valid = true; IntPtr prov = IntPtr.Zero; IntPtr hash = IntPtr.Zero; IntPtr rsaKey = IntPtr.Zero; try { byte [] tempHash = (byte [])rgbHash.Clone(); byte [] tempSig = (byte [])rgbSignature.Clone(); prov = Context.AcquireContext("dSaContainerImp", ProvName.MS_ENH_DSS_DH_PROV, ProvType.DSS_DH, ContextFlag.NONE); hash = GetHashAlgorithm(prov, str); //Hash.HashData(hash, tempData); Hash.SetHashParam(hash, HashParam.HASHVAL, tempHash); rsaKey = Key.ImportKey(prov, _dsa.rawKey, IntPtr.Zero, GenKeyParam.NONE); //EXPORTABLE wont work here Array.Reverse(tempSig, 0, 20); //r Array.Reverse(tempSig, 20, 20); //s Hash.VerifySignature(hash, tempSig, rsaKey); } catch (Exception) { valid = false; } finally { Key.DestroyKey(rsaKey); Hash.DestroyHash(hash); Context.ReleaseContext(prov); } return(valid); }
public DSACryptoServiceProvider(bool genKey) { IntPtr prov = IntPtr.Zero; IntPtr key = IntPtr.Zero; //for bad key state //Crypto.CryptAcquireContext(out prov, "dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, (uint)ProvType.DSS_DH, (uint)ContextFlag.DELETEKEYSET); //Crypto.CryptAcquireContext(out prov, "dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, (uint)ProvType.DSS_DH, (uint)ContextFlag.NEWKEYSET); try { if (genKey == false) //faster { prov = Context.AcquireContext("dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, ProvType.DSS_DH, ContextFlag.NONE); key = Key.GetUserKey(prov, KeySpec.SIGNATURE); } else //create new one { prov = Context.AcquireContext("dSaContainerImp", ProvName.MS_ENH_DSS_DH_PROV, ProvType.DSS_DH, ContextFlag.NONE); key = Key.GenKey(prov, Calg.DSS_SIGN, GenKeyParam.EXPORTABLE); } byte [] baPriKey = Key.ExportKey(key, IntPtr.Zero, KeyBlob.PRIVATEKEYBLOB); byte [] baPubKey = Key.ExportKey(key, IntPtr.Zero, KeyBlob.PUBLICKEYBLOB); _dsa = new Dsa(baPriKey); //X is returned Dsa dsaPub = new Dsa(baPubKey); //Y is only from public call _dsa.Y = (byte[])dsaPub.Y.Clone(); } finally { Key.DestroyKey(key); Context.ReleaseContext(prov); } }
public RSACryptoServiceProvider(KeySpec keySpec, bool genKey) { IntPtr prov = IntPtr.Zero; IntPtr rsaKey = IntPtr.Zero; try { this.keySpec = keySpec; if (genKey == false) //faster { prov = Context.AcquireContext("rSaContainer"); rsaKey = Key.GetUserKey(prov, keySpec); } else //new key { prov = Context.AcquireContext("rSaContainerImp"); Calg calg = Calg.RSA_KEYX; if (keySpec == KeySpec.SIGNATURE) { calg = Calg.RSA_SIGN; } rsaKey = Key.GenKey(prov, calg, GenKeyParam.EXPORTABLE); } byte [] baPrivKey = Key.ExportKey(rsaKey, IntPtr.Zero, KeyBlob.PRIVATEKEYBLOB); _rsa = new Rsa(baPrivKey); } finally { Key.DestroyKey(rsaKey); Context.ReleaseContext(prov); } }
public override byte[] SignHash(byte[] rgbHash, string str) { IntPtr prov = IntPtr.Zero; IntPtr hash = IntPtr.Zero; IntPtr rsaKey = IntPtr.Zero; byte [] sig; try { byte [] tempHash = (byte[])rgbHash.Clone(); prov = Context.AcquireContext("dSaContainerImp", ProvName.MS_ENH_DSS_DH_PROV, ProvType.DSS_DH, ContextFlag.NONE); hash = GetHashAlgorithm(prov, str); //Hash.HashData(hash, tempData); Hash.SetHashParam(hash, HashParam.HASHVAL, tempHash); rsaKey = Key.ImportKey(prov, _dsa.rawKey, IntPtr.Zero, GenKeyParam.EXPORTABLE); sig = Hash.SignHash(hash, KeySpec.SIGNATURE); Array.Reverse(sig, 0, 20); //r Array.Reverse(sig, 20, 20); //s } finally { Key.DestroyKey(rsaKey); Hash.DestroyHash(hash); Context.ReleaseContext(prov); } return(sig); }
public override bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature) { bool valid = true; IntPtr prov = IntPtr.Zero; IntPtr hash = IntPtr.Zero; IntPtr rsaKey = IntPtr.Zero; try { byte [] tempHash = (byte [])rgbHash.Clone(); byte [] tempSig = (byte [])rgbSignature.Clone(); prov = Context.AcquireContext("rSaContainerImp"); hash = GetHashAlgorithm(prov, str); //Hash.HashData(hash, tempData); Hash.SetHashParam(hash, HashParam.HASHVAL, tempHash); rsaKey = Key.ImportKey(prov, _rsa.rawKey, IntPtr.Zero, GenKeyParam.NONE); //EXPORTABLE Array.Reverse(tempSig, 0, tempSig.Length); Hash.VerifySignature(hash, tempSig, rsaKey); } catch (Exception ex) { string sex = ex.ToString(); valid = false; } finally { Key.DestroyKey(rsaKey); Hash.DestroyHash(hash); Context.ReleaseContext(prov); } return(valid); }
public override byte[] SignHash(byte[] rgbHash, string str) { IntPtr prov = IntPtr.Zero; IntPtr hash = IntPtr.Zero; IntPtr rsaKey = IntPtr.Zero; byte [] sig; try { byte [] tempHash = (byte[])rgbHash.Clone(); prov = Context.AcquireContext("rSaContainerImp"); hash = GetHashAlgorithm(prov, str); //Hash.HashData(hash, tempData); Hash.SetHashParam(hash, HashParam.HASHVAL, tempHash); rsaKey = Key.ImportKey(prov, _rsa.rawKey, IntPtr.Zero, GenKeyParam.EXPORTABLE); sig = Hash.SignHash(hash, keySpec); //KeySpec.EXCHANGE? Array.Reverse(sig, 0, sig.Length); } finally { Key.DestroyKey(rsaKey); Hash.DestroyHash(hash); Context.ReleaseContext(prov); } return(sig); }
public MACTripleDES() { IntPtr prov = Context.AcquireContext(); IntPtr ipKey = OpenNETCF.Security.Cryptography.Internal.Key.GenKey(prov, Calg.TRIP_DES, GenKeyParam.EXPORTABLE); key = OpenNETCF.Security.Cryptography.Internal.Key.ExportSessionKey(prov, ipKey, 24, true); //reversed above OpenNETCF.Security.Cryptography.Internal.Key.DestroyKey(ipKey); OpenNETCF.Security.Cryptography.Internal.Context.ReleaseContext(prov); }
public TripleDESCryptoServiceProvider() { IntPtr prov = IntPtr.Zero; IntPtr ipKey = IntPtr.Zero; try { prov = Context.AcquireContext("sYmContainer"); ipKey = OpenNETCF.Security.Cryptography.NativeMethods.Key.GenKey(prov, Calg.TRIP_DES, GenKeyParam.EXPORTABLE); this.Key = OpenNETCF.Security.Cryptography.NativeMethods.Key.ExportSessionKey(prov, ipKey, 24, true); this.IV = OpenNETCF.Security.Cryptography.NativeMethods.Key.GetIv(ipKey); } finally { OpenNETCF.Security.Cryptography.NativeMethods.Key.DestroyKey(ipKey); Context.ReleaseContext(prov); } }
//non-standard public override byte[] EncryptValue(byte[] pBuff) { IntPtr prov = IntPtr.Zero; IntPtr ipKey = IntPtr.Zero; byte [] cBuff; try { prov = Context.AcquireContext("sYmContainer"); ipKey = OpenNETCF.Security.Cryptography.NativeMethods.Key.ImportSessionKey(prov, OpenNETCF.Security.Cryptography.NativeMethods.Calg.TRIP_DES, this.Key, true); OpenNETCF.Security.Cryptography.NativeMethods.Key.SetIv(ipKey, this.IV); cBuff = OpenNETCF.Security.Cryptography.NativeMethods.Cipher.Encrypt(ipKey, IntPtr.Zero, pBuff); } finally { OpenNETCF.Security.Cryptography.NativeMethods.Key.DestroyKey(ipKey); Context.ReleaseContext(prov); } return(cBuff); }
//non-standard public override byte[] DecryptValue(byte[] cBuff) { IntPtr prov = IntPtr.Zero; IntPtr ipKey = IntPtr.Zero; byte [] pBuff; try { prov = Context.AcquireContext("sYmContainer"); ipKey = OpenNETCF.Security.Cryptography.Internal.Key.ImportSessionKey(prov, Calg.DES, this.Key, true); OpenNETCF.Security.Cryptography.Internal.Key.SetIv(ipKey, this.IV); pBuff = Cipher.Decrypt(ipKey, IntPtr.Zero, cBuff); } finally { OpenNETCF.Security.Cryptography.Internal.Key.DestroyKey(ipKey); Context.ReleaseContext(prov); } return(pBuff); }
public override byte[] DecryptValue(byte[] cBuff) { IntPtr prov = IntPtr.Zero; IntPtr rsaKey = IntPtr.Zero; byte [] pBuff; try { prov = Context.AcquireContext("rSaContainerImp"); rsaKey = Key.ImportKey(prov, _rsa.rawKey, IntPtr.Zero, GenKeyParam.EXPORTABLE); byte [] tempBuff = (byte [])cBuff.Clone(); Array.Reverse(tempBuff, 0, tempBuff.Length); pBuff = Cipher.Decrypt(rsaKey, IntPtr.Zero, tempBuff); } finally { Key.DestroyKey(rsaKey); Context.ReleaseContext(prov); } return(pBuff); }
/// <summary> /// Derives a cryptographic key from the PasswordDeriveBytes object. /// </summary> /// <remarks> /// If the keySize parameter is set to 0, the default key size for the specified algorithm is used. /// </remarks> /// <param name="algName">The algorithm name for which to derive the key. </param> /// <param name="algHashName">The hash algorithm name to use to derive the key. </param> /// <param name="keySize">The size of the key to derive. </param> /// <param name="IV">The initialization vector (IV) to use to derive the key.</param> /// <returns>The derived key.</returns> public byte [] CryptDeriveKey(string algName, string algHashName, int keySize, byte [] IV) { //RC2 / SHA1 works //TODO DES / MD5 seems to be salted //TODO not using salt, IV, or keySize IntPtr prov = Context.AcquireContext(); byte [] baPassword = Format.GetBytes(this.password); IntPtr hash = GetHashAlgorithm(prov, algHashName); Hash.HashData(hash, baPassword); IntPtr key = GetKeyAlgorithm(prov, hash, algName); int keyLen = Key.GetKeyLength(key) / 8; byte [] baKey = Key.ExportSessionKey(prov, key, keyLen, true); Hash.DestroyHash(hash); Key.DestroyKey(key); Context.ReleaseContext(prov); return(baKey); }