using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace Sog.Crypto { /// /// mono(.NetFramework 2.0) crypto api 和 BouncyCastle兼容性处理 /// public static class CryptoConvertUtils { /* /// /// 根据mod,exp生成publickey /// /// /// /// /// RSAParameters public static RsaKeyParameters GetRsaPublicKey(byte[] modulus, byte[] exponent) { RSAParameters return new RsaKeyParameters( false, new BigInteger(1, modulus), new BigInteger(1, exponent)); } /// /// 公钥解密 /// /// /// /// public static byte[] DecryptWithPublicKey(byte[] encryptedData, RsaKeyParameters publicParameters) { IAsymmetricBlockCipher eng = new Pkcs1Encoding(new RsaEngine()); eng.Init(false, publicParameters); byte[] data = eng.ProcessBlock(encryptedData, 0, encryptedData.Length); return data; } public static byte[] DecryptWithPublicKey(byte[] encryptedData, byte[] modulus, byte[] exponent) { RsaKeyParameters publicParameters = GetRsaPublicKey(modulus,exponent); return DecryptWithPublicKey(encryptedData, publicParameters); } /// /// 公钥加密 /// /// /// 公钥参数 /// 加密后的数据 public static byte[] EncryptWithPublicKey(byte[] data, RsaKeyParameters publicParameters) { IAsymmetricBlockCipher eng = new Pkcs1Encoding(new RsaEngine()); eng.Init(true, publicParameters); byte[] encryptedData = eng.ProcessBlock(data, 0, data.Length); return encryptedData; } public static byte[] EncryptWithPublicKey(byte[] data, byte[] modulus, byte[] exponent) { RsaKeyParameters publicParameters = GetRsaPublicKey(modulus, exponent); return EncryptWithPublicKey(data, publicParameters); } */ /// /// 对ExportCspBlob(false)方法到处的私钥进行解析,提取私钥参数 /// /// RSA.ExportCspBlob(false)得到的包含私钥信息 /// 公钥模数 public static byte[] PublicKeyResolve(byte[] cspblobPublicKey) { byte[] modulus = new byte[128]; Array.Reverse(cspblobPublicKey, 0, cspblobPublicKey.Length); Buffer.BlockCopy(cspblobPublicKey, 0, modulus, 0, 128); return modulus; } /// /// 对ExportCspBlob(true)方法到处的私钥进行解析,提取私钥参数 /// /// RSA.ExportCspBlob(true)得到的包含私钥信息 /// 私钥参数 public static Dictionary PrivateKeyResolve(byte[] cspblobPrivateKey) { Dictionary privateKeyParameters = new Dictionary(); Array.Reverse(cspblobPrivateKey, 0, cspblobPrivateKey.Length); int offset = 0; byte[] part = new byte[128]; Buffer.BlockCopy(cspblobPrivateKey, offset, part, offset, part.Length); privateKeyParameters.Add("D", part); offset += part.Length; part = new byte[64]; Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length); privateKeyParameters.Add("INVERSEQ", part); offset += part.Length; part = new byte[64]; Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length); privateKeyParameters.Add("DQ", part); offset += part.Length; part = new byte[64]; Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length); privateKeyParameters.Add("DP", part); offset += part.Length; part = new byte[64]; Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length); privateKeyParameters.Add("Q", part); offset += part.Length; part = new byte[64]; Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length); privateKeyParameters.Add("P", part); offset += part.Length; part = new byte[128]; Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length); privateKeyParameters.Add("MODULUS", part); return privateKeyParameters; } /// /// 数组转成16进制字符串 /// /// /// public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF " { string hexString = string.Empty; if (bytes != null) { StringBuilder strB = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { strB.Append(bytes[i].ToString("X2")); } hexString = strB.ToString(); } return hexString; } public static byte[] EncryptWithPublicKeyWithChewKeongTANBigInteger(byte[] data, byte[] modulus, byte[] exponent) { ChewKeongTAN.BigInteger bi_n = new ChewKeongTAN.BigInteger(modulus); ChewKeongTAN.BigInteger bi_e = new ChewKeongTAN.BigInteger(exponent); ChewKeongTAN.BigInteger biText = new ChewKeongTAN.BigInteger(data); ChewKeongTAN.BigInteger biEnText = biText.modPow(bi_e, bi_n); byte[] outbytes = biEnText.getBytes(); //TraceLog.Trace("EncryptWithPublicKeyWithChewKeongTANBigInteger entext {0}", biEnText.ToString()); return outbytes; } } }