public async Task <DecryptedResponse> Execute(DecryptOfficeDocumentParameter decryptOfficeDocumentParameter, string accessToken, AuthenticateParameter authenticateParameter) { if (decryptOfficeDocumentParameter == null) { throw new ArgumentNullException(nameof(decryptOfficeDocumentParameter)); } if (authenticateParameter == null) { throw new ArgumentNullException(nameof(authenticateParameter)); } _decryptOfficeDocumentParameterValidator.Check(decryptOfficeDocumentParameter); await _getOfficeDocumentAction.Execute(decryptOfficeDocumentParameter.DocumentId); var jsonWebKey = await _jsonWebKeyRepository.Get(decryptOfficeDocumentParameter.Kid); if (jsonWebKey == null) { throw new BaseDocumentManagementApiException(ErrorCodes.InvalidRequest, string.Format(ErrorDescriptions.TheJsonWebKeyDoesntExist, decryptOfficeDocumentParameter.Kid)); } var payload = Convert.FromBase64String(decryptOfficeDocumentParameter.Credentials); byte[] decryptedPayload = null; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { using (var provider = new RSACryptoServiceProvider()) { provider.FromXmlStringCore(jsonWebKey.SerializedKey); decryptedPayload = provider.Decrypt(payload, true); } } else { using (var rsa = new RSAOpenSsl()) { rsa.FromXmlString(jsonWebKey.SerializedKey); decryptedPayload = rsa.Decrypt(payload, RSAEncryptionPadding.OaepSHA1); } } var decryptedContent = Encoding.UTF8.GetString(decryptedPayload); var splitted = decryptedContent.Split('.'); return(new DecryptedResponse { Password = splitted[0], Salt = splitted[1] }); }
private UserConnection ProcessClient(Socket s) { var ss = Encoding.ASCII.GetBytes("aaaa can you see this??"); s.Send(ss); UserConnection conn = new UserConnection(); conn.Sock = s; conn.Aes = new AesCryptoServiceProvider(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { using (RSACng rsa = new RSACng(3072)) { conn.Sock.Send(rsa.ExportRSAPublicKey()); var aesKey = new byte[384]; conn.Sock.Receive(aesKey); conn.Aes.Key = rsa.Decrypt(aesKey, RSAEncryptionPadding.Pkcs1); } } else { using (RSAOpenSsl rsa = new RSAOpenSsl(3072)) { conn.Sock.Send(rsa.ExportRSAPublicKey()); var aesKey = new byte[384]; conn.Sock.Receive(aesKey); conn.Aes.Key = rsa.Decrypt(aesKey, RSAEncryptionPadding.Pkcs1); } } byte[] encryptedMsg; var header = new byte[20]; conn.Aes.IV.CopyTo(header, 0); using (MemoryStream mem = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(mem, conn.Aes.CreateEncryptor(), CryptoStreamMode.Write)) { using (StreamWriter sw = new StreamWriter(cs)) sw.Write("OK!"); encryptedMsg = mem.ToArray(); } } BitConverter.GetBytes(encryptedMsg.Length).CopyTo(header, 16); conn.Sock.Send(header); conn.Sock.Send(encryptedMsg); return(conn); }
public byte[] Decrypt( byte[] toBeDecrypted, JsonWebKey jsonWebKey) { #if UAP return(null); #elif NET46 || NET45 using (var rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(jsonWebKey.SerializedKey); return(rsa.Decrypt(toBeDecrypted, _oaep)); } #elif NETSTANDARD using (var rsa = new RSAOpenSsl()) { rsa.FromXmlString(jsonWebKey.SerializedKey); return(rsa.Decrypt(toBeDecrypted, RSAEncryptionPadding.Pkcs1)); } #endif }
public byte[] Decrypt( byte[] toBeDecrypted, JsonWebKey jsonWebKey) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { using (var rsa = new RSACryptoServiceProvider()) { rsa.FromXmlStringNetCore(jsonWebKey.SerializedKey); return(rsa.Decrypt(toBeDecrypted, _oaep)); } } else { using (var rsa = new RSAOpenSsl()) { rsa.FromXmlStringNetCore(jsonWebKey.SerializedKey); return(rsa.Decrypt(toBeDecrypted, RSAEncryptionPadding.Pkcs1)); } } }