private void Decrypt() { SecureData data = (SecureData)CryptoHelper.AESDecrypt(BindingContext as SecureData); TextMessage.Text = data.Data; NoteTitle.Text = data.NoteTitle; BindingContext = (SecureData)data; }
protected override async void OnAppearing() { List <SecureData> list = new List <SecureData>(); foreach (SecureData data in await App.NotesDatabase.GetDataAsync()) { list.Add((SecureData)CryptoHelper.AESDecrypt(data)); } NotesLists.ItemsSource = list; list = null; }
private XResult <String> Decode(XElement root, String responseBodyType) { String service = $"{this.GetType().FullName}.Decode(...)"; if (root == null) { return(new XResult <String>(null, ErrorCode.INVALID_ARGUMENT, new ArgumentNullException(nameof(root)))); } var responseBodyEl = root.Descendants(responseBodyType).FirstOrDefault(); if (responseBodyEl == null) { return(new XResult <String>(null, ErrorCode.XML_ELEMENT_NOT_EXIST, new RemoteException($"<{responseBodyType}> not found"))); } var errorCodeEl = responseBodyEl.Element("errorCode"); if (errorCodeEl != null && errorCodeEl.Value != "0000") { var errorMsgEl = responseBodyEl.Element("errorMsg"); var errorMsg = $"{errorCodeEl.Value}:{errorMsgEl.Value}"; return(new XResult <String>(null, ErrorCode.DEPENDENT_API_CALL_FAILED, new RemoteException(errorMsg))); } var digitalEnvEl = responseBodyEl.Descendants("digitalEnvelope").FirstOrDefault(); if (digitalEnvEl == null || String.IsNullOrWhiteSpace(digitalEnvEl.Value)) { return(new XResult <String>(null, ErrorCode.XML_ELEMENT_NOT_EXIST, new RemoteException("<digitalEnvelope> not found"))); } Byte[] digitalEnvData = null; try { digitalEnvData = Convert.FromBase64String(digitalEnvEl.Value); } catch (Exception ex) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "Convert.FromBase64String(...)", "数字信封的值不是有效的Base64字符串"); return(new XResult <String>(null, ErrorCode.DECODE_FAILED, ex)); } Byte[] key = null; using (var ms = new MemoryStream(digitalEnvData)) { var decryptKeyResult = CryptoHelper.RSADecrypt(ms, KeyConfig.Bill99FOHehuaPrivateKey, PrivateKeyFormat.PKCS8); if (!decryptKeyResult.Success) { return(new XResult <String>(null, ErrorCode.DECRYPT_FAILED, decryptKeyResult.FirstException)); } key = decryptKeyResult.Value; } var encryptedDataEl = responseBodyEl.Descendants("encryptedData").FirstOrDefault(); if (encryptedDataEl == null) { return(new XResult <String>(null, ErrorCode.XML_ELEMENT_NOT_EXIST, new RemoteException("<encryptedData> not found"))); } Byte[] encryptedData = null; try { encryptedData = Convert.FromBase64String(encryptedDataEl.Value); } catch (Exception ex) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "Convert.FromBase64String(...)", "encryptedData不是有效的Base64字符串"); return(new XResult <String>(null, ErrorCode.DECODE_FAILED, ex)); } var decryptedResult = CryptoHelper.AESDecrypt(encryptedData, key); if (!decryptedResult.Success) { return(new XResult <String>(null, ErrorCode.DECRYPT_FAILED, decryptedResult.FirstException)); } var signedDataEl = responseBodyEl.Descendants("signedData").FirstOrDefault(); if (signedDataEl == null) { return(new XResult <String>(null, ErrorCode.XML_ELEMENT_NOT_EXIST, new RemoteException("<signedData> not found"))); } Byte[] sign = null; try { sign = Convert.FromBase64String(signedDataEl.Value); } catch (Exception ex) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "Convert.FromBase64String(...)", "signedData不是有效的Base64字符串", ex); return(new XResult <String>(null, ErrorCode.DECODE_FAILED, new RemoteException("signedData不是有效的Base64字符串"))); } Byte[] signContent = decryptedResult.Value; var verifyResult = CryptoHelper.VerifySign(sign, signContent, KeyConfig.Bill99FOPublicKey, HashAlgorithmName.SHA1); if (!verifyResult.Value) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "verifyResult", "验签失败", verifyResult.FirstException); return(new XResult <String>(null, ErrorCode.SIGN_VERIFY_FAILED, new SignException("sign verify failed"))); } try { String resultXml = Encoding.UTF8.GetString(decryptedResult.Value); _logger.Trace(TraceType.BLL.ToString(), CallResultStatus.OK.ToString(), service, "resultXml", LogPhase.ACTION, "代付解析结果", resultXml); return(new XResult <String>(resultXml)); } catch (Exception ex) { return(new XResult <String>(null, ErrorCode.DECODE_FAILED, ex)); } }
public static XResult <TResponse> Execute <TRequest, TResponse>(String interfaceUrl, TRequest request) { if (request == null) { return(new XResult <TResponse>(default(TResponse), new ArgumentNullException(nameof(request)))); } String service = $"{typeof(Bill99UtilV1).FullName}.Execute(...)"; var client = GetClient(); var serializeResult = JsonUtil.SerializeObject(request); if (!serializeResult.Success) { return(new XResult <TResponse>(default(TResponse), serializeResult.FirstException)); } String postBody = serializeResult.Value; _logger.Trace(TraceType.BLL.ToString(), CallResultStatus.OK.ToString(), service, "postBody", LogPhase.ACTION, "请求消息体明文", postBody); Byte[] postData = Encoding.UTF8.GetBytes(postBody); //签名数据 var signedResult = SignUtil.MakeSign(postData, KeyConfig.Bill99_COE_v1_Hehua_PrivateKey, PrivateKeyFormat.PKCS8, "RSA"); if (!signedResult.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "signedResult", "生成签名数据失败", signedResult.FirstException, postBody); return(null); } var encryptKey = CryptoHelper.GenerateRandomKey(); //密文 var encryptedResult = CryptoHelper.AESEncrypt(postData, encryptKey); if (!encryptedResult.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "encryptedResult", "生成密文失败", encryptedResult.FirstException, postBody); return(null); } //数字信封 var digResult = CryptoHelper.RSAEncrypt(encryptKey, KeyConfig.Bill99_COE_v1_PublicKey); if (!digResult.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "digResult", "生成数字信封失败", digResult.FirstException); return(null); } var dic = new Dictionary <String, String>(3); dic["envelope"] = Convert.ToBase64String(digResult.Value); dic["encryptedData"] = Convert.ToBase64String(encryptedResult.Value); dic["signature"] = Convert.ToBase64String(signedResult.Value); String postJson = JsonUtil.SerializeObject(dic).Value; String requestUrl = $"{ApiConfig.Bill99_COE_v1_RequestUrl}{interfaceUrl}"; String traceMethod = $"{nameof(client)}.PostJson(...)"; _logger.Trace(TraceType.UTIL.ToString(), CallResultStatus.OK.ToString(), service, traceMethod, LogPhase.BEGIN, "快钱COE:开始请求快钱COE接口", new Object[] { requestUrl, postJson }); var result = client.PostJson(requestUrl, postJson); _logger.Trace(TraceType.UTIL.ToString(), (result.Success ? CallResultStatus.OK : CallResultStatus.ERROR).ToString(), service, traceMethod, LogPhase.ACTION, "快钱COE:结束请求快钱COE接口"); if (!result.Success) { _logger.Error(TraceType.UTIL.ToString(), CallResultStatus.ERROR.ToString(), service, traceMethod, $"快钱COE:调用快钱COE接口失败:{result.ErrorMessage}", result.FirstException); return(new XResult <TResponse>(default(TResponse), result.FirstException)); } if (result.Value == null) { _logger.Error(TraceType.UTIL.ToString(), CallResultStatus.ERROR.ToString(), service, traceMethod, $"快钱COE:调用快钱COE接口超时"); return(new XResult <TResponse>(default(TResponse), ErrorCode.REQUEST_TIMEOUT)); } try { String respString = result.Value.Content.ReadAsStringAsync().GetAwaiter().GetResult(); _logger.Trace(TraceType.UTIL.ToString(), CallResultStatus.OK.ToString(), service, traceMethod, LogPhase.END, "快钱COE:快钱COE返回结果", respString); var decodeResponseResult = JsonUtil.DeserializeObject <Dictionary <String, String> >(respString); if (!decodeResponseResult.Success) { return(new XResult <TResponse>(default(TResponse), ErrorCode.DESERIALIZE_FAILED, decodeResponseResult.FirstException)); } var respDic = decodeResponseResult.Value; String envelope = respDic["envelope"]; if (envelope.IsNullOrWhiteSpace()) { return(new XResult <TResponse>(default(TResponse), ErrorCode.INFO_NOT_EXIST, new ArgumentException($"快钱未返回{nameof(envelope)}字段"))); } Byte[] digitalEnvData = null; try { digitalEnvData = Convert.FromBase64String(envelope); } catch (Exception ex) { return(new XResult <TResponse>(default(TResponse), ErrorCode.DECODE_FAILED, ex)); } Byte[] key = null; using (var ms = new MemoryStream(digitalEnvData)) { var decryptKeyResult = CryptoHelper.RSADecrypt(ms, KeyConfig.Bill99_COE_v1_Hehua_PrivateKey, PrivateKeyFormat.PKCS8); if (!decryptKeyResult.Success) { return(new XResult <TResponse>(default(TResponse), ErrorCode.DECRYPT_FAILED, decryptKeyResult.FirstException)); } key = decryptKeyResult.Value; } String encryptedBase64String = respDic["encryptedData"]; Byte[] encryptedData = null; try { encryptedData = Convert.FromBase64String(encryptedBase64String); } catch (Exception ex) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "Convert.FromBase64String(...)", "encryptedData不是有效的Base64字符串"); return(new XResult <TResponse>(default(TResponse), ErrorCode.DECODE_FAILED, ex)); } var decryptedResult = CryptoHelper.AESDecrypt(encryptedData, key); if (!decryptedResult.Success) { return(new XResult <TResponse>(default(TResponse), ErrorCode.DECRYPT_FAILED, decryptedResult.FirstException)); } String signBase64String = respDic["signature"]; Byte[] sign = null; try { sign = Convert.FromBase64String(signBase64String); } catch (Exception ex) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "Convert.FromBase64String(...)", "signedData不是有效的Base64字符串", ex); return(new XResult <TResponse>(default(TResponse), ErrorCode.DECODE_FAILED, new RemoteException("signedData不是有效的Base64字符串"))); } Byte[] signContent = decryptedResult.Value; var verifyResult = CryptoHelper.VerifySign(sign, signContent, KeyConfig.Bill99_COE_v1_PublicKey, HashAlgorithmName.SHA1); if (!verifyResult.Value) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, "verifyResult", "快钱返回的数据验签失败", verifyResult.FirstException); return(new XResult <TResponse>(default(TResponse), ErrorCode.SIGN_VERIFY_FAILED, new SignException("快钱返回的数据验签失败"))); } try { String decryptedValue = Encoding.UTF8.GetString(decryptedResult.Value); _logger.Trace(TraceType.BLL.ToString(), CallResultStatus.OK.ToString(), service, nameof(decryptedValue), LogPhase.ACTION, "解密得到结果", decryptedValue); return(JsonUtil.DeserializeObject <TResponse>(decryptedValue)); } catch (Exception ex) { return(new XResult <TResponse>(default(TResponse), ErrorCode.DECODE_FAILED, ex)); } } catch (Exception ex) { return(new XResult <TResponse>(default(TResponse), ex)); } }
string Decrypt(string ciphertext) { return(CryptoHelper.AESDecrypt(ciphertext, _key, _iv)); }