The development of smart grids, made possible by advancements in computer technology, represents a transformative solution for electricity grids. These systems offer significant improvements in terms of security, reliability, and energy efficiency, while also promoting a low-carbon economy. Smart grids enable more efficient management of electricity supply and demand, as well as optimization of energy storage. However, despite these advantages, challenges persist, particularly in the areas of cybersecurity, and the reliability and speed of information processing. To address these challenges, we propose an innovative algorithm designed for energy management within electricity grids. This algorithm, specifically tailored for grids comprising hybrid storage systems, photovoltaic energy sources, and variable loads, aims primarily to balance supply and demand. This comprehensive approach not only ensures a continuous power supply but also reduces vulnerabilities associated with reliance on a single energy source. To evaluate the effectiveness of this algorithm, we conducted a series of rigorous simulation experiments using the MATLAB platform. These simulations meticulously tested the system's performance, providing substantial experimental evidence of its durability and viability in real-world scenarios. In summary, smart grids offer promising opportunities to enhance the security of electricity grids, maintain supply-demand balance, and promote sustainable energy storage practices. Despite persistent challenges in cybersecurity and information processing, our proposed algorithm represents a significant advancement in addressing these issues and strengthening the capabilities of smart grids.