public static byte[] Decrypt( byte[] encryptedData, byte[] pass, byte[] salt, CryptConfigFileHelperScryptParameters scryptParameters = null) { if (encryptedData == null || pass == null || salt == null) { throw new ArgumentNullException(); } var keyIvGenerated = GenKeyAndIv(pass, salt, scryptParameters); try { return(keyIvGenerated.DecryptData(encryptedData)); } catch (EnumException <AesKeyIvPair.EDecryptDataErrCodes> enumExc) { if (enumExc.ExceptionCode == AesKeyIvPair.EDecryptDataErrCodes.WrongKey) { throw EnumException.Create(EDecryptErrCodes.WrongPassword, innerException: enumExc); } throw; } }
public T1 GetValue <T1>( byte[] pass, CryptConfigFileHelperScryptParameters scryptParameters = null ) { try { return(Encoding.UTF8.GetString(CryptConfigFileHelper.Decrypt( EncryptedData, pass, Salt, scryptParameters )).ParseJsonToType <T1>()); } catch (EnumException <CryptConfigFileHelper.EDecryptErrCodes> enumExc) { if (enumExc.ExceptionCode == CryptConfigFileHelper.EDecryptErrCodes.WrongPassword) { throw EnumException.Create( EGetValueT1ErrCodes.WrongPassword, innerException: enumExc ); } throw; } }
/// <summary> /// 失败后的输出 /// </summary> /// <param name="exception"></param> /// <returns></returns> public static ResultDTO Error(EnumException exception) { return(new ResultDTO { status = false, info = exception.ToString() }); }
public static JsonRpcException Create <T1>(EnumException <T1> originExc) where T1 : struct, IConvertible { if (!typeof(T1).IsEnum) { throw new ArgumentException("T1 isn't enum"); } return(new JsonRpcException() { JsonErrorCode = originExc.ExceptionCode.ToInt32(CultureInfo.InvariantCulture), JsonErrorMessage = originExc.InnerMessage }); }
public static RpcRethrowableException Create <T1>( EnumException <T1> originExc ) where T1 : struct, IConvertible { if (!typeof(T1).IsEnum) { throw new ArgumentException("T1 isn't enum"); } return(Create( originExc.ExceptionCode, originExc.InnerMessage )); }
public byte[] DecryptData( byte[] encryptedData ) { var key = Key; Assert.NotNull(key); var iv = Iv; Assert.NotNull(iv); using (var aesAlg = new AesManaged()) { aesAlg.Key = key; aesAlg.IV = iv; ICryptoTransform decryptor = aesAlg.CreateDecryptor( aesAlg.Key, aesAlg.IV ); /**/ using (var input = new MemoryStream(encryptedData)) { using (var output = new MemoryStream( encryptedData.Length + 32) ) { using (var disposableBuffer = new TempByteArray(4096)) { var buffer = disposableBuffer.Data; try { using ( var csDecrypt = new CryptoStream( input, decryptor, CryptoStreamMode.Read ) ) { var read = csDecrypt.Read(buffer, 0, buffer.Length); while (read > 0) { output.Write(buffer, 0, read); read = csDecrypt.Read(buffer, 0, buffer.Length); } } } catch (CryptographicException cryptExc) { throw EnumException.Create( EDecryptDataErrCodes.WrongKey, innerException: cryptExc ); } } var totalLength = (int)output.Length; if (totalLength < 32) { throw EnumException.Create( EDecryptDataErrCodes.WrongEncryptedDataLength); } output.Seek(0, SeekOrigin.Begin); using ( var reader = new EndianBinaryReader( _littleConverter, output ) ) { using ( var tempDataHash = new TempByteArray( reader.ReadBytesOrThrow(32) ) ) { var data = reader.ReadBytesOrThrow(totalLength - 32); using (var mySha256 = new SHA256Managed()) { var computedDataHash = mySha256.ComputeHash(data); if ( tempDataHash.Data.SequenceEqual( computedDataHash ) ) { return(data); } } throw EnumException.Create( EDecryptDataErrCodes.WrongDecryptedDataHash ); } } } } } }
public static JObject GetErrJson(EnumException <EProcessRequestErrs> errExc) { if (errExc == null) { throw new ArgumentNullException("errExc"); } if (errExc.Tag == null) { errExc.Tag = 1; } var errJson = new JObject(); errJson["jsonrpc"] = "2.0"; errJson["id"] = null; var errDesc = new JsonRpcError(); switch (errExc.ExceptionCode) { case EProcessRequestErrs.ParseRequestStringError: errDesc.ErrorCode = -32701; errDesc.Message = "Byte[] to string conversion error"; break; case EProcessRequestErrs.ParseRequestJObjectError: errDesc.ErrorCode = -32700; errDesc.Message = "Parse json object error"; break; case EProcessRequestErrs.ParseJsonRpcError: errDesc.ErrorCode = -32600; errDesc.Message = "Parse json-rpc request error"; break; case EProcessRequestErrs.MethodNameNotFound: errDesc.ErrorCode = -32601; errDesc.Message = "The method does not exist / is not available"; errJson["id"] = JToken.FromObject(errExc.Tag); break; case EProcessRequestErrs.ParseParamEnumerableError: errDesc.ErrorCode = -32602; errDesc.Message = "Parse params object error"; errJson["id"] = JToken.FromObject(errExc.Tag); break; case EProcessRequestErrs.ParseParamItemError: errDesc.ErrorCode = -32602; errDesc.Message = "Deserialize params item error"; errJson["id"] = JToken.FromObject(errExc.Tag); break; case EProcessRequestErrs.ParseParamItemNullError: errDesc.ErrorCode = -32602; errDesc.Message = "Deserialize params item error: null item"; errJson["id"] = JToken.FromObject(errExc.Tag); break; case EProcessRequestErrs.NotEnoughParameters: errDesc.ErrorCode = -32602; errDesc.Message = "Not enough parameters"; errJson["id"] = JToken.FromObject(errExc.Tag); break; case EProcessRequestErrs.InvokeError: errDesc.ErrorCode = -32500; errDesc.Message = "Method invoke error"; errJson["id"] = JToken.FromObject(errExc.Tag); break; case EProcessRequestErrs.ConvertToReturnTypeError: errDesc.ErrorCode = -32603; errDesc.Message = "Serialize method result error"; errJson["id"] = JToken.FromObject(errExc.Tag); break; case EProcessRequestErrs.RethrowableException: var innerExc = (RpcRethrowableException)errExc.InnerException; errDesc.ErrorCode = -32501; errDesc.Message = JsonConvert.SerializeObject(innerExc.ErrorData); errJson["id"] = JToken.FromObject(errExc.Tag); break; case EProcessRequestErrs.JsonRpcException: var innerJsonExc = (JsonRpcException)errExc.InnerException; errDesc.ErrorCode = innerJsonExc.JsonErrorCode; errDesc.Message = innerJsonExc.JsonErrorMessage; errJson["id"] = JToken.FromObject(errExc.Tag); break; default: throw new Exception("Unknown exception code"); } errJson["error"] = JToken.Parse(errDesc.WriteObjectToJson()); return(errJson); }