์ƒˆ์†Œ์‹

Languages/Python

[์•”ํ˜ธ ํ”„๋กœํ† ์ฝœ] XOR๋ฅผ ์ด์šฉํ•œ ์•”ํ˜ธ | XOR ํ”„๋กœ์„ธ์Šค | XOR

  • -
๋ฐ˜์‘ํ˜•

https://www.itworld.co.kr/tags/2784/%EC%95%94%ED%98%B8%ED%99%94/132494


 

XOR ์—ฐ์‚ฐ

์•”ํ˜ธํ™” ๋ฐ ๋ณตํ˜ธํ™”์˜ XOR ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ผ๋ฐ˜ ํ…์ŠคํŠธ๋ฅผ ASCII ๋ฐ”์ดํŠธ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ 

XOR ํ”„๋กœ ์‹œ์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •๋œ ๋ฐ”์ดํŠธ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ ์„ ์ œ๊ณตํ•จ.

 

  • ๋น ๋ฅธ ๊ณ„์‚ฐ
  • ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์— ํ‘œ์‹œ๋œ ์ฐจ์ด ์—†์Œ
  • ์ดํ•ดํ•˜๊ณ  ๋ถ„์„ํ•˜๊ธฐ ์‰ฌ์›€

 

ํŠน์ง•

- XOR์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” key๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ, key๋Š” ์ž„์˜๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

- ์•”ํ˜ธํ™” ๋ณตํ˜ธํ™”๊ฐ€ ๊ฐ™๋‹ค

- xor์—ฐ์‚ฐ์€ ^ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

์ฐธ์กฐ

XOR ์•”ํ˜ธํ™”๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์•”ํ˜ธํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ ๋ฌด์ฐจ๋ณ„ ๋Œ€์ž… ๋ฐฉ์‹,

์ฆ‰ ์˜ฌ๋ฐ”๋ฅธ ์•”ํ˜ธ ํ…์ŠคํŠธ์™€ ์ผ์น˜ํ•˜๋Š” ์ž„์˜์˜ ์•”ํ˜ธํ™” ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ  ํฌ๋ž˜ํ‚นํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

 

www.tutorialspoint.com/cryptography_with_python/cryptography_with_python_xor_process.htm

 

Cryptography with Python - XOR Process - Tutorialspoint

Cryptography with Python - XOR Process In this chapter, let us understand the XOR process along with its coding in Python. Algorithm XOR algorithm of encryption and decryption converts the plain text in the format ASCII bytes and uses XOR procedure to conv

www.tutorialspoint.com

 

 

XOR ๊ต์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

์ถœ์ฒ˜ : ์œ„ํ‚ค๋ฐฑ๊ณผ

XOR ๊ต์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜(XOR swap algorithm) ๋˜๋Š” XOR ์Šค์™‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ž„์‹œ ๋ณ€์ˆ˜๋ฅผ ๋‘์ง€ ์•Š๊ณ , ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ๋ฐฐํƒ€์  ๋…ผ๋ฆฌํ•ฉ(XOR) ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์ด์šฉํ•˜์—ฌ ๊ต์ฒด(swap)ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ถ„์•ผ์—์„œ ํ”„๋กœ์„ธ์„œ์˜ ์ตœ์ ํ™” ๋Šฅ๋ ฅ์ด ๋ถ€์กฑํ–ˆ์„ ๋•Œ ๋Œ€์•ˆ์œผ๋กœ ์ž์ฃผ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค.

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

๋™์ž‘๊ณผ์ •์€ msg๊ธธ์ด๋งŒํผ key๊ณผ xor์—ฐ์‚ฐ์„ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

๋‹จ key๊ฐ€ msg๋ณด๋‹ค ์งง๋‹ค๋ฉด ๋‹ค์‹œ key์˜ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ์—ฐ์‚ฐํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.(์ดˆ๊ณผ x)

def encrypt_xor(msg,key):
    m_size = len(msg)
    k_size = len(key)
    ans = bytearray()
    
    for i in range(m_size):
        ans.append(msg[i] ^ key[i % k_size])
     
    return ans

def main():
    msg = 'hello world'
    key = 'haeun'
    enc = encrypt_xor(msg.encode(),key.encode())
    dec = encrypt_xor(enc, key.encode()) #enc๋Š” byteํ˜•ํƒœ์ด๋ฏ€๋กœ encode()๊ฐ€ ํ•„์š” ์—†์Œ.
    print("์›๋ฌธ : ",msg)
    print("์•”ํ˜ธํ™” : ",enc)
    print("๋ณตํ˜ธํ™” : ",dec.decode())

main()

 

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.