/// <summary> /// Décode une chaine en base64 /// </summary> /// <param name="b64_encrypted_data">texte encodé</param> /// <param name="buf">résultat du décodage</param> /// <returns>0 si succès, != 0 si erreur</returns> private int nss_decode(string b64_encrypted_data, out string buf) { //valeur de retour int retval = 0; //texte déchiffré SECItem text = new SECItem(); //texte décodé mais crypté IntPtr ok = IntPtr.Zero; buf = string.Empty; /* input was base64 encoded. Decode it. */ ok = NSSBase64_DecodeBuffer(IntPtr.Zero, IntPtr.Zero, b64_encrypted_data, b64_encrypted_data.Length); if (ok == IntPtr.Zero) { retval = -1; } else { //récupère le SECItem pointé text = (SECItem)Marshal.PtrToStructure(ok, typeof(SECItem)); //récupère la chaine contenue dans le SECItem buf = Marshal.PtrToStringAnsi(text.data, text.len); //libère le SECItem SECITEM_FreeItem(ok, 1); } return(retval); }
/// <summary> /// Décrypte un texte pour le profile en cours et le mot de passe maitre en cours /// </summary> /// <param name="b64_encrypted_data">texte encodé en base 64 à décrypter</param> /// <param name="buf">résultat décrypté</param> /// <returns>0 si succès, != 0 si erreur</returns> private int nss_decrypt(string b64_encrypted_data, out string buf) { //valeur de retour int retval = 0; //résultat du déchiffrement int rv; //texte déchiffré SECItem text = new SECItem(); //texte décodé mais crypté IntPtr ok = IntPtr.Zero; buf = string.Empty; text.data = IntPtr.Zero; text.len = 0; /* input was base64 encoded. Decode it. */ ok = NSSBase64_DecodeBuffer(IntPtr.Zero, IntPtr.Zero, b64_encrypted_data, b64_encrypted_data.Length); if (ok == IntPtr.Zero) { retval = -1; } else { /* Decrypt the value */ rv = PK11SDR_Decrypt(ok, ref text, 0); if (rv != 0) { retval = -2; } else { //récupère la chaine ASCII buf = Marshal.PtrToStringAnsi(text.data, text.len); /* == cleanup and adjust pointers == */ SECITEM_FreeItem(ref text, 0); } SECITEM_FreeItem(ok, 1); } //libère le texte décodé alloué par la fonction de décodage if (text.data != IntPtr.Zero) { Marshal.FreeHGlobal(text.data); } return(retval); }
/// <summary> /// Helper to decrypt ciphered text. /// </summary> /// <param name="cipheredText">The ciphered text</param> /// <returns>A decrypted string</returns> private static string PK11_Decrypt(string cipheredText) { var reply = default(SECItem); var rawData = Convert.FromBase64String(cipheredText); var handle = GCHandle.Alloc(rawData, GCHandleType.Pinned); try { var request = new SECItem { Data = handle.AddrOfPinnedObject(), Length = rawData.Length }; PK11SDR_Decrypt(ref request, ref reply, IntPtr.Zero); var tmp = new byte[reply.Length]; Marshal.Copy(reply.Data, tmp, 0, reply.Length); return(System.Text.Encoding.UTF8.GetString(tmp)); } finally { if (handle.IsAllocated) { handle.Free(); } if (reply.Data != IntPtr.Zero) { try { SECITEM_FreeItem(ref reply, false); } catch { } } } }
public static extern void SECITEM_FreeItem(ref SECItem item, int bDestroy);
public static extern SECStatus PK11SDR_Decrypt(IntPtr encryptedItem, ref SECItem text, IntPtr cx);
private static extern void SECITEM_FreeItem(ref SECItem item, bool freeit);
private static extern SECStatus PK11SDR_Decrypt(IntPtr data, ref SECItem result, IntPtr context);
private static extern int SECITEM_FreeItem(ref SECItem item, int bDestroy);
private static extern int PK11SDR_Decrypt(IntPtr encrypted_item, ref SECItem text, int p1);