public static MemoryStream DecryptRsa(Stream inputStream, string pemKey) { AsymmetricKeyParameter keyParameter = ReadPem(pemKey); var engine = new RsaEngine(); engine.Init(false, keyParameter); var outputStream = new MemoryStream(); int inputBlockSize = engine.GetInputBlockSize(); int outputBlockSize = engine.GetOutputBlockSize(); byte[] inputBlock = new byte[inputBlockSize]; while (inputStream.Read(inputBlock, 0, inputBlock.Length) > 0) { byte[] outputBlock = engine.ProcessBlock(inputBlock, 0, inputBlockSize); int requiredPadding = outputBlockSize - outputBlock.Length; if (requiredPadding > 0) { byte[] paddedOutputBlock = new byte[outputBlockSize]; outputBlock.CopyTo(paddedOutputBlock, requiredPadding); outputBlock = paddedOutputBlock; } outputStream.Write(outputBlock, 0, outputBlock.Length); } outputStream.Seek(0, SeekOrigin.Begin); return(outputStream); }
public static string Decrypt(string CipherText, string mod, string exp) { var Modulus = HelperFunctions.FromHexToByte(mod); var Exponent = HelperFunctions.FromHexToByte(exp); var CipherTextBytes = Convert.FromBase64String(CipherText); var modulus = new BigInteger(1, Modulus); var exponent = new BigInteger(1, Exponent); var Parameters = new RsaKeyParameters(true, modulus, exponent); var RSAengine = new RsaEngine(); RSAengine.Init(false, Parameters); var blockSize = RSAengine.GetInputBlockSize(); var output = new List <byte>(); for (var chunkPosition = 0; chunkPosition < CipherTextBytes.Length; chunkPosition += blockSize) { var chunkSize = Math.Min(blockSize, CipherTextBytes.Length - chunkPosition * blockSize); output.AddRange(RSAengine.ProcessBlock(CipherTextBytes, chunkPosition, chunkSize)); } var output2 = output.ToArray(); var PlainText = Encoding.ASCII.GetString(output2); return(PlainText); }
public static byte[] Decrypt(byte[] data, AsymmetricKeyParameter privateKey) { if (privateKey == null || !privateKey.IsPrivate) { throw new ArgumentException("Not a valid decryption key"); } // purely mathematical RSA var engine = new RsaEngine(); // use padding var cipher = new OaepEncoding(engine); cipher.Init(false, privateKey); var length = data.Length; var blockSize = engine.GetInputBlockSize(); var plainTextBytes = new List <byte>(); for (var i = 0; i < length; i += blockSize) { var chunkSize = Math.Min(blockSize, length - i); plainTextBytes.AddRange(engine.ProcessBlock(data, i, chunkSize)); } return(plainTextBytes.ToArray()); }
public int GetBlockSize() { RsaEngine rsa = new RsaEngine(); rsa.Init(true, publicKey); return(rsa.GetInputBlockSize()); }
public byte[] Decrypt(byte[] cipher) { RsaEngine rsa = new RsaEngine(); rsa.Init(false, privateKey); int blockSize = rsa.GetInputBlockSize(); List <byte> output = new List <byte>(); for (int chunkPosition = 0; chunkPosition < cipher.Length; chunkPosition += blockSize) { int chunkSize = Math.Min(blockSize, cipher.Length - chunkPosition); byte[] tmp = cipher.ToList().GetRange(chunkPosition, chunkSize).ToArray().Reverse().ToArray(); output.AddRange(rsa.ProcessBlock(tmp, 0, tmp.Length)); } return(output.ToArray()); }
public byte[] Encrypt(byte[] message) { RsaEngine rsa = new RsaEngine(); rsa.Init(true, publicKey); int blockSize = rsa.GetInputBlockSize(); List <byte> output = new List <byte>(); for (int chunkPosition = 0; chunkPosition < message.Length; chunkPosition += blockSize) { int chunkSize = Math.Min(blockSize, message.Length - (chunkPosition * blockSize)); byte[] tmp = rsa.ProcessBlock(message, chunkPosition, chunkSize); output.AddRange(tmp); } return(output.ToArray()); }
/// <summary> /// Decrypts a file with a provided decryption key. /// </summary> /// <param name="filePath">An encrypted file</param> /// <param name="key">The RSA key in PEM format</param> /// <exception cref="ArgumentNullException">When the argument filePath is null</exception> /// <exception cref="ArgumentNullException">When the argument keyPath is null</exception> /// <returns>A memory stream with the decrypted file</returns> public static MemoryStream DecryptRsa(string filePath, string key) { if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } if (key == null) { throw new ArgumentNullException(nameof(key)); } AsymmetricKeyParameter keyParameter = GetKeyOrDefault(key); RsaEngine engine = new RsaEngine(); engine.Init(false, keyParameter); MemoryStream outputStream = new MemoryStream(); using (FileStream inputStream = File.OpenRead(filePath)) { int inputBlockSize = engine.GetInputBlockSize(); int outputBlockSize = engine.GetOutputBlockSize(); byte[] inputBlock = new byte[inputBlockSize]; while (inputStream.Read(inputBlock, 0, inputBlock.Length) > 0) { byte[] outputBlock = engine.ProcessBlock(inputBlock, 0, inputBlockSize); int requiredPadding = outputBlockSize - outputBlock.Length; if (requiredPadding > 0) { byte[] paddedOutputBlock = new byte[outputBlockSize]; outputBlock.CopyTo(paddedOutputBlock, requiredPadding); outputBlock = paddedOutputBlock; } outputStream.Write(outputBlock, 0, outputBlock.Length); } } outputStream.Seek(0, SeekOrigin.Begin); return(outputStream); }
/// <summary> /// Decrypts data using RSA methology with VisualGest PUBLIC KEY /// </summary> /// <param name="cipherText">string data to Decrypt</param> /// <returns></returns> public static string Decrypt(string cipherText, int encodingPageNumber = VGEST_ENCODING_PAGE) { string executableLocation = Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location); string pemFilename = Path.Combine(executableLocation, "RSAEncryption\\VGestKeys\\VGPubKey.pem"); //string pemFilename = "RSAEncryption\\VGestKeys\\VGPubKey.pem"; byte[] cipherTextBytes = Convert.FromBase64String(cipherText); RsaKeyParameters keys; using (var reader = File.OpenText(pemFilename)) keys = (RsaKeyParameters) new PemReader(reader).ReadObject(); // Pure mathematical RSA implementation RsaEngine eng = new RsaEngine(); // PKCS1 v1.5 paddings // Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine()); // PKCS1 OAEP paddings //OaepEncoding eng = new OaepEncoding(new RsaEngine()); eng.Init(false, keys); int length = cipherTextBytes.Length; int blockSize = eng.GetInputBlockSize(); List <byte> plainTextBytes = new List <byte>(); for (int chunkPosition = 0; chunkPosition < length; chunkPosition += blockSize) { int chunkSize = Math.Min(blockSize, length - chunkPosition); plainTextBytes.AddRange(eng.ProcessBlock( cipherTextBytes, chunkPosition, chunkSize )); } return(Encoding.GetEncoding(encodingPageNumber).GetString(plainTextBytes.ToArray())); }
public byte[] Decrypt(byte[] data, AsymmetricKeyParameter key) { if (key == null) { throw new Exception("licmanPrivateKey not Found: Please check your settings in SomeResources.resx"); } RsaEngine e = new RsaEngine(); e.Init(false, key); int blockSize = e.GetInputBlockSize(); List <byte> output = new List <byte>(); for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize) { int chunkSize = Math.Min(blockSize, data.Length - (chunkPosition * blockSize)); output.AddRange(e.ProcessBlock(data, chunkPosition, chunkSize)); } return(output.ToArray()); }