public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var encryptedBytes = Convert.FromBase64String(encryptedData); var bytes = Decrypt(privateKeyXml, encryptedBytes, rsaKeyLength); var data = Encoding.UTF8.GetString(bytes); return data; }
public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var bytes = Encoding.UTF8.GetBytes(data); var encryptedBytes = Encrypt(publicKeyXml, bytes, rsaKeyLength); string encryptedData = Convert.ToBase64String(encryptedBytes); return encryptedData; }
public static string Decrypt(string privateKey, string data, RsaKeyLengths length = RsaKeyLengths.Bit2048) { var dwKeySize = (int)length; // TODO: Add Proper Exception Handlers var rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize); rsaCryptoServiceProvider.FromXmlString(privateKey); int base64BlockSize = ((dwKeySize / 8) % 3 != 0) ? (((dwKeySize / 8) / 3) * 4) + 4 : ((dwKeySize / 8) / 3) * 4; int iterations = data.Length / base64BlockSize; var arrayList = new ArrayList(); for (int i = 0; i < iterations; i++) { byte[] encryptedBytes = Convert.FromBase64String( data.Substring(base64BlockSize * i, base64BlockSize)); // Be aware the RSACryptoServiceProvider reverses the order of // encrypted bytes after encryption and before decryption. // If you do not require compatibility with Microsoft Cryptographic // API (CAPI) and/or other vendors. // Comment out the next line and the corresponding one in the // EncryptString function. Array.Reverse(encryptedBytes); arrayList.AddRange(rsaCryptoServiceProvider.Decrypt(encryptedBytes, true)); } return(Encoding.Unicode.GetString(arrayList.ToArray(typeof(byte)) as byte[])); }
public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using var rsa = CreateRsa(rsaKeyLength); rsa.FromXml(privateKeyXml); byte[] bytes = rsa.Decrypt(encryptedBytes); return(bytes); }
public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { return(rsa.ExportParameters(includePrivateParameters: true)); } }
private static RSA CreateRsa(RsaKeyLengths rsaKeyLength) { var rsa = RSA.Create(); rsa.KeySize = (int)rsaKeyLength; return(rsa); }
public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { return rsa.ExportParameters(includePrivateParameters: true); } }
public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { return(rsa.ExportParameters(includePrivateParameters: true)); } }
public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var encryptedBytes = Convert.FromBase64String(encryptedData); var bytes = Decrypt(privateKeyXml, encryptedBytes, rsaKeyLength); var data = Encoding.UTF8.GetString(bytes); return(data); }
public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var bytes = Encoding.UTF8.GetBytes(data); var encryptedBytes = Encrypt(publicKeyXml, bytes, rsaKeyLength); string encryptedData = Convert.ToBase64String(encryptedBytes); return(encryptedData); }
public static string Encrypt(string text, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var bytes = Encoding.UTF8.GetBytes(text); var encryptedBytes = Encrypt(bytes, publicKey, rsaKeyLength); string encryptedData = Convert.ToBase64String(encryptedBytes); return(encryptedData); }
public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.ImportParameters(publicKey); return(rsa.Encrypt(bytes)); } }
public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.FromXmlString(publicKeyXml); return(rsa.Encrypt(bytes)); } }
public static string Decrypt(string encryptedText, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var encryptedBytes = Convert.FromBase64String(encryptedText); var bytes = Decrypt(encryptedBytes, privateKey, rsaKeyLength); var data = Encoding.UTF8.GetString(bytes); return(data); }
public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.FromXmlString(publicKeyXml); return(rsa.Encrypt(bytes, DoOAEPPadding)); } }
public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.ImportParameters(publicKey); return(rsa.Encrypt(bytes, DoOAEPPadding)); } }
public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using var rsa = CreateRsa(rsaKeyLength); return(new RsaKeyPair { PrivateKey = rsa.ToXml(includePrivateParameters: true), PublicKey = rsa.ToXml(includePrivateParameters: false), }); }
public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var rsaProvider = new RSACryptoServiceProvider((int)rsaKeyLength); return new RsaKeyPair { PrivateKey = rsaProvider.ToXmlString(true), PublicKey = rsaProvider.ToXmlString(false), }; }
private static byte[] Decrypt(string privateKeyXml, byte[] encryptedBytes, RsaKeyLengths rsaKeyLength) { using (var RSA = new RSACryptoServiceProvider((int) rsaKeyLength)) { RSA.FromXmlString(privateKeyXml); byte[] bytes = RSA.Decrypt(encryptedBytes, DoOAEPPadding); return bytes; } }
public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths length = RsaKeyLengths.Bit2048) { var rsaProvider = new RSACryptoServiceProvider((int)length); return(new RsaKeyPair { PrivateKey = rsaProvider.ToXmlString(true), PublicKey = rsaProvider.ToXmlString(false), }); }
private static byte[] Decrypt(string privateKeyXml, byte[] encryptedBytes, RsaKeyLengths rsaKeyLength) { using (var RSA = new RSACryptoServiceProvider((int)rsaKeyLength)) { RSA.FromXmlString(privateKeyXml); byte[] bytes = RSA.Decrypt(encryptedBytes, DoOAEPPadding); return(bytes); } }
public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.FromXmlString(privateKeyXml); byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding); return(bytes); } }
private static byte[] Encrypt(string publicKeyXml, byte[] bytes, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { byte[] encryptedBytes; using (var RSA = new RSACryptoServiceProvider((int) rsaKeyLength)) { RSA.FromXmlString(publicKeyXml); encryptedBytes = RSA.Encrypt(bytes, DoOAEPPadding); } return encryptedBytes; }
private static byte[] Encrypt(string publicKeyXml, byte[] bytes, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { byte[] encryptedBytes; using (var RSA = new RSACryptoServiceProvider((int)rsaKeyLength)) { RSA.FromXmlString(publicKeyXml); encryptedBytes = RSA.Encrypt(bytes, DoOAEPPadding); } return(encryptedBytes); }
public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { return new RsaKeyPair { PrivateKey = rsa.ToXmlString(includePrivateParameters: true), PublicKey = rsa.ToXmlString(includePrivateParameters: false), }; } }
/// <summary> /// Encrypt an arbitrary string of data under the supplied public key /// </summary> /// <param name="publicKey">The public key to encrypt under</param> /// <param name="data">The data to encrypt</param> /// <param name="length">The bit length or strength of the public key: 1024, 2048 or 4096 bits. This must match the /// value actually used to create the publicKey</param> /// <returns></returns> public static string Encrypt(string publicKey, string data, RsaKeyLengths length = RsaKeyLengths.Bit2048) { // full array of bytes to encrypt byte[] bytesToEncrypt; // worker byte array byte[] block; // encrypted bytes byte[] encryptedBytes; // length of bytesToEncrypt var dataLength = 0; // number of bytes in key var keySize = 0; // maximum block length to encrypt var maxLength = 0; // how many blocks must we encrypt to encrypt entire message? var iterations = 0; // the encrypted data var encryptedData = new StringBuilder(); // instantiate the crypto provider with the correct key length var rsaCryptoServiceProvider = new RSACryptoServiceProvider((int)length); // initialize the RSA object from the given public key rsaCryptoServiceProvider.FromXmlString(publicKey); // convert data to byte array bytesToEncrypt = Encoding.Unicode.GetBytes(data); // get length of byte array dataLength = bytesToEncrypt.Length; // convert length of key from bits to bytes keySize = (int)length / 8; // .NET RSACryptoServiceProvider uses SHA1 Hash function // use this to work out the maximum length to encrypt per block maxLength = ((keySize - 2) - (2 * SHA1.Create().ComputeHash(bytesToEncrypt).Length)); // how many blocks do we need to encrypt? iterations = dataLength / maxLength; // encrypt block by block for (int index = 0; index <= iterations; index++) { // is there more than one full block of data left to encrypt? if ((dataLength - maxLength * index) > maxLength) { block = new byte[maxLength]; } else { block = new byte[dataLength - maxLength * index]; } // copy the required number of bytes from the array of bytes to encrypt to our worker array Buffer.BlockCopy(bytesToEncrypt, maxLength * index, block, 0, block.Length); // encrypt the current worker array block of bytes encryptedBytes = rsaCryptoServiceProvider.Encrypt(block, true); // RSACryptoServiceProvider reverses the order of encrypted bytesToEncrypt after encryption and before decryption. // Undo this reversal for compatibility with other implementations Array.Reverse(encryptedBytes); // convert to base 64 string encryptedData.Append(Convert.ToBase64String(encryptedBytes)); } return encryptedData.ToString(); }
public static string Decrypt(string privateKey, string data, RsaKeyLengths length = RsaKeyLengths.Bit2048) { var dwKeySize = (int)length; // TODO: Add Proper Exception Handlers var rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize); rsaCryptoServiceProvider.FromXmlString(privateKey); int base64BlockSize = ((dwKeySize / 8) % 3 != 0) ? (((dwKeySize / 8) / 3) * 4) + 4 : ((dwKeySize / 8) / 3) * 4; int iterations = data.Length / base64BlockSize; var arrayList = new ArrayList(); for (int i = 0; i < iterations; i++) { byte[] encryptedBytes = Convert.FromBase64String( data.Substring(base64BlockSize * i, base64BlockSize)); // Be aware the RSACryptoServiceProvider reverses the order of // encrypted bytes after encryption and before decryption. // If you do not require compatibility with Microsoft Cryptographic // API (CAPI) and/or other vendors. // Comment out the next line and the corresponding one in the // EncryptString function. Array.Reverse(encryptedBytes); arrayList.AddRange(rsaCryptoServiceProvider.Decrypt(encryptedBytes, true)); } return Encoding.Unicode.GetString(arrayList.ToArray(typeof(byte)) as byte[]); }
public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { return RsaUtils.Encrypt(encryptedData, privateKeyXml, rsaKeyLength); }
public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { return RsaUtils.CreatePublicAndPrivateKeyPair(rsaKeyLength); }
public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { return rsa.ExportParameters(includePrivateParameters: true); } }
public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { return RsaUtils.Encrypt(data, publicKeyXml, rsaKeyLength); }
public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.FromXmlString(privateKeyXml); byte[] bytes = rsa.Decrypt(encryptedBytes); return bytes; } }
public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.ImportParameters(publicKey); var verified = rsa.VerifyData(dataToVerify, signature, hashAlgorithm); return verified; } }
public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.ImportParameters(publicKey); var verified = rsa.VerifyData(dataToVerify, hashAlgorithm, signature); return(verified); } }
public static byte[] Authenticate(byte[] dataToSign, RSAParameters privateKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.ImportParameters(privateKey); //.NET 4.5 doesn't let you specify padding, defaults to PKCS#1 v1.5 padding var signature = rsa.SignData(dataToSign, hashAlgorithm); return(signature); } }
public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { return(RsaUtils.Encrypt(data, publicKeyXml, rsaKeyLength)); }
public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.ImportParameters(privateKey); byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding); return(bytes); } }
/// <summary> /// Encrypt an arbitrary string of data under the supplied public key /// </summary> /// <param name="publicKey">The public key to encrypt under</param> /// <param name="data">The data to encrypt</param> /// <param name="length">The bit length or strength of the public key: 1024, 2048 or 4096 bits. This must match the /// value actually used to create the publicKey</param> /// <returns></returns> public static string Encrypt(string publicKey, string data, RsaKeyLengths length = RsaKeyLengths.Bit2048) { // full array of bytes to encrypt byte[] bytesToEncrypt; // worker byte array byte[] block; // encrypted bytes byte[] encryptedBytes; // length of bytesToEncrypt var dataLength = 0; // number of bytes in key var keySize = 0; // maximum block length to encrypt var maxLength = 0; // how many blocks must we encrypt to encrypt entire message? var iterations = 0; // the encrypted data var encryptedData = new StringBuilder(); // instantiate the crypto provider with the correct key length var rsaCryptoServiceProvider = new RSACryptoServiceProvider((int)length); // initialize the RSA object from the given public key rsaCryptoServiceProvider.FromXmlString(publicKey); // convert data to byte array bytesToEncrypt = Encoding.Unicode.GetBytes(data); // get length of byte array dataLength = bytesToEncrypt.Length; // convert length of key from bits to bytes keySize = (int)length / 8; // .NET RSACryptoServiceProvider uses SHA1 Hash function // use this to work out the maximum length to encrypt per block maxLength = ((keySize - 2) - (2 * SHA1.Create().ComputeHash(bytesToEncrypt).Length)); // how many blocks do we need to encrypt? iterations = dataLength / maxLength; // encrypt block by block for (int index = 0; index <= iterations; index++) { // is there more than one full block of data left to encrypt? if ((dataLength - maxLength * index) > maxLength) { block = new byte[maxLength]; } else { block = new byte[dataLength - maxLength * index]; } // copy the required number of bytes from the array of bytes to encrypt to our worker array Buffer.BlockCopy(bytesToEncrypt, maxLength * index, block, 0, block.Length); // encrypt the current worker array block of bytes encryptedBytes = rsaCryptoServiceProvider.Encrypt(block, true); // RSACryptoServiceProvider reverses the order of encrypted bytesToEncrypt after encryption and before decryption. // Undo this reversal for compatibility with other implementations Array.Reverse(encryptedBytes); // convert to base 64 string encryptedData.Append(Convert.ToBase64String(encryptedBytes)); } return(encryptedData.ToString()); }
public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.ImportParameters(publicKey); return rsa.Encrypt(bytes); } }
public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.FromXmlString(publicKeyXml); return rsa.Encrypt(bytes); } }
public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.ImportParameters(publicKey); var verified = rsa.VerifyData(dataToVerify, hashAlgorithm, signature); return verified; } }
public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { return(RsaUtils.CreatePublicAndPrivateKeyPair(rsaKeyLength)); }
public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { return(RsaUtils.Encrypt(encryptedData, privateKeyXml, rsaKeyLength)); }
public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.ImportParameters(privateKey); byte[] bytes = rsa.Decrypt(encryptedBytes); return bytes; } }
public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.FromXmlString(publicKeyXml); return rsa.Encrypt(bytes, DoOAEPPadding); } }
private static RSA CreateRsa(RsaKeyLengths rsaKeyLength) { var rsa = RSA.Create(); rsa.KeySize = (int)rsaKeyLength; return rsa; }
public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.ImportParameters(publicKey); return rsa.Encrypt(bytes, DoOAEPPadding); } }
public static string Decrypt(string encryptedText, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var encryptedBytes = Convert.FromBase64String(encryptedText); var bytes = Decrypt(encryptedBytes, privateKey, rsaKeyLength); var data = Encoding.UTF8.GetString(bytes); return data; }
public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.ImportParameters(privateKey); byte[] bytes = rsa.Decrypt(encryptedBytes); return(bytes); } }
public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.FromXmlString(privateKeyXml); byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding); return bytes; } }
public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.ImportParameters(publicKey); var verified = rsa.VerifyData(dataToVerify, signature, hashAlgorithm); return(verified); } }
public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength)) { rsa.ImportParameters(privateKey); byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding); return bytes; } }
public static string Encrypt(string text, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { var bytes = Encoding.UTF8.GetBytes(text); var encryptedBytes = Encrypt(bytes, publicKey, rsaKeyLength); string encryptedData = Convert.ToBase64String(encryptedBytes); return encryptedData; }
public static byte[] Authenticate(byte[] dataToSign, RSAParameters privateKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048) { using (var rsa = CreateRsa(rsaKeyLength)) { rsa.ImportParameters(privateKey); //.NET 4.5 doesn't let you specify padding, defaults to PKCS#1 v1.5 padding var signature = rsa.SignData(dataToSign, hashAlgorithm); return signature; } }