|
byte[] byData = System.Text.UnicodeEncoding.Unicode.GetBytes(strMyString);
|
|
|
|
|
If you need to open a file (for reading only) in IIS for your ASP.NET application, be sure to use the FileAccess.Read flag in your FileStream constructor, e.g. :
FileStream fs = new FileStream
(
m_strServerPath + sbBinFileName.ToString(),
FileMode.Open,
FileAccess.Read // <-- important.
);
Otherwise, the file access attempt may fail.
|
|
|
|
|
If you use a StringBuilder object over and over again, you may need to reset its values before attempting to re-assign values to it via AppendFormat :
In this case, use the Remove() method, e.g. :
sb_string.Remove(0, sb_string.Length);
sb_string.AppendFormat(@"...", ...);
|
|
|
|
|
Initialization code :
using System.Security.Principal;
using System.Reflection;
using System.Security.Permissions;
using System.Threading;
using System.Security;
...
...
...
private enum role
{
OPERATIONAL = 0,
ADMIN = 1,
MANAGER = 2
};
private static string[] m_strAvailableRoles = new string[] { "OPERATIONAL", "ADMIN", "MANAGER" };
private static GenericIdentity m_generic_identity;
private static GenericPrincipal m_generic_principal;
...
...
...
string[] m_strCurrentRoles = new string[]
{ m_strAvailableRoles[(int)role.ADMIN] };
// Initialize the identity and principal objects used in this application.
InitializeIdentityAndPrincipal("john", m_strCurrentRoles);
// Initialize the entire application according to the values set in the
// current identity and principal objects.
InitializeApplicationAccordingToCurrentIdentityAndPrincipal();
...
...
...
// Create and initialize the identity and principal objects used in this
// application.
// We will use Reflection to create objects.
protected void InitializeIdentityAndPrincipal
(string strUserName, string[] strCurrentRoles)
{
Type[] type_array = new Type [] { typeof(string) };
object[] object_array = new object[] { strUserName };
ConstructorInfo ci = typeof(GenericIdentity).GetConstructor(type_array);
// Create a GenericIdentity object by invoking the GenericIdentity
// constructor that takes in a single string value (i.e. the name of the
// user on whose behalf the code is running).
m_generic_identity = (GenericIdentity)(ci.Invoke(object_array));
// Re-use type_array and object_array.
type_array = new Type [] { typeof(GenericIdentity), typeof(string[]) };
object_array = new object[] { m_generic_identity, strCurrentRoles };
ci = typeof(GenericPrincipal).GetConstructor(type_array);
// Create a GenericPrincipal object by invoking the GenericPrincipal
// constructor that takes in a GenericIdentity object and a string array of
// roles.
m_generic_principal = (GenericPrincipal)(ci.Invoke(object_array));
}
// Initialize the entire application according to the values set in the
// current identity and principal objects.
protected void InitializeApplicationAccordingToCurrentIdentityAndPrincipal()
{
// Set the current thread's current principal (for role-based security).
Thread.CurrentPrincipal = m_generic_principal;
// Enable and disable the menu items in the application according to
// the role of the current principal.
if (m_generic_principal.IsInRole(m_strAvailableRoles[(int)
role.OPERATIONAL]))
{
operationsItem.Enabled = true;
}
else
{
operationsItem.Enabled = false;
}
...
...
...
}
|
|
|
|
|
PrincipalPermission Demands :
// We use declarative PrincipalPermission demands on the current thread.
// Here, we are stating that any person with OPERATIONAL and MANAGER
// roles may perform OperationsAction().
// However, ONLY a person named "janet" with ADMIN role may perform
// OperationsAction().
[PrincipalPermissionAttribute(SecurityAction.Demand, Role="OPERATIONAL")]
[PrincipalPermissionAttribute(SecurityAction.Demand, Name="janet", Role="ADMIN")]
[PrincipalPermissionAttribute(SecurityAction.Demand, Role="MANAGER")]
private void OperationsAction()
{
Message("Operations Action");
}
// We use declarative PrincipalPermission demands on the current thread.
// Here, we are stating that any person with ADMIN and MANAGER
// roles may perform OperationsAction().
[PrincipalPermissionAttribute(SecurityAction.Demand, Role="ADMIN")]
[PrincipalPermissionAttribute(SecurityAction.Demand, Role="MANAGER")]
private void AdminAction()
{
Message("Admin Action");
}
private void ManagerAction()
{
// We use imperative PrincipalPermission demand to ensure that
// only "john", "paul", "george" and "ringo" (all having ADMIN role)
// can perform important action in this function.
PrincipalPermission principal_permission_john = new PrincipalPermission("john", m_strAvailableRoles[(int)role.MANAGER]);
PrincipalPermission principal_permission_paul = new PrincipalPermission("paul", m_strAvailableRoles[(int)role.MANAGER]);
PrincipalPermission principal_permission_george = new PrincipalPermission("george", m_strAvailableRoles[(int)role.MANAGER]);
PrincipalPermission principal_permission_ringo = new PrincipalPermission("ringo", m_strAvailableRoles[(int)role.MANAGER]);
try
{
principal_permission_john.Union(principal_permission_paul.Union(principal_permission_george.Union(principal_permission_ringo))).Demand();
Message("Manager Action");
}
catch (SecurityException se)
{
Message("Security Exception : " + se.Message);
}
}
|
|
|
|
|
In order to combine the use of SymmetricAlgorithm and AsymmetricAlgorithm for key exchange purposes, it seems that the key and block sizes must each be 256.
Otherwise, we get a "System.Security.Cryptography.CryptographicException" with additional info : "PKCS7 padding is invalid and cannot be removed."
|
|
|
|
|
In addition, if you were to use the CryptoStream object to perform Symmetric Encryption, e.g. :
public static void Encrypt_SymmetricAlgorithm_File
(
ref Stream stream_in,
ref Stream stream_out,
ref SymmetricAlgorithm sa
)
{
CryptoStream c = new CryptoStream
(
stream_out,
sa.CreateEncryptor(),
CryptoStreamMode.Write
);
byte [] buf = new byte[BUFSIZE];
int siz;
while ((siz = stream_in.Read(buf,0, BUFSIZE)) > 0)
c.Write(buf, 0, siz);
c.Close(); // This better be called.
return;
}
You better call the CryptoStream.Close() method. Otherwise, the cryptographic output to the output stream ("stream_out" in the above example), may become corrupted.
|
|
|
|
|
Also, it would be good programming practice to call CryptoStream.Close() when you use the CryptoStream for dercyption, e.g. :
public static void Decrypt_SymmetricAlgorithm_File
(
ref Stream stream_in,
ref Stream stream_out,
ref SymmetricAlgorithm sa
)
{
CryptoStream c = new CryptoStream
(
stream_in,
sa.CreateDecryptor(),
CryptoStreamMode.Read
);
byte [] buf = new byte[BUFSIZE];
int siz;
while ((siz = c.Read(buf,0, BUFSIZE)) > 0)
stream_out.Write(buf, 0, siz);
c.Close(); // This better be called.
}
|
|
|
|
|
The Close() ensures that padding is correctly performed. Without it, the stream is likely to be corrupted.
|
|
|
|
|
1. Suppose that you have created a string out of a DateTime object e.g. :
string strDateTime = DateTime.Now.ToShortDateString();
2. To later convert this DateTime string back to its original DateTime object, use the Parse() method together with DateTimeFormatInfo.CurrentInfo :
DateTime dt = DateTime.Parse(strDateTime, DateTimeFormatInfo.CurrentInfo);
3. CurrentInfo is a property of DateTimeFormatInfo which returns a read-only DateTimeFormatInfo object that formats values based on the current culture.
4. Note that because "strDateTime" was formatted according to the current culture, we can use DateTimeFormatInfo.CurrentInfo to retrieve -that- culture.
|
|
|
|
|
|