/// <summary> /// Decrypt byte[] data with xml/json format. /// </summary> /// <param name="dataBytes">The data to be encrypted.</param> /// <param name="privateKey">The private key of xml format.</param> /// <param name="fOEAP"></param> /// <param name="sizeType"></param> /// <param name="keyType"></param> /// <returns>The decrypted data.</returns> public static string DecryptByPrivateKey( byte[] dataBytes, string privateKey, bool fOEAP, RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML) { var rsa = TouchRsaUtilFromPrivateKey(keyType, Encoding.UTF8, privateKey, sizeType); return(rsa.DecryptByPrivateKey(dataBytes, fOEAP)); }
/// <summary> /// Encrypt byte[] data with xml/json format. /// </summary> /// <param name="dataBytes">The data to be encrypted.</param> /// <param name="privateKey">The public key of xml format.</param> /// <param name="sizeType"></param> /// <param name="keyType"></param> /// <param name="padding"></param> /// <returns>The encrypted data.</returns> public static string EncryptByPrivateKey( byte[] dataBytes, string privateKey, RSAEncryptionPadding padding, RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML) { var rsa = TouchRsaUtilFromPrivateKey(keyType, Encoding.UTF8, privateKey, sizeType); return(rsa.EncryptByPrivateKey(dataBytes, padding)); }
/// <summary> /// Decrypt string data with xml/json format. /// </summary> /// <param name="data">The data to be encrypted.</param> /// <param name="privateKey">The private key of xml format.</param> /// <param name="fOEAP"></param> /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param> /// <param name="sizeType"></param> /// <param name="keyType"></param> /// <returns>The decrypted data.</returns> public static string DecryptByPrivateKey( string data, string privateKey, bool fOEAP, Encoding encoding = null, RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML) { var rsa = TouchRsaUtilFromPrivateKey(keyType, encoding, privateKey, sizeType); return(rsa.DecryptByPrivateKey(data, fOEAP)); }
/// <summary> /// Signature as byte[] /// </summary> /// <param name="data"></param> /// <param name="privateKey"></param> /// <param name="hashAlgorithmName"></param> /// <param name="encoding"></param> /// <param name="sizeType"></param> /// <param name="keyType"></param> /// <returns></returns> public static byte[] SignatureByPrivateKeyAsBytes( string data, string privateKey, HashAlgorithmName hashAlgorithmName, Encoding encoding = null, RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML) { var rsa = TouchRsaUtilFromPrivateKey(keyType, encoding, privateKey, sizeType); return(rsa.SignDataByPrivateKeyToBytes(data, hashAlgorithmName)); }
/// <summary> /// Signature as string /// </summary> /// <param name="data"></param> /// <param name="publicKey"></param> /// <param name="hashAlgorithmName"></param> /// <param name="encoding"></param> /// <param name="sizeType"></param> /// <param name="keyType"></param> /// <returns></returns> public static string SignatureByPublicKeyAsString( string data, string publicKey, HashAlgorithmName hashAlgorithmName, Encoding encoding = null, RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML) { var rsa = TouchRsaUtilFromPublicKey(keyType, encoding, publicKey, sizeType); return(rsa.SignDataByPublicKey(data, hashAlgorithmName)); }
/// <summary> /// Encrypt string data with xml/json format. /// </summary> /// <param name="data">The data to be encrypted.</param> /// <param name="privateKey">The public key of xml format.</param> /// <param name="padding"></param> /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param> /// <param name="sizeType"></param> /// <param name="keyType"></param> /// <returns>The encrypted data.</returns> public static string EncryptByPrivateKey( string data, string privateKey, RSAEncryptionPadding padding, Encoding encoding = null, RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML) { var rsa = TouchRsaUtilFromPrivateKey(keyType, encoding, privateKey, sizeType); return(rsa.EncryptByPrivateKey(data, padding)); }
/// <summary> /// Verify /// </summary> /// <param name="data"></param> /// <param name="privateKey"></param> /// <param name="signature"></param> /// <param name="hashAlgorithmName"></param> /// <param name="encoding"></param> /// <param name="sizeType"></param> /// <param name="keyType"></param> /// <returns></returns> public static bool VerifyByPrivateKey( string data, string privateKey, string signature, HashAlgorithmName hashAlgorithmName, Encoding encoding = null, RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML) { var rsa = TouchRsaUtilFromPrivateKey(keyType, encoding, privateKey, sizeType); return(rsa.VerifyDataByPrivateKey(data, signature, hashAlgorithmName)); }
public static RSAKey CreateKey(RSAKeySizeTypes size = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML) { using (var rsa = new RSACryptoServiceProvider((int)size)) { var publicKey = keyType == RSAKeyTypes.JSON ? rsa.ToJsonString(false) : rsa.ToLvccXmlString(false); var privateKey = keyType == RSAKeyTypes.JSON ? rsa.ToJsonString(true) : rsa.ToLvccXmlString(true); return(new RSAKey { PublicKey = publicKey, PrivateKey = privateKey, Exponent = rsa.ExportParameters(false).Exponent.ToHexString(), Modulus = rsa.ExportParameters(false).Modulus.ToHexString() }); } }
/// <summary> /// Create a new <see cref="RSAKey"/> /// </summary> /// <param name="size"></param> /// <param name="keyType"></param> /// <param name="format"></param> /// <returns></returns> public static RSAKey CreateKey(RSAKeySizeTypes size = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML, bool format = false) { switch (keyType) { case RSAKeyTypes.Pkcs1: return(Core.RSAKeyGenerator.Pkcs1Key((int)size, format)); case RSAKeyTypes.Pkcs8: return(Core.RSAKeyGenerator.Pkcs8Key((int)size, format)); case RSAKeyTypes.XML: return(Core.RSAKeyGenerator.XmlKey((int)size)); case RSAKeyTypes.JSON: return(Core.RSAKeyGenerator.JsonKey((int)size)); default: return(Core.RSAKeyGenerator.XmlKey((int)size)); } }
private static RSABase TouchRsaUtilFromPrivateKey(RSAKeyTypes keyType, Encoding encoding, string privateKey, RSAKeySizeTypes sizeType) { RSABase rsa = keyType switch { RSAKeyTypes.XML => new RSAXmlUtil(encoding, null, privateKey, (int)sizeType), RSAKeyTypes.JSON => new RSAJsonUtil(encoding, null, privateKey, (int)sizeType), RSAKeyTypes.Pkcs1 => new RSAPkcs1Util(encoding, null, privateKey, (int)sizeType), RSAKeyTypes.Pkcs8 => new RSAPkcs8Util(encoding, null, privateKey, (int)sizeType), _ => throw new NotSupportedException("Unknown RSA key type."), }; return(rsa); }