public void encryptFile(string FileName, string publicKey, bool OAEPpadding) { using (FileStream stReader = new FileStream(FileName, FileMode.Open, FileAccess.Read)) { using (FileStream stWriter = new FileStream(FileName + ".rsa", FileMode.OpenOrCreate, FileAccess.Write)) { RSACryptoServiceProvider rsa = PEM.ImportPublicKey(publicKey); int RSAHeadersize; if (OAEPpadding) { RSAHeadersize = 42; } else { RSAHeadersize = 11; } int MaxBufferlength = (rsa.KeySize / 8) - RSAHeadersize; long RemainingBytes = stReader.Length; if (ProgressConfigure != null) { ProgressConfigure(stReader.Length / MaxBufferlength); } byte[] buff; if (RemainingBytes > MaxBufferlength) { buff = new byte[MaxBufferlength]; } else { buff = new byte[RemainingBytes]; } int bytesread = 0; while ((bytesread = stReader.Read(buff, 0, buff.Length)) > 0) { byte[] buffEncrypted = rsa.Encrypt(buff, OAEPpadding); stWriter.Write(buffEncrypted, 0, buffEncrypted.Length); RemainingBytes = RemainingBytes - bytesread; if (RemainingBytes > MaxBufferlength) { buff = new byte[MaxBufferlength]; } else { buff = new byte[RemainingBytes]; } var eh = ProgressChanged; if (eh != null) { ProgressChanged(); } } } } }
public static string Encrypt(string publickKey, string inputString, bool OAEPpadding) { RSACryptoServiceProvider rsa = PEM.ImportPublicKey(publickKey); byte[] InputBytes = Encoding.ASCII.GetBytes(inputString); int RSAHeadersize; if (OAEPpadding) { RSAHeadersize = 42; } else { RSAHeadersize = 11; } int room = (rsa.KeySize / 8) - RSAHeadersize; int remainingBytes = InputBytes.Length; int startIndex = 0; byte[] outputBytes = new byte[0]; while (remainingBytes > 0) { int lengthToEncrypt; if (remainingBytes > room) { lengthToEncrypt = room; } else { lengthToEncrypt = remainingBytes; } byte[] block = new byte[lengthToEncrypt]; Array.Copy(InputBytes, startIndex, block, 0, lengthToEncrypt); byte[] encryptedBytes = rsa.Encrypt(block, OAEPpadding); byte[] tempBytes = new byte[outputBytes.Length + encryptedBytes.Length]; if (outputBytes.Length > 0) { Array.Copy(outputBytes, 0, tempBytes, 0, outputBytes.Length); } Array.Copy(encryptedBytes, 0, tempBytes, outputBytes.Length, encryptedBytes.Length); outputBytes = tempBytes; startIndex += lengthToEncrypt; remainingBytes -= lengthToEncrypt; } return(Convert.ToBase64String(outputBytes)); }
public static bool validate(string key, bool isprivate) { try { RSACryptoServiceProvider rsa; if (isprivate) { rsa = PEM.ImportPrivateKey(key); } else { rsa = PEM.ImportPublicKey(key); } return(true); } catch { return(false); } }
public static bool validateInputSize(string inputString, string publickKey, bool OAEPpadding) { RSACryptoServiceProvider rsa = PEM.ImportPublicKey(publickKey); int RSAHeadersize; if (OAEPpadding) { RSAHeadersize = 42; } else { RSAHeadersize = 11; } int room = (rsa.KeySize / 8) - RSAHeadersize; if (inputString.Length > room) { return(false); } return(true); }
public static bool Verify(string publicKey, string inputString, string signature, string HashAlgo) { RSACryptoServiceProvider rsa = PEM.ImportPublicKey(publicKey); return(rsa.VerifyData(Encoding.ASCII.GetBytes(inputString), getHashingAlgorithm(HashAlgo), Convert.FromBase64String(signature))); }