깊은복사 얕은 복사
import sys
import random
LETTER = []
KEY_LETTER = []
for symbol in range(65, 91):
'''range(65, 91) => 65, 66, 67, 68, ..., 77, ... ,90, 91'''
LETTER.append(chr(symbol))
print(LETTER)
KEY_LETTER = LETTER[:]
random.shuffle(KEY_LETTER)
print(id(KEY_LETTER), id(LETTER))
print(LETTER)
print(KEY_LETTER)
'언어 > python' 카테고리의 다른 글
python random (0) | 2016.02.11 |
---|---|
시저 한글 ,영어(대, 소문자) 제어 버전 (0) | 2016.02.11 |
시저, 대치, 비즈네르 (0) | 2015.11.21 |
파이썬 (0) | 2015.09.29 |
python vs c (최소 공배수) (0) | 2015.09.18 |
시저, 대치, 비즈네르
#-*-coding: utf-8 -*-
'''
(1)작성자:김준현
'''
import random
''' ---------- SETTING ---------- '''
TEMP_CEA_LIST=[]
AlphaBat_String=''
TEMP_SUB_LIST=[]
SUB_String=''
for symbol in range(97, 123):
'''97, 98, 99, 100, ..., 122'''
TEMP_CEA_LIST.append(chr(symbol))
TEMP_SUB_LIST.append(chr(symbol))
while(True):
random.shuffle(TEMP_SUB_LIST)
if TEMP_SUB_LIST != TEMP_CEA_LIST:
break
AlphaBat_String = ''.join(TEMP_CEA_LIST)
SUB_String = ''.join(TEMP_SUB_LIST)
'''---------------------------------'''
'''--------- ceasar cipher ---------'''
class ceasar:
def __init__(self, p, k, c, pp):
''' p: 평서문, k: 비밀키(1~25), c: 암호문, pp:해독평문 '''
self.p = p
self.k = k
self.c = c
self.decryption_P = pp
def plainText_encrypt(self):
for charactor in self.p:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor) + self.k
if index > len(AlphaBat_String)-1:
index = index % len(AlphaBat_String)
self.c = self.c + AlphaBat_String[index]
else:
'''charactor not in AlphaBat_String'''
self.c = self.c +charactor
return self.c
def cipherText_decrypt(self):
for charactor in self.c:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor) - self.k
if index < 0:
index = index + len(AlphaBat_String)
self.decryption_P = self.decryption_P + AlphaBat_String[index]
else:
'''charactor not in AlphaBat_String'''
self.decryption_P = self.decryption_P + charactor
return self.decryption_P
'''--------- substitution cipher ---------'''
class substitutionCipher:
def __init__(self, p, c, pp):
self.p = p
self.c = c
self.decryption_P = pp
def plainText_encrypt(self):
for charactor in self.p:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor)
self.c = self.c + SUB_String[index]
else:
'''charactor not in AlphaBat_String'''
self.c = self.c +charactor
return self.c
def cipherText_decrypt(self):
for charactor in self.c:
if charactor in SUB_String:
index = SUB_String.find(charactor)
self.decryption_P = self.decryption_P + AlphaBat_String[index]
else:
'''charactor not in AlphaBat_String'''
self.decryption_P = self.decryption_P +charactor
return self.decryption_P
'''--------- vigenere cipher ---------'''
class vigenere:
def __init__(self, p, k, c, pp):
''' p: 평서문, k: 비밀키(1~25), c: 암호문, pp:해독평문 '''
self.p = p
self.k = k
self.c = c
self.decryption_P = pp
#cipherText = vigenere(plaintext, secretKey,cipherText,Result_plaintext).plainText_encrypt()
def plainText_encrypt(self):
i=0
for charactor in self.p:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor) + AlphaBat_String.find(self.k[i])
if index > len(AlphaBat_String)-1:
index = index % len(AlphaBat_String)
self.c += AlphaBat_String[index]
else:
self.c += charactor
i+=1
if i >len(self.k)-1:
i = 0
return self.c
def cipherText_decrypt(self):
i=0
for charactor in self.c:
if charactor in AlphaBat_String:
index = AlphaBat_String.find(charactor) - AlphaBat_String.find(self.k[i])
if index <0 :
index = index + len(AlphaBat_String)
self.decryption_P += AlphaBat_String[index]
else:
self.decryption_P += charactor
i+=1
if i >len(self.k)-1:
i = 0
return self.decryption_P
def main():
plaintext = input("plaintext is ")
plaintext = plaintext.lower()
cipherText = ''
Result_plaintext = ''
print("(1): ceasar cipher ")
print("(2): substitution cipher")
print("(3): vigenere cipher ")
while(True):
result = int(input("result: "))
#ceasar cipher
if result == 1:
secretKey = random.randint(1, 25)
cipherText = ceasar(plaintext, secretKey,cipherText,Result_plaintext).plainText_encrypt()
print("시저암호법: 평문[{0:s}] -> 암호문[{1:s}] ". format(plaintext,cipherText))
d = ceasar(plaintext, secretKey,cipherText,Result_plaintext).cipherText_decrypt()
print("시저암호법: 암호문[{0:s}] -> 평문[{1:s}] ". format(cipherText,d))
break
#substitution cipher
elif result == 2:
cipherText = substitutionCipher(plaintext,cipherText,Result_plaintext).plainText_encrypt()
print("대치암호법: 평문[{0:s}] -> 암호문[{1:s}] ". format(plaintext,cipherText))
d = substitutionCipher(plaintext,cipherText,Result_plaintext).cipherText_decrypt()
print("대치암호법: 암호문[{0:s}] -> 평문[{1:s}] ". format(cipherText,d))
break
#vigenere cipher
elif result == 3:
secretKey_SIZE = random.randint(1, len(plaintext))
secretKey = vigenere_secret_key_setting(secretKey_SIZE)
cipherText = vigenere(plaintext, secretKey,cipherText,Result_plaintext).plainText_encrypt()
print("비즈네르암호법: 평문[{0:s}] -> 암호문[{1:s}] ". format(plaintext,cipherText))
d = vigenere(plaintext, secretKey,cipherText,Result_plaintext).cipherText_decrypt()
print("비즈네르암호법: 암호문[{0:s}] -> 평문[{1:s}] ". format(cipherText,d))
break
else:
print("잘못입력했어")
def vigenere_secret_key_setting(secretKey_SIZE):
size = 0
tmp_key=''
while(size!=secretKey_SIZE-1):
tmp_key += AlphaBat_String[random.randint(0, len(AlphaBat_String)-1)]
size +=1
return tmp_key
if __name__=="__main__":
main()
'언어 > python' 카테고리의 다른 글
시저 한글 ,영어(대, 소문자) 제어 버전 (0) | 2016.02.11 |
---|---|
깊은복사 얕은 복사 (0) | 2015.12.16 |
파이썬 (0) | 2015.09.29 |
python vs c (최소 공배수) (0) | 2015.09.18 |
시저 (0) | 2015.09.12 |
'언어 > python' 카테고리의 다른 글
깊은복사 얕은 복사 (0) | 2015.12.16 |
---|---|
시저, 대치, 비즈네르 (0) | 2015.11.21 |
python vs c (최소 공배수) (0) | 2015.09.18 |
시저 (0) | 2015.09.12 |
python excel (0) | 2015.09.12 |
python vs c (최소 공배수)
left = input("left_value input: ")
right = input("right_value input: ")
gcd = 0
lcm = 0
left_temp = 0
right_temp = 0
def greatest_common_divisor(left, right):
i = 0
if left>=right:
for number in range(1,right+1):
if left%number == 0 and right%number == 0:
i = number
else:#left < right
for number in range(1,left+1):
if left%number == 0 and right%number == 0:
i = number
print i
return i
def least_common_divisor(left, right, gcd, left_temp, right_temp):
lcm = 1
left_temp = left/gcd
right_temp = right/gcd
lcm = lcm * left_temp * right_temp * gcd
return lcm
gcd = greatest_common_divisor(left, right)
print "gcd is {0:d}".format(gcd)
lcm = least_common_divisor(left, right, gcd, left_temp, right_temp)
print "lcm is {0:d}".format(lcm)
#include<stdio.h>
void value1_and_value2(int*, int*);
int greatest_common_divisor(int*, int*);
int main(void)
{
int a, b;
int temp_a = 0;
int temp_b = 0;
int gcd = 0;
value1_and_value2(&a, &b);
gcd = greatest_common_divisor(&a, &b);
printf("%d와 %d의 최대 공약수는 %d입니다.\n", a, b, gcd);
temp_a = a / gcd;
temp_b = b / gcd;
printf("%d와 %d의 최소 공배수는 %d입니다.\n", a, b, temp_a*gcd*temp_b);
return 0;
}
void value1_and_value2(int* L, int* R)
{
printf("value1 -> ");
scanf_s("%d", L);
printf("value2 -> ");
scanf_s("%d", R);
}
int greatest_common_divisor(int* L, int* R)
{
int i;//index
int temp = 0;
if (*L >= *R)
{
for (i = 1; i <= *R; i++)
{
if ((*L%i == 0) && (*R%i == 0))
{
temp = i;
}
}
}
else//(*L < *R)
{
for (i = 1; i <= *L; i++)
{
if ((*L%i == 0) && (*R%i == 0))
{
temp = i;
}
}
}
return temp;
}
'언어 > python' 카테고리의 다른 글
시저, 대치, 비즈네르 (0) | 2015.11.21 |
---|---|
파이썬 (0) | 2015.09.29 |
시저 (0) | 2015.09.12 |
python excel (0) | 2015.09.12 |
시저암호(수정할 것!!!) (0) | 2015.09.04 |
__author__ = 'User'
import random as rand
#(1)
def plain_text_function():
p = raw_input("plain_text input: ")
return p
#(2)
def encryption_function(p):
key = 3
cipher_text_temp = [0]*len(p)
#test printf-------------------
for i in range(0, len(p)):
print ord(p[i]),
print"before: ",
for i in range(0, len(p)):
print cipher_text_temp[i],
#------------------------------
for i in range(0, len(p)):
if ord(p[i]) == 32:
pass
else:
cipher_text_temp[i] = ord(p[i])-97
cipher_text_temp[i] = cipher_text_temp[i] + key
if cipher_text_temp[i]>25:
cipher_text_temp[i] = cipher_text_temp[i]%25
cipher_text_temp[i] = cipher_text_temp[i] + 97
#test printf-------------------
print"after: ",
for i in range(0, len(p)):
print cipher_text_temp[i],
#------------------------------
return cipher_text_temp
#(3)
def decryption_function(c):
key = 0
decryption_plain_text = [0]*len(c)
while(key!=26):
for i in range(0, len(c)):
if c[i] == 0:
pass
else:
decryption_plain_text[i] = c[i] - 97
decryption_plain_text[i] = decryption_plain_text[i] - key
if decryption_plain_text[i]<0:
decryption_plain_text[i] = decryption_plain_text[i]+25
decryption_plain_text[i] = decryption_plain_text[i] + 97
print"{0:02d} ".format(key+1),
for i in range(0, len(c)):
print chr(decryption_plain_text[i]),
key += 1
plain_text=" "
plain_text = plain_text_function()
print"plain_text is ",plain_text
cipher_text = encryption_function(plain_text)
print"cipher_test is ",
for i in cipher_text:
print chr(i),
print"-----------------------------------------"
plain_text_re = decryption_function(cipher_text)
print"-----------------------------------------"
'언어 > python' 카테고리의 다른 글
시저, 대치, 비즈네르 (0) | 2015.11.21 |
---|---|
파이썬 (0) | 2015.09.29 |
python vs c (최소 공배수) (0) | 2015.09.18 |
python excel (0) | 2015.09.12 |
시저암호(수정할 것!!!) (0) | 2015.09.04 |
python excel
'언어 > python' 카테고리의 다른 글
시저, 대치, 비즈네르 (0) | 2015.11.21 |
---|---|
파이썬 (0) | 2015.09.29 |
python vs c (최소 공배수) (0) | 2015.09.18 |
시저 (0) | 2015.09.12 |
시저암호(수정할 것!!!) (0) | 2015.09.04 |
시저암호(수정할 것!!!)
# -*-coding: utf-8 -*-
# a-> 97 ~ z-> 122
def Encript(PLAIN_TEXT, CIPHER_TEXT):
secret_key = int(input("secret key input: "))
###############################################################
#암호화 부분
for i in range(0, len(PLAIN_TEXT)):
if ord(PLAIN_TEXT[i]) == 32:
print"white space pass!!"
pass
else:
CIPHER_TEXT[i] = ord(PLAIN_TEXT[i]) -97 + secret_key
CIPHER_TEXT[i] = (CIPHER_TEXT[i]%26)+97
for i in range(0, len(PLAIN_TEXT)):
print chr(CIPHER_TEXT[i]),
###############################################################
#복호화 부분
for i in range(0, len(PLAIN_TEXT)):
if CIPHER_TEXT[i] == 0:
pass
else:
CIPHER_TEXT[i] = CIPHER_TEXT[i] - 97 - secret_key
if CIPHER_TEXT[i]<0:
CIPHER_TEXT[i] = (CIPHER_TEXT[i]+26)+97
elif CIPHER_TEXT[i]>0:
CIPHER_TEXT[i] = (CIPHER_TEXT[i]%26)+97
else:
CIPHER_TEXT[i] = CIPHER_TEXT[i] + 97
for i in range(0, len(PLAIN_TEXT)):
print chr(CIPHER_TEXT[i]),
###############################################################
#평문을 입력해라!!
plain_text = raw_input("plain_text input: ")
#평문의 길이 입니다.
plain_text_length = len(plain_text)
for i in range(0, plain_text_length):
print "[%02d]-> %2c, %03d"%(i+1, plain_text[i], ord(plain_text[i]))
#암호문을 만들 배열 입니다.
CIPHER_TEXT = [0]*plain_text_length
#평문을 암호화하는 함수
Encript(plain_text,CIPHER_TEXT)
'언어 > python' 카테고리의 다른 글
시저, 대치, 비즈네르 (0) | 2015.11.21 |
---|---|
파이썬 (0) | 2015.09.29 |
python vs c (최소 공배수) (0) | 2015.09.18 |
시저 (0) | 2015.09.12 |
python excel (0) | 2015.09.12 |