Luke 22:35-38
35 And he said unto them, When I sent you without purse, and scrip, and shoes, lacked ye any thing? And they said, Nothing.
36 Then said he unto them, But now, he that hath a purse, let him take it, and likewise his scrip: and he that hath no sword, let him sell his garment, and buy one.
37 For I say unto you, that this that is written must yet be accomplished in me, And he was reckoned among the transgressors: for the things concerning me have an end.
38 And they said, Lord, behold, here are two swords. And he said unto them, It is enough.
Matthew 26:51-54
51 And, behold, one of them which were with Jesus stretched out his hand, and drew his sword, and struck a servant of the high priest's, and smote off his ear.
52 Then said Jesus unto him, Put up again thy sword into his place: for all they that take the sword shall perish with the sword.
53 Thinkest thou that I cannot now pray to my Father, and he shall presently give me more than twelve legions of angels?
54 But how then shall the scriptures be fulfilled, that thus it must be?
In Luke chapter 22, Jesus tells His disciples to buy a sword, if they don't have one. Then, when Jesus is arrested, and Peter attacks one of the arresting agents, Christ says "all they that take the sword shall perish with the sword". How do we reconcile these two concepts?
In Luke chapter 22, Christ was teaching that using weapons for self defense is legitimate, and was shortly going to be necessary, as He would no longer be with them (Matthew 9:15, 10:35-36).
So, was Peter's use of a sword legitimate? Was Peter being attacked? No, he wasn't. He was attacking those who had come to arrest Jesus, which was not what Christ wanted him to do (Matthew 26:53-54). This offensive action is what Christ is condemning when He says "all they that take the sword shall perish with the sword".
In other instances, however, God makes it clear that even beyond self defense, there are legitimate uses for weapons, as in the case of the government executing criminals:
Romans 13:4
4 For he is the minister of God to thee for good. But if thou do that which is evil, be afraid; for he beareth not the sword in vain: for he is the minister of God, a revenger to execute wrath upon him that doeth evil.
Therefore, the Lord teaches a nuanced view of weapons, where we are supposed to have them, and may legitimately use them, while at the same time, need to recognize that they can easily be used inappropriately. That's what we get from these two passages.