Showing posts with label digital certificate. Show all posts
Showing posts with label digital certificate. Show all posts

Thursday, June 11, 2009

Encrypt And Decrypt Using Digital Signatures

Encrypt And Decrypt Using Digital Signatures


Now in continutaion to my earlier artcile the mirroe link of the article here I am going to describe how we can use the digital signatures for the purpose of Encryption and Decryption.

The Following code will show how we can encrypt the plain text.

Encryption


#region Encryption

public string GetEncryptedText(string PlainStringToEncrypt)

{

try

{

string PlainString = PlainStringToEncrypt.Trim();

byte[] cipherbytes = ASCIIEncoding.ASCII.GetBytes(PlainString);

RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509_2.PublicKey.Key;

byte[] cipher = rsa.Encrypt(cipherbytes, false);

return Convert.ToBase64String(cipher);

}

catch (Exception e)

{

throw e;

}

}
#endregion

Now once we have encrypted text we need to decrypt it. the following section of code will demonstrate how to decrypt that encrypted text.

Decryption


#region Decryption

public string GetDecryptedText(string EncryptedStringToDecrypt)

{

try

{



try

{

byte[] cipherbytes = Convert.FromBase64String(EncryptedStringToDecrypt);

if (x509_2.HasPrivateKey)

{

RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509_2.PrivateKey;

byte[] plainbytes = rsa.Decrypt(cipherbytes, false);

System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

return enc.GetString(plainbytes);

}

else

{

throw new Exception("Certificate used for has no private key.");

}

}

catch (Exception e)

{

return e.Message;

}

}

catch

{

return EncryptedStringToDecrypt;

}

}
#endregion



Thanks and Regards

Meetu Choudhary

Wednesday, June 3, 2009

Accessing the properties of a digital signature

Accessing the properties of a digital signature



Now once we have selected the certificate. We need to access the properties of the certificate. Here is a small piece of code to access all the properties which may be helpful to us for any context.

before accessing these properties we have to set the certificate object. for that you can get the code from any of the two previous articles.
1. Article link , Mirror link
2. Article link , Mirror link


Now once we have the certificate (X509Certificate2 x509_2;)
we can o with the following code

[code]

///



/// Set All the Properties of the Certificate

///


public Boolean SetProperties()

{

if (x509_2 != null)

{

RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509_2.PublicKey.Key;

_PublicKey_Key =_PublicKeyXML = rsa.ToXmlString(false);

//_PrivateKeyXML = rsa.ToXmlString(true );

_PrivateKeyXML=_Private_Key = x509_2.PrivateKey.ToXmlString(false);

_PKeyExchangeAlgorithm = x509_2.PrivateKey.KeyExchangeAlgorithm;

_PublicKey = x509_2.GetPublicKeyString();

//_PublicKey_Key = Convert.ToString(x509_2.PublicKey.Key);

_SerialNumber = x509_2.GetPublicKeyString();

_Thumbprint = x509_2.Thumbprint;

_RawCertDataString = x509_2.GetRawCertDataString();

_FriendlyName = x509_2.FriendlyName;

_HashString = x509_2.GetCertHashString();

_EffectiveDate = x509_2.GetEffectiveDateString();

_ExpirationDate = x509_2.GetExpirationDateString();

_Format = x509_2.GetFormat();

_IssuerName = x509_2.GetIssuerName();

_KeyAlgorithm = x509_2.GetKeyAlgorithm();

_KeyAlgorithmParameters = x509_2.GetKeyAlgorithmParametersString();

_CertName = x509_2.GetName();

_CertSubject = x509_2.Subject;

_CertVersion = x509_2.Version;

_SignatureAlgorithm_Value = x509_2.SignatureAlgorithm.Value;

_SignatureAlgorithm_ToString = x509_2.SignatureAlgorithm.ToString();

_SignatureAlgorithm_FriendlyName = x509_2.SignatureAlgorithm.FriendlyName;

return true;

}

else { return false; }

}

[/code]




Thanks and Regards
Meetu Choudhary

Open Certificate Stores Including Certificates in Token

Open Certificate Stores Including Certificates in Token



In My Previous article Mirror Link I have shown how to open a certificate store (Machine certificate store which is displayed in the Internet Explorer.) but the drawback of that code was it can't open the certificates stored in the token. so here is another method which will overcome the drawback stated above. It will open the certificates of the machine store as well as of the tokens.

[code]
///

/// Opens the Certificate Store of IE including the Certificates in Token

///


/// The variable passed to store the reason if function returns false

/// True if a certificate is selected and false if no certificate is selected

public Boolean OpenStoreToken(ref string popupScript)

{

x509_2 = null;

//Create and Initilaize a variable of x509Store providing the store name and the store location

X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);

//Create X509Certificate2Collection

X509Certificate2Collection col = new X509Certificate2Collection();

//Create X509Certificate2Collection

X509Certificate2Collection sel = new X509Certificate2Collection();

//Create X509Certificate2Enumerator

X509Certificate2Enumerator en;// =new X509Certificate2Enumerator();

//Open the Store for readonly purpose

st.Open(OpenFlags.ReadOnly);

//set the col i.e. X509Certificate2Collection to the collection of the certificates stored in the IE and the Token

col = st.Certificates;

//set the sel i.e. X509Certificate2Collection which actuly displays a dialog box for selecting an X.509 certificate from a certificate collection.

sel = X509Certificate2UI.SelectFromCollection(col, "Certificates", "Select one to sign", X509SelectionFlag.SingleSelection);

if (sel.Count > 0)

{

en = sel.GetEnumerator();

en.MoveNext();

x509_2 = en.Current;

}

st.Close();

if (x509_2 == null)

{

popupScript = "You didn't select a certificate!!!";

return false;

}

else

{

return true;

}

}



[/code]



Thanks and Regards
Meetu Choudhary

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com