Double Righteousness
The Torah exhorts us to pursue justice. Towards that end, the Torah commands us to appoint judges and officers in all of our cities. That general exhortation is followed by a more specific one: “Tzedek, tzedek tirdof (you shall surely pursue righteousness).”
A more accurate translation would be, “Righteousness, righteousness you shall pursue.” The repetition of the word tzedek has led commentators to offer many interpretations.
One familiar interpretation is that we should pursue righteousness with righteousness actions. In other words, we should not try to bend the rules to achieve a correct and righteous outcome.  Judaism does not accept the notion that the end always justifies the means. Both the end, and the means to that end, must be righteous. To achieve tzedek, one must use tzedek.
The end of our Torah verse promises a reward for the pursuit of righteousness: “…so that you will live and take possession of the land that G‑d, your G‑d, gives you.”
The implication of this statement is that the only way we can live on our land is if we pursue unmitigated righteousness.
The words tzedek tzedek add up numerically to laMoshiach (“to Moshiach”). This formulation suggests that uncompromised righteousness is the force that leads us to Moshiach.
Furthermore, the word l’ma’an (“so that”) shares the numerical value of the word ketz (“the end [of days]”), another reference to the end of Galus and the beginning of the Final Redemption.
What is it about the “end not justifying the means” that places it as a necessary predicate to inheriting the land and to being a force to bring Moshiach and thus end our exile?
Kosher Parallels 
The Talmud (Chulin  109b) states that for every non-kosher food there is a parallel kosher food with a similar taste. This, commentators tell us, is to impress upon us that we desist partaking of non-kosher food not because it tastes bad, but because it is G‑d’s will. Proof of that motive is the fact that we may partake of a parallel kosher food even though it has the same taste as the non-kosher one.
Later sages extended this idea of parallel kosher and non-kosher things to other areas of life and Jewish practices. Literally speaking, every prohibition has a similar act that is permissible and, indeed, desirable.
One of the fundamental beliefs in Judaism is that G‑d provides for all of our needs and we should always trust that He will provide. Yet, when we see a person who is destitute, we should not just say G‑d will provide and then turn away.  Instead, we must act as if there were no one other than ourselves able to help that individual.
Based on the premise that every prohibition has a parallel act that is both permissible and desirable, we should ask “what is the kosher form of “the end justifying the means?”  When can we say that righteousness in the end will render the means righteous as well?
Liberating the Sparks
One of the principles of Judaism as seen through the eyes of Kabbalah is that the entire purpose of exile is to capture the sparks of holiness embedded within every physical phenomenon, location, time and experience. When we do a Mitzvah that involves a physical object we release the spark of G‑dly energy that is imprisoned within the object. That is a microcosm of the process of Redemption.
The Final Redemption does not begin when Moshiach takes us out of geographic and spiritual exile. It ends with Moshiach. The process of Redemption began the moment Adam and Eve partook of the forbidden fruit of the Tree of Knowledge. From that time onward the physical world suffered a major setback. Every aspect of our world became an admixture of the holy and the profane. Instead of the physical world serving as a reflection of the spiritual world; instead of seeing the Divine in everything; instead of seeing the hand of G‑d in every detail of creation, we see physicality and gross materiality.
From that time onward, G‑d and spirituality have been hidden away in a virtual prison.
How do we liberate G‑d from His imprisonment?
Every time we use a physical object, location, experience for a higher purpose that object, location, and experience will be elevated. It will no longer block and obscure the Divine; it will allow the Divine to be fully expressed.  
Thus, every effort on our part to reveal the Divine is a form of Redemption. The slightest positive gesture on our part has the capacity to redeem at least one small part of the world; the part of the world that was allotted to us. Each and every soul is given its body and a share of the world that becomes his or her “territory;” every soul is charged with transforming its share with holiness. When we use our bodies and our extended resources to serve G‑d through Torah and Mitzvos, we reveal the true Divine character of our world.
In the aggregate, with all the Mitzvos that our people have performed throughout the ages, we have liberated the Divine from its state of imprisonment in virtually all of the world. When we reach the final critical mass of liberation, Moshiach will take us out of the last bitter vestiges of exile; we will have successfully redeemed G‑d from His exile.  Moshiach’s role is not to begin the process, but to empower all of us to complete it.
Retroactive Holiness
While still in Galus we continue to engage in many activities that are devoid of a spiritual character. Nevertheless, when we use the energy or other resources that were generated by those activities they will be retroactively considered to have been liberated.
To illustrate the point: When we eat a meal and later use the energy derived from the food we ingested to help another person, that meal itself becomes foundational part of the Mitzvah. Retroactively, the rather mundane activity of eating has become a holy exercise, infused with G‑dly light.
To be sure, there are holy people for whom the very act of eating is infused with such spirituality that they don’t have to wait until they use the energy derived from the food to do a Mitzvah.  Their eating is inherently holy and immediately liberates the Divine spark embedded in the food. The majority of people, however, have to wait until they use the power generated by their meal to do good things. When that happens, they retroactively transform the mundane and non-spiritual activity of nourishment into a liberating Mitzvah experience.
This is an example of a kosher form of the negative attitude of “the end justifying the means.” The “means” here is the physical activity we engage in, the purchase or consumption of material goods, which are not in themselves holy. The “end” here is the Mitzvah we do as a result of the means, which then justifies the means by rendering it also holy and liberating.
Positive Reinterpretation
With this analysis behind us we can reinterpret the double expression of tzedek-righteousness in a positive fashion:
Instead of stating that our means should be righteous we can say that  there are times when the righteous goal retroactively renders the means righteous, even if that was not our intention at the time. The Torah is telling us that we shall indeed follow a pattern where our goal of righteousness justifies, validates and sanctifies the means we have used to achieve that goal. When we direct our energies towards the goal of bringing Moshiach, then any mundane activities which contribute in some fashion to the pursuit of that goal are rendered tzedek.
This recursive relationship, between the means, the end and then the means again, illustrates the connection between the double expression of righteousness – tzedek, tzedek - and Moshiach and Redemption. The entire process of Redemption involves the dynamic of the end transforming all that we do into acts of holiness and liberation. All of our lives are validated and transformed.
Thanking for the Past Suffering!
In a more general way, this concept of retroactive righteousness is connected to the suffering and pain we have experienced throughout our long history.  All of the persecutions, pogroms, expulsions and the hideous trial of the Holocaust cannot possibly be understood by the limited human mind.  We cannot give a satisfying answer to the impossible question of why so many bad things have happened to so many good people.
The prophet Isaiah (12:1), in describing the Messianic Age, states “I will thank You, O G‑d for having shown His anger to me.” That means that the present unfathomable suffering will ultimately be understood by us in the end. This will happen only because G‑d, and only G‑d, who transcends both the natural and the supernatural, can take that which defies rationality and make it rational and accessible to a finite human mind. In effect, in the “end” we will see the justification of the “means;” the why and wherefore of the centuries and millennia of suffering we have endured. All of life’s mysteries will be understood in hindsight.
One can ask the question, if, in the end, G‑d will demonstrate that everything that He did in the past was tzedek-righteous, why doesn’t He reveal that tzedek to us now? Why do the “means” (read: all the tragic events, particularly those that transpired in Galus-exile) have to be shrouded in mystery until the Messianic Age brings understanding? Why do we have to wait for the Redemption for all the apparent “wrongs” to be righted? 
The Rebbe once explained that if we were equipped with that knowledge, and understood the purpose of the pain and suffering, we would become inured to it and do nothing to stop it. By hiding His plan and concealing the righteousness of His ways, G‑d forces us to resist pain and suffering and do everything in our power to prevent it. And, paradoxically, by fighting the suffering we actually hasten the time when we will be able to thank G‑d for it, in the time when all suffering and pain will cease.