You have described The Legion of Space, a novel (short by today's standards) by Jack Williamson. It was originally published as a six-part serial in the April, May, June, July, August, and September, 1934 issues of of Astounding Stories, which are available at the Internet Archive (, , , , , ). It has been asked about before, e.g. here. Here is a plot summary from Wikipedia:
The Legion is the military and police force of the Solar System. It was created to keep the peace after the overthrow of the "Purples", a dynasty that ruled all humanity for generations. John Ulnar, a young graduate of the Legion academy, shares a surname with the Purples but is an enthusiastic supporter of the Legion.
A weapon called AKKA was used to defeat the Purples. Using a space/time distortion, it erases matter from the Universe—any matter, of any size, even a star or a planet. The secret of AKKA is kept in one family, descended from its creator, and is passed down from mother to daughter. One of the Legion's most important tasks is to guard the current Keeper, a beautiful young woman named Aladoree Anthar.
Through the machinations of his uncle, a powerful politician with a hidden agenda, John Ulnar is assigned to Aladoree's guard force at a secret fort on Mars. When she is kidnapped by a huge alien spaceship, John and the three other survivors of the guard force follow her kidnappers to a planet of Barnard's Star. They crash-land and must battle their way across a savage continent to the sole remaining citadel of the Medusae.
John Ulnar's uncle and his nephew have allied with the Medusae as a means to regain their empire, and have kidnapped Aladoree to ensure that AKKA is not used against them. The Medusae, however, turn on the Purples, seeking to destroy all humans and move to the Solar System, as their own world, far older than Earth, is spiraling into Barnard's Star.
John Ulnar and his companions rescue Aladoree, but the invasion of the Solar System has already begun. The Medusae conquer the Moon, set up bases there, and bombard Earth with gas projectiles. John, Aladoree, and their companions land on a ravaged Earth. Fighting off cannibals maddened by the gas, they build AKKA and destroy the Medusae fleets (and Earth's Moon as well).