public string ToQueryString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat("AppId:{0}", this.AppId).AppendLine() .AppendFormat("MAC:{0}", this.MAC).AppendLine() .AppendFormat("CN:{0}", this.ComputerName).AppendLine() .AppendFormat("AppVersion:{0}", this.AppVersion).AppendLine() .AppendFormat("RuntimeVersion:{0}", this.RuntimeVersion).AppendLine() .AppendFormat("CDSN:{0}", this.CDriveSerialNumber).AppendLine() .AppendFormat("CPU:{0}", this.CPUNumber).AppendLine() .AppendFormat("SN:{0}", SN); string s = sb.ToString(); RSAPublicKey rsaPublic = RSAPublicKey.FromXmlString(AppHelper.STR_PUBLIC_KEY); Byte[] bs = RSAHelper.Encrypt(Encoding.UTF8.GetBytes(s), rsaPublic); var result = Convert.ToBase64String(bs); #if DEBUG Debug.WriteLine("Encrypted and encoded with base64:" + result); var bs2 = Convert.FromBase64String(result); var decryptedBytes = RSAHelper.Decrypt(bs2, RSAPrivateKey.FromXmlString(AppHelper.STR_PRIVATE_KEY)); var original = Encoding.UTF8.GetString(decryptedBytes); if (original != s) { throw new Exception("failed to decrypt"); } #endif return(result); }
private static byte[] Compute(byte[] data, RSAPrivateKey privateKey, int blockSize) { // // 私钥加密/解密公式为:mi = ci^d ( mod n ) // // 先将 c(二进制表示)分成数据块 c1, c2, ..., ci ,然后进行运算。 // BigInteger d = new BigInteger(privateKey.D); BigInteger n = new BigInteger(privateKey.Modulus); int blockOffset = 0; using (MemoryStream stream = new MemoryStream()) { while (blockOffset < data.Length) { int blockLen = Math.Min(blockSize, data.Length - blockOffset); byte[] blockData = new byte[blockLen]; Buffer.BlockCopy(data, blockOffset, blockData, 0, blockLen); BigInteger ci = new BigInteger(blockData); BigInteger mi = ci.modPow(d, n);//mi = ci^d ( mod n ) byte[] block = mi.getBytes(); stream.Write(block, 0, block.Length); blockOffset += blockLen; } return(stream.ToArray()); } }
public static byte[] Encrypt(byte[] data, RSAPrivateKey privateKey) { if (data == null) { throw new ArgumentNullException("data"); } if (privateKey == null) { throw new ArgumentNullException("privateKey"); } int blockSize = privateKey.Modulus.Length - 1; return(Compute(data, privateKey, blockSize)); }
public static byte[] Sign(byte[] data, RSAPrivateKey privateKey, HashAlgorithm hash) { if (data == null) { throw new ArgumentNullException("data"); } if (privateKey == null) { throw new ArgumentNullException("privateKey"); } if (hash == null) { throw new ArgumentNullException("hash"); } byte[] hashData = hash.ComputeHash(data); byte[] signature = Encrypt(hashData, privateKey); return(signature); }
public static RSAPrivateKey FromXmlString(string xmlString) { if (string.IsNullOrEmpty(xmlString)) { return(null); } try { using (XmlReader reader = XmlReader.Create(new StringReader(xmlString))) { if (!reader.ReadToFollowing("RSAKeyValue")) { return(null); } if (reader.LocalName != "Modulus" && !reader.ReadToFollowing("Modulus")) { return(null); } string modulus = reader.ReadElementContentAsString(); if (reader.LocalName != "D" && !reader.ReadToFollowing("D")) { return(null); } string d = reader.ReadElementContentAsString(); RSAPrivateKey privateKey = new RSAPrivateKey(); privateKey.Modulus = Convert.FromBase64String(modulus); privateKey.D = Convert.FromBase64String(d); return(privateKey); } } catch { return(null); } }
public static bool Verify(byte[] data, RSAPrivateKey privateKey, HashAlgorithm hash, byte[] signature) { if (data == null) { throw new ArgumentNullException("data"); } if (privateKey == null) { throw new ArgumentNullException("privateKey"); } if (hash == null) { throw new ArgumentNullException("hash"); } if (signature == null) { throw new ArgumentNullException("signature"); } byte[] hashData = hash.ComputeHash(data); byte[] signatureHashData = Decrypt(signature, privateKey); if (signatureHashData != null && signatureHashData.Length == hashData.Length) { for (int i = 0; i < signatureHashData.Length; i++) { if (signatureHashData[i] != hashData[i]) { return(false); } } return(true); } return(false); }