// CCrypto.cs // http://svc.luckstar.com.tw/CodeHelper/cs/KeyWord/Crypto.txt // http://svc.luckstar.com.tw/CodeHelper/cs/index.html // 2017-03-29, Honda, Copy from Luckstar lib. // 2017-04-04, Honda, Add RSACryptoServiceProvider and SHA1, SHA256, SHA384, SHA512 using System; using System.Collections.Generic; using System.Linq; using System.Text; // add using System.IO; using System.Security.Cryptography; namespace ConsoleBase { public class CCrypto { public void Run() { Console.WriteLine("Crypto:"); Console.WriteLine("The size of the IV property must be the same as the BlockSize property divided by 8."); Console.WriteLine("The size of the Key property must be (<= KeySize) property divided by 8."); string sData; string sEncrypt; string sDecrypt; byte[] baData; byte[] baEncrypt; byte[] baDecrypt; string sKey; string sIV; byte[] baKey; byte[] baIV; PrintDashLine(); sData = "Message 包括中文 need to Digest."; baData = FromUTF8String(sData); Console.WriteLine("sData={0}.", sData); Console.WriteLine("sData.Length={0}, baData.Length={0}.", sData.Length, baData.Length); Console.WriteLine(); Console.WriteLine("MD5"); baEncrypt = EncryptMD5(baData); sEncrypt = ToHexString(baEncrypt); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine(); Console.WriteLine("SHA1"); baEncrypt = EncryptSHA1(baData); sEncrypt = ToHexString(baEncrypt); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine(); Console.WriteLine("SHA256"); baEncrypt = EncryptSHA256(baData); sEncrypt = ToHexString(baEncrypt); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine(); Console.WriteLine("SHA384"); baEncrypt = EncryptSHA384(baData); sEncrypt = ToHexString(baEncrypt); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine(); Console.WriteLine("SHA512"); baEncrypt = EncryptSHA512(baData); sEncrypt = ToHexString(baEncrypt); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); PrintDashLine(); Console.WriteLine("AesManaged"); sData = "test data 包括中文 for AesManaged."; sKey = "12345678901234567890123456789012"; sIV = "1234567890123456"; baData = FromUTF8String(sData); baKey = FromASCIIString(sKey); baIV = FromASCIIString(sIV); baEncrypt = EncryptAES(baData, baKey, baIV); sEncrypt = ToBase64String(baEncrypt); baEncrypt = FromBase64String(sEncrypt); baDecrypt = DecryptAES(baEncrypt, baKey, baIV); sDecrypt = ToUTF8String(baDecrypt); Console.WriteLine("sData={0}.", sData); Console.WriteLine("sData.Length={0}, baData.Length={0}.", sData.Length, baData.Length); Console.WriteLine("sKey={0}, sIV={1}.", sKey, sIV); Console.WriteLine("sKey.Length={0}, sIV.Length={1}.", sKey.Length, sIV.Length); Console.WriteLine("baKey.Length={0}, baIV.Length={1}.", baKey.Length, baIV.Length); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine("sDecrypt={0}.", sDecrypt); Console.WriteLine("Verify={0}.", sDecrypt == sData); PrintDashLine(); Console.WriteLine("DES"); sData = "test data 包括中文 for DESCryptoServiceProvider."; sKey = "12345678"; sIV = "ABCDEFGH"; baData = FromUTF8String(sData); baKey = FromASCIIString(sKey); baIV = FromASCIIString(sIV); baEncrypt = EncryptDES(baData, baKey, baIV); sEncrypt = ToBase64String(baEncrypt); baEncrypt = FromBase64String(sEncrypt); baDecrypt = DecryptDES(baEncrypt, baKey, baIV); sDecrypt = ToUTF8String(baDecrypt); Console.WriteLine("sData={0}.", sData); Console.WriteLine("sData.Length={0}, baData.Length={0}.", sData.Length, baData.Length); Console.WriteLine("sKey={0}, sIV={1}.", sKey, sIV); Console.WriteLine("sKey.Length={0}, sIV.Length={1}.", sKey.Length, sIV.Length); Console.WriteLine("baKey.Length={0}, baIV.Length={1}.", baKey.Length, baIV.Length); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine("sDecrypt={0}.", sDecrypt); Console.WriteLine("Verify={0}.", sDecrypt == sData); PrintDashLine(); Console.WriteLine("RC2"); sData = "test data 包括中文 for RC2CryptoServiceProvider."; sKey = "1234567890123456"; sIV = "ABCDEFGH"; baData = FromUTF8String(sData); baKey = FromASCIIString(sKey); baIV = FromASCIIString(sIV); baEncrypt = EncryptRC2(baData, baKey, baIV); sEncrypt = ToBase64String(baEncrypt); baEncrypt = FromBase64String(sEncrypt); baDecrypt = DecryptRC2(baEncrypt, baKey, baIV); sDecrypt = ToUTF8String(baDecrypt); Console.WriteLine("sData={0}.", sData); Console.WriteLine("sData.Length={0}, baData.Length={0}.", sData.Length, baData.Length); Console.WriteLine("sKey={0}, sIV={1}.", sKey, sIV); Console.WriteLine("sKey.Length={0}, sIV.Length={1}.", sKey.Length, sIV.Length); Console.WriteLine("baKey.Length={0}, baIV.Length={1}.", baKey.Length, baIV.Length); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine("sDecrypt={0}.", sDecrypt); Console.WriteLine("Verify={0}.", sDecrypt == sData); PrintDashLine(); Console.WriteLine("RijndaelManaged"); sData = "test data 包括中文 for RijndaelManaged."; sKey = "12345678901234567890123456789012"; sIV = "1234567890123456"; baData = FromUTF8String(sData); baKey = FromASCIIString(sKey); baIV = FromASCIIString(sIV); baEncrypt = EncryptRijndael(baData, baKey, baIV); sEncrypt = ToBase64String(baEncrypt); baEncrypt = FromBase64String(sEncrypt); baDecrypt = DecryptRijndael(baEncrypt, baKey, baIV); sDecrypt = ToUTF8String(baDecrypt); Console.WriteLine("sData={0}.", sData); Console.WriteLine("sData.Length={0}, baData.Length={0}.", sData.Length, baData.Length); Console.WriteLine("sKey={0}, sIV={1}.", sKey, sIV); Console.WriteLine("sKey.Length={0}, sIV.Length={1}.", sKey.Length, sIV.Length); Console.WriteLine("baKey.Length={0}, baIV.Length={1}.", baKey.Length, baIV.Length); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine("sDecrypt={0}.", sDecrypt); Console.WriteLine("Verify={0}.", sDecrypt == sData); PrintDashLine(); Console.WriteLine("TripleDES:"); sData = "test data 包括中文 for TripleDESCryptoServiceProvider."; sKey = "123456789012345678901234"; baData = FromUTF8String(sData); baKey = FromASCIIString(sKey); baEncrypt = EncryptTripleDES(baData, baKey); sEncrypt = ToBase64String(baEncrypt); baEncrypt = FromBase64String(sEncrypt); baDecrypt = DecryptTripleDES(baEncrypt, baKey); sDecrypt = ToUTF8String(baDecrypt); Console.WriteLine("sData={0}.", sData); Console.WriteLine("sData.Length={0}, baData.Length={0}.", sData.Length, baData.Length); Console.WriteLine("sKey={0}.", sKey); Console.WriteLine("sKey.Length={0}.", sKey.Length); Console.WriteLine("baKey.Length={0}.", baKey.Length); Console.WriteLine("baEncrypt.Length={0}, sEncrypt={1}.", baEncrypt.Length, sEncrypt); Console.WriteLine("sDecrypt={0}.", sDecrypt); Console.WriteLine("Verify={0}.", sDecrypt == sData); PrintDashLine(); Console.WriteLine("RSACryptoServiceProvider"); string sContainerA; string sContainerB; string sPublicKeyA; string sPublicKeyB; string sPrivateKeyA; string sPrivateKeyB; byte[] baSign; Boolean bVerifySign; sContainerA = "AContainer"; sPrivateKeyA = RSAGetKeyXML(sContainerA, true); sPublicKeyA = RSAGetKeyXML(sContainerA, false); sContainerB = "BContainer"; sPrivateKeyB = RSAGetKeyXML(sContainerB, true); sPublicKeyB = RSAGetKeyXML(sContainerB, false); sData = "Message 包括中文 send from A to B."; baData = FromUTF8String(sData); baEncrypt = RSAEncrypt(baData, sContainerA, sPublicKeyB); baSign = RSASignData(baData, sContainerA, sPrivateKeyA); baDecrypt = RSADecrypt(baEncrypt, sContainerB, sPrivateKeyB); sDecrypt = ToUTF8String(baDecrypt); bVerifySign = RSAVerifyData(baDecrypt, baSign, sContainerB, sPublicKeyA); Console.WriteLine("sPrivateKeyA.Length={0}, sPublicKeyA.Length={1}.", sPrivateKeyA.Length, sPublicKeyA.Length); Console.WriteLine("sPrivateKeyB.Length={0}, sPublicKeyB.Length={1}.", sPrivateKeyB.Length, sPublicKeyB.Length); Console.WriteLine("sData={0}.", sData); Console.WriteLine("sData.Length={0}, baData.Length={0}.", sData.Length, baData.Length); Console.WriteLine("Verify={0}, sDecrypt={1}.", sDecrypt == sData, sDecrypt); Console.WriteLine("VerifySignData={0}.", bVerifySign); Console.WriteLine("baEncrypt.Length={0}.", baEncrypt.Length); Console.WriteLine("baSign.Length={0}.", baSign.Length); PrintDashLine(); Console.WriteLine("LegalKeySizes for the symmetric algorithms:"); AesManaged aes = new AesManaged(); Console.WriteLine("AesManaged "); KeySizes[] ks = aes.LegalKeySizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min key size = " + k.MinSize); Console.WriteLine("\tLegal max key size = " + k.MaxSize); } ks = aes.LegalBlockSizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min block size = " + k.MinSize); Console.WriteLine("\tLegal max block size = " + k.MaxSize); } DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Console.WriteLine("DESCryptoServiceProvider "); ks = des.LegalKeySizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min key size = " + k.MinSize); Console.WriteLine("\tLegal max key size = " + k.MaxSize); } ks = des.LegalBlockSizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min block size = " + k.MinSize); Console.WriteLine("\tLegal max block size = " + k.MaxSize); } RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider(); Console.WriteLine("RC2CryptoServiceProvider "); ks = rc2.LegalKeySizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min key size = " + k.MinSize); Console.WriteLine("\tLegal max key size = " + k.MaxSize); } ks = rc2.LegalBlockSizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min block size = " + k.MinSize); Console.WriteLine("\tLegal max block size = " + k.MaxSize); } RijndaelManaged rij = new RijndaelManaged(); Console.WriteLine("RijndaelManaged "); ks = rij.LegalKeySizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min key size = " + k.MinSize); Console.WriteLine("\tLegal max key size = " + k.MaxSize); } ks = rij.LegalBlockSizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min block size = " + k.MinSize); Console.WriteLine("\tLegal max block size = " + k.MaxSize); } TripleDESCryptoServiceProvider tsp = new TripleDESCryptoServiceProvider(); Console.WriteLine("TripleDESCryptoServiceProvider "); ks = tsp.LegalKeySizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min key size = " + k.MinSize); Console.WriteLine("\tLegal max key size = " + k.MaxSize); } ks = tsp.LegalBlockSizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min block size = " + k.MinSize); Console.WriteLine("\tLegal max block size = " + k.MaxSize); } RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(); Console.WriteLine("RSACryptoServiceProvider "); ks = tsp.LegalKeySizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min key size = " + k.MinSize); Console.WriteLine("\tLegal max key size = " + k.MaxSize); } ks = tsp.LegalBlockSizes; foreach (KeySizes k in ks) { Console.WriteLine("\tLegal min block size = " + k.MinSize); Console.WriteLine("\tLegal max block size = " + k.MaxSize); } } private void PrintDashLine() { Console.WriteLine(); Console.WriteLine("----------"); } public static byte[] DecryptAES(byte[] baData, byte[] baKey, byte[] baIV) { AesManaged aes = new AesManaged(); aes.Key = baKey; aes.IV = baIV; using (MemoryStream ms1 = new MemoryStream()) { using (CryptoStream cs1 = new CryptoStream(ms1, aes.CreateDecryptor(), CryptoStreamMode.Write)) { cs1.Write(baData, 0, baData.Length); cs1.FlushFinalBlock(); } return ms1.ToArray(); } } public static byte[] DecryptDES(byte[] baData, byte[] baKey, byte[] baIV) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Key = baKey; des.IV = baIV; using (MemoryStream ms1 = new MemoryStream()) { using (CryptoStream cs1 = new CryptoStream(ms1, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs1.Write(baData, 0, baData.Length); cs1.FlushFinalBlock(); } return ms1.ToArray(); } } public static byte[] DecryptRC2(byte[] baData, byte[] baKey, byte[] baIV) { RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider(); rc2.Key = baKey; rc2.IV = baIV; using (MemoryStream ms1 = new MemoryStream()) { using (CryptoStream cs1 = new CryptoStream(ms1, rc2.CreateDecryptor(), CryptoStreamMode.Write)) { cs1.Write(baData, 0, baData.Length); cs1.FlushFinalBlock(); } return ms1.ToArray(); } } public static byte[] DecryptRijndael(byte[] baData, byte[] baKey, byte[] baIV) { RijndaelManaged Rijndael = new RijndaelManaged(); Rijndael.Key = baKey; Rijndael.IV = baIV; using (MemoryStream ms1 = new MemoryStream()) { using (CryptoStream cs1 = new CryptoStream(ms1, Rijndael.CreateDecryptor(), CryptoStreamMode.Write)) { cs1.Write(baData, 0, baData.Length); cs1.FlushFinalBlock(); } return ms1.ToArray(); } } public static byte[] DecryptTripleDES(byte[] baData, byte[] baKey) { TripleDESCryptoServiceProvider des3 = new TripleDESCryptoServiceProvider(); des3.Key = baKey; des3.Mode = CipherMode.ECB; //des3.Mode = CipherMode.CBC; des3.Padding = PaddingMode.PKCS7; ICryptoTransform decryptor1 = des3.CreateDecryptor(); return decryptor1.TransformFinalBlock(baData, 0, baData.Length); } public static byte[] EncryptAES(byte[] baData, byte[] baKey, byte[] baIV) { Console.WriteLine("AesManaged"); AesManaged aes = new AesManaged(); aes.Key = baKey; Console.WriteLine("BlockSize={0}, KeySize={1}.", aes.BlockSize, aes.KeySize); aes.IV = baIV; // The size of the IV property must be the same as the BlockSize property divided by 8. using (MemoryStream ms1 = new MemoryStream()) { using (CryptoStream cs1 = new CryptoStream(ms1, aes.CreateEncryptor(), CryptoStreamMode.Write)) { cs1.Write(baData, 0, baData.Length); cs1.FlushFinalBlock(); } return ms1.ToArray(); } } public static byte[] EncryptDES(byte[] baData, byte[] baKey, byte[] baIV) { Console.WriteLine("DESCryptoServiceProvider"); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Console.WriteLine("BlockSize={0}, KeySize={1}.", des.BlockSize, des.KeySize); des.Key = baKey; des.IV = baIV; using (MemoryStream ms1 = new MemoryStream()) { using (CryptoStream cs1 = new CryptoStream(ms1, des.CreateEncryptor(), CryptoStreamMode.Write)) { cs1.Write(baData, 0, baData.Length); cs1.FlushFinalBlock(); } return ms1.ToArray(); } } public static byte[] EncryptMD5(byte[] baData) { Console.WriteLine("MD5CryptoServiceProvider"); return (new MD5CryptoServiceProvider()).ComputeHash(baData); } public static byte[] EncryptSHA1(byte[] baData) { Console.WriteLine("SHA1CryptoServiceProvider"); return (new SHA1CryptoServiceProvider()).ComputeHash(baData); } public static byte[] EncryptSHA256(byte[] baData) { Console.WriteLine("SHA256CryptoServiceProvider"); return (new SHA256CryptoServiceProvider()).ComputeHash(baData); } public static byte[] EncryptSHA384(byte[] baData) { Console.WriteLine("SHA384CryptoServiceProvider"); return (new SHA384CryptoServiceProvider()).ComputeHash(baData); } public static byte[] EncryptSHA512(byte[] baData) { Console.WriteLine("SHA512CryptoServiceProvider"); return (new SHA512CryptoServiceProvider()).ComputeHash(baData); } public static byte[] EncryptRC2(byte[] baData, byte[] baKey, byte[] baIV) { Console.WriteLine("RC2CryptoServiceProvider"); RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider(); Console.WriteLine("BlockSize={0}, KeySize={1}.", rc2.BlockSize, rc2.KeySize); rc2.Key = baKey; rc2.IV = baIV; using (MemoryStream ms1 = new MemoryStream()) { using (CryptoStream cs1 = new CryptoStream(ms1, rc2.CreateEncryptor(baKey, baIV), CryptoStreamMode.Write)) { cs1.Write(baData, 0, baData.Length); cs1.FlushFinalBlock(); } return ms1.ToArray(); } } public static byte[] EncryptRijndael(byte[] baData, byte[] baKey, byte[] baIV) { Console.WriteLine("RijndaelManaged"); RijndaelManaged Rijndael = new RijndaelManaged(); Console.WriteLine("BlockSize={0}, KeySize={1}.", Rijndael.BlockSize, Rijndael.KeySize); Rijndael.Key = baKey; Rijndael.IV = baIV; using (MemoryStream ms1 = new MemoryStream()) { using (CryptoStream cs1 = new CryptoStream(ms1, Rijndael.CreateEncryptor(baKey, baIV), CryptoStreamMode.Write)) { cs1.Write(baData, 0, baData.Length); cs1.FlushFinalBlock(); } return ms1.ToArray(); } } public static byte[] EncryptTripleDES(byte[] baData, byte[] baKey) { Console.WriteLine("TripleDESCryptoServiceProvider"); TripleDESCryptoServiceProvider des3 = new TripleDESCryptoServiceProvider(); Console.WriteLine("BlockSize={0}, KeySize={1}.", des3.BlockSize, des3.KeySize); des3.Key = baKey; des3.Mode = CipherMode.ECB; //des3.Mode = CipherMode.CBC; des3.Padding = PaddingMode.PKCS7; ICryptoTransform encryptor1 = des3.CreateEncryptor(); return encryptor1.TransformFinalBlock(baData, 0, baData.Length); } public static RSACryptoServiceProvider RSACreateMachineKeyStore(string sKeyContainerName) { RSACryptoServiceProvider rsa1; CspParameters para1; para1 = new CspParameters(); para1.KeyContainerName = sKeyContainerName; para1.Flags = CspProviderFlags.UseMachineKeyStore; para1.ProviderName = "Microsoft Strong Cryptographic Provider"; para1.ProviderType = 1; // PROV_RSA_FULL. refer to WinCrypt.h. rsa1 = new RSACryptoServiceProvider(para1); rsa1.KeySize = 1024; // 128 bytes. return rsa1; } public static string RSAGetKeyXML(string sKeyContainerName, Boolean bIncludePublicAndPrivateKey) { RSACryptoServiceProvider rsa1 = RSACreateMachineKeyStore(sKeyContainerName); return rsa1.ToXmlString(bIncludePublicAndPrivateKey); } public static void RSASetKeyXML(string sKeyContainerName, string sKeyXML) { RSACryptoServiceProvider rsa1 = RSACreateMachineKeyStore(sKeyContainerName); rsa1.FromXmlString(sKeyXML); } public static byte[] RSADecrypt(byte[] baData, string sKeyContainerName, string sPrivateKeyXML = "") { RSACryptoServiceProvider rsa1 = RSACreateMachineKeyStore(sKeyContainerName); if (!string.IsNullOrEmpty(sPrivateKeyXML)) rsa1.FromXmlString(sPrivateKeyXML); Boolean fOAEP = false; // true to perform direct RSA decryption using OAEP padding (only available on a computer running Microsoft Windows XP or later) ; otherwise, false to use PKCS#1 v1.5 padding return rsa1.Decrypt(baData, fOAEP); } public static byte[] RSAEncrypt(byte[] baData, string sKeyContainerName, string sPublicKeyXML = "") { RSACryptoServiceProvider rsa1 = RSACreateMachineKeyStore(sKeyContainerName); if (!(string.IsNullOrEmpty(sPublicKeyXML))) rsa1.FromXmlString(sPublicKeyXML); Boolean fOAEP = false; // true to perform direct RSA decryption using OAEP padding (only available on a computer running Microsoft Windows XP or later) ; otherwise, false to use PKCS#1 v1.5 padding return rsa1.Encrypt(baData, fOAEP); } public static byte[] RSASignData(byte[] baData, string sKeyContainerName, string sPublicKeyXML = "") { RSACryptoServiceProvider rsa1 = RSACreateMachineKeyStore(sKeyContainerName); if (!(string.IsNullOrEmpty(sPublicKeyXML))) rsa1.FromXmlString(sPublicKeyXML); return rsa1.SignData(baData, new SHA1CryptoServiceProvider()); } public static Boolean RSAVerifyData(byte[] baData, byte[] baSignData, string sKeyContainerName, string sPublicKeyXML = "") { RSACryptoServiceProvider rsa1 = RSACreateMachineKeyStore(sKeyContainerName); if (!(string.IsNullOrEmpty(sPublicKeyXML))) rsa1.FromXmlString(sPublicKeyXML); return rsa1.VerifyData(baData, new SHA1CryptoServiceProvider(), baSignData); } public static byte[] FromASCIIString(string sASCII) { return Encoding.ASCII.GetBytes(sASCII); } public static byte[] FromBase64String(String sBase64String) { return Convert.FromBase64String(sBase64String); } public static byte[] FromBig5String(string sBig5) { return Encoding.GetEncoding(950).GetBytes(sBig5); } public static string ToASCIIString(byte[] ba) { return Encoding.ASCII.GetString(ba); } public static string ToBase64String(byte[] ba) { return Convert.ToBase64String(ba); } public static string ToBig5String(byte[] ba) { return Encoding.GetEncoding(950).GetString(ba); } public static string ToHexString(byte[] ba) { return ToHexString(ba, string.Empty); } public static string ToHexString(byte[] ba, string sDelimiter) { //StringBuilder hex = new StringBuilder(ba.Length * 2); //foreach (byte b in ba) // hex.AppendFormat("{0:x2}", b); //return hex.ToString(); string hex = BitConverter.ToString(ba); return hex.Replace("-", sDelimiter); } public static string ToHexString(byte b1) { //StringBuilder hex = new StringBuilder(ba.Length * 2); //foreach (byte b in ba) // hex.AppendFormat("{0:x2}", b); //return hex.ToString(); //return b1.ToString("X2"); return b1.ToString("x2"); } public static string ToUTF8String(byte[] baUTF8) { return Encoding.UTF8.GetString(baUTF8); } public static byte[] FromHexString(String sHex) { return FromHexString(sHex, 0); } public static byte[] FromHexString(String sHex, int iLenDelimiter) { if (string.IsNullOrEmpty(sHex)) return null; if (sHex.Length < 2) return null; if (iLenDelimiter < 0) iLenDelimiter = 0; int iBytes = (sHex.Length / (2 + iLenDelimiter)); if ((sHex.Length % (2 + iLenDelimiter)) != 0) iBytes++; byte[] ba1 = new byte[iBytes]; int j = 0; for (int i = 0; i < sHex.Length; i += (2 + iLenDelimiter)) { ba1[j] = Convert.ToByte(sHex.Substring(i, 2), 16); j++; } return ba1; } public static byte[] FromUTF8String(string sUTF8) { return Encoding.UTF8.GetBytes(sUTF8); } } } result: Crypto: The size of the IV property must be the same as the BlockSize property divided by 8. The size of the Key property must be (<= KeySize) property divided by 8. ---------- sData=Message 包括中文 need to Digest.. sData.Length=28, baData.Length=28. MD5 MD5CryptoServiceProvider baEncrypt.Length=16, sEncrypt=CE1F2CDFFDB4973453FCE79AE303D578. SHA1 SHA1CryptoServiceProvider baEncrypt.Length=20, sEncrypt=ACC20FA202CBD7EF3EC0148EE1767AEADF74BEFD. SHA256 SHA256CryptoServiceProvider baEncrypt.Length=32, sEncrypt=010AAF88ED6A7715D58A8D717905613CB45CA1B17AFABC939B4BEFC5B13C7BF9. SHA384 SHA384CryptoServiceProvider baEncrypt.Length=48, sEncrypt=A17DFC1F86AFF5D463D53664B8887AB0CD50FDEA5151D660E8493A260A7843DC390CB6ACAD75FD6D0A8FFAEB50FB4607. SHA512 SHA512CryptoServiceProvider baEncrypt.Length=64, sEncrypt=72EB2B85E2400BAA219EC687A2B6794975DB6BD2F2ED79D007126B40337B14B31D232C00B34765922235FAC232CE4ECC74F1260AE19A4E1CF50E0510535E0F99. ---------- AesManaged AesManaged BlockSize=128, KeySize=256. sData=test data 包括中文 for AesManaged.. sData.Length=30, baData.Length=30. sKey=12345678901234567890123456789012, sIV=1234567890123456. sKey.Length=32, sIV.Length=16. baKey.Length=32, baIV.Length=16. baEncrypt.Length=48, sEncrypt=+8nHOtEVFF3Gmh0Dcr5/J/3ToW3wELvw0tUkDuDRs/V8pTe9u0ZCBbQVMExSZvZV. sDecrypt=test data 包括中文 for AesManaged.. Verify=True. ---------- DES DESCryptoServiceProvider BlockSize=64, KeySize=64. sData=test data 包括中文 for DESCryptoServiceProvider.. sData.Length=44, baData.Length=44. sKey=12345678, sIV=ABCDEFGH. sKey.Length=8, sIV.Length=8. baKey.Length=8, baIV.Length=8. baEncrypt.Length=56, sEncrypt=zA6JZAuOz4LoD8JVs9CK8FwM2/OeFVwu+ircmpIwgSIkbV8fgrc4CWLRCflXe8SbR1qAwCjjOIk=. sDecrypt=test data 包括中文 for DESCryptoServiceProvider.. Verify=True. ---------- RC2 RC2CryptoServiceProvider BlockSize=64, KeySize=128. sData=test data 包括中文 for RC2CryptoServiceProvider.. sData.Length=44, baData.Length=44. sKey=1234567890123456, sIV=ABCDEFGH. sKey.Length=16, sIV.Length=8. baKey.Length=16, baIV.Length=8. baEncrypt.Length=56, sEncrypt=pWsbe1LvSn3sAVZCRv6vws83keTj4GN4tJMQYoxnJ+YhIHptTCJbriaaNcaWg80BYtAkzoCCiY0=. sDecrypt=test data 包括中文 for RC2CryptoServiceProvider.. Verify=True. ---------- RijndaelManaged RijndaelManaged BlockSize=128, KeySize=256. sData=test data 包括中文 for RijndaelManaged.. sData.Length=35, baData.Length=35. sKey=12345678901234567890123456789012, sIV=1234567890123456. sKey.Length=32, sIV.Length=16. baKey.Length=32, baIV.Length=16. baEncrypt.Length=48, sEncrypt=+8nHOtEVFF3Gmh0Dcr5/JwR5Fz4Bg3wqJmpmNCLH1eb9A+2/H4pqO9noZF0GLl5z. sDecrypt=test data 包括中文 for RijndaelManaged.. Verify=True. ---------- TripleDES: TripleDESCryptoServiceProvider BlockSize=64, KeySize=192. sData=test data 包括中文 for TripleDESCryptoServiceProvider.. sData.Length=50, baData.Length=50. sKey=123456789012345678901234. sKey.Length=24. baKey.Length=24. baEncrypt.Length=64, sEncrypt=qnktJIRNI6OoD6QOzjO7cCoD/BLielqqcwDWOa8xBGQuL0xua3+DvP1KH+xtuqipIzFF1YL1LbLGcET3zgc6MA==. sDecrypt=test data 包括中文 for TripleDESCryptoServiceProvider.. Verify=True. ---------- RSACryptoServiceProvider sPrivateKeyA.Length=915, sPublicKeyA.Length=243. sPrivateKeyB.Length=915, sPublicKeyB.Length=243. sData=Message 包括中文 send from A to B.. sData.Length=30, baData.Length=30. Verify=True, sDecrypt=Message 包括中文 send from A to B.. VerifySignData=True. baEncrypt.Length=128. baSign.Length=128. ---------- LegalKeySizes for the symmetric algorithms: AesManaged Legal min key size = 128 Legal max key size = 256 Legal min block size = 128 Legal max block size = 128 DESCryptoServiceProvider Legal min key size = 64 Legal max key size = 64 Legal min block size = 64 Legal max block size = 64 RC2CryptoServiceProvider Legal min key size = 40 Legal max key size = 128 Legal min block size = 64 Legal max block size = 64 RijndaelManaged Legal min key size = 128 Legal max key size = 256 Legal min block size = 128 Legal max block size = 256 TripleDESCryptoServiceProvider Legal min key size = 128 Legal max key size = 192 Legal min block size = 64 Legal max block size = 64 RSACryptoServiceProvider Legal min key size = 128 Legal max key size = 192 Legal min block size = 64 Legal max block size = 64