Team:Shanghai City/Model

Model

Part 1: Encode and Decode DNA sequence

Reference: Zakeri, B., Carr, P. A. & Lu, T. K. Multiplexed Sequence Encoding: A Framework for DNA Communication. PLoS One 11, e0152774, doi:10.1371/journal.pone.0152774 (2016).
We write a python and an exe. file to encode or decode DNA sequence. You can download here.

Encode code

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 25 17:17:29 2019
@author: Zokla
"""
dic={'\'':'CGG','1':'AGC','2':'TGA','3':'GAC','4':'CGA','5':'ACG','6':'TAG','7':'GCT','8':'TCG','9':'GCA','0':'CTG','-':'TTA','=':'GAA','end':'GAT','start':'ATC','Q':'AAC','W':'AAT','E':'TCA','R':'CGT','T':'ATG','Y':'CGC','U':'GTG','I':'GTA','O':'ACT','P':'TGT','{':'CTT','}':'TTG','\\':'ATT','A':'TGC','S':'CAT','D':'TCT','F':'GCG','G':'GAG','H':'CTC','J':'CCA','K':'TTC','L':'AGA',':':'TCC','~':'ACC','enter':'TGG','forward':'AGG','Z':'GGA','X':'AAG','C':'ATA','V':'GTT','B':'TAT','N':'GTC','M':'ACA',',':'CAG','.':'TAC','/':'TAA','reverse':'CAA','F1':'AAA','F2':'CCC','F3':'GGG','F4':'TTT',' ':'AGT','
':'CTA','F5':'GGC','\"':'CACCGG','!':'CACAGC','@':'CACTGA','#':'CACGAC','$':'CACCGA','%':'CACACG','^':'CACTAG','&':'CACGCT','*':'CACTCG','(':'CACGCA',')':'CACCTG','_':'CACTTA','+':'CACGAA',';':'CACTCC','<':'CACCAG','>':'CACTAC','?':'CACTAA'}
stri=raw_input('Please input your text to be encoded(uppercase).'+'\n')
result=''
for i in stri:
#print i
result+=dic[i]
print 'Encoded result:'+result

Decode code

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 25 16:12:22 2019
@author: Zokla
"""
dic={'CGG':'\'','AGC':'1','TGA':'2','GAC':'3','CGA':'4','ACG':'5','TAG':'6','GCT':'7','TCG':'8','GCA':'9','CTG':'0','TTA':'-','GAA':'=','GAT':'end','ATC':'start','AAC':'Q','AAT':'W','TCA':'E','CGT':'R','ATG':'T','CGC':'Y','GTG':'U','GTA':'I','ACT':'O','TGT':'P','CTT':'{','TTG':'}','ATT':'\\','TGC':'A','CAT':'S','TCT':'D','GCG':'F','GAG':'G','CTC':'H','CCA':'J','TTC':'K','AGA':'L','TCC':':','ACC':'~','TGG':'enter','AGG':'forward','GGA':'Z','AAG':'X','ATA':'C','GTT':'V','TAT':'B','GTC':'N','ACA':'M','CAG':'
,','TAC':'.','TAA':'/','CAA':'reverse','AAA':'F1','CCC':'F2','GGG':'F3','TTT':'F4','AGT':' ','CTA':' ','GGC':'F5'} dic_shift={'CGG':'\"','AGC':'!','TGA':'@','GAC':'#','CGA':'$','ACG':'%','TAG':'^','GCT':'&','TCG':'*','GCA':'(','CTG':')','TTA':'_','GAA':'+','TCC':';','CAG':'<','TAC':'>','TAA':'?'}
choose=raw_input('1.Input sequences directly 2.Import fasta file'+'\n')
print choose

Part 2: RSA Asymmetric Encryption

Reference materials:

https://blog.csdn.net/u014044812/article/details/80782448
https://blog.csdn.net/u014044812/article/details/80866759
The RSA system is used to encrypt the primer information, so that the sender of the information only needs to obtain the public key generated by the receiver, and the primer is encrypted according to the public key and then sent to the receiver, thereby increasing the security of information transmission.

Public Key Generation:

1. Select two unequal prime numbers p and q randomly(in practical applications, the larger the two prime numbers, the harder it is to crack. To simplify the process, we choose a relatively small prime number)
p=5074933 ;q=112517
2. n = p ∗ q = 5074933 ∗ 112517 = 571016236361
3. Calculate the Euler function of n
φ(n) = φ(p) × φ(q) = (p − 1) × (q − 1) = 5074932 × 112516 = 571011048912
4. Select an integer number e randomly with a condition that 1 < e < φ(n), but e and φ(n) are relatively prime. e=13 (In practice, 65537 is often chosen, but here we choose a smaller number to simplify the process)
5. Calculate e for the modulo inverse element of φ(n) d (d, k are positive integers)
Ed = 1(modeφ(n))
e × d − k × φ(n) = 1
d × 17 − k × 571011048912 = 1
Utilizing Excel, we find one set of solutions: d = 268711081841
6. Encapsulate n and e into a public key and key
Public key (n,e) (571016236361,17)
Key (n,d) (571016236361,268711081841)

Sending Message:

For example: Primer F: ATTATCCTGAATGAAAACTCGAGA
Primer R: ACATGATAACATGACTTCATGATA
1.The information which needs to be sent will be converted into a number based on a comparison table of bases and numbers (the form can be specified by itself and needs to be agreed between the recipient and the sender)

m must be less than n
2. Encrypt information with a public key
𝑚𝐹𝑒 = c(mode(n))
32987371697717 = c(mode(571016236361))
c𝐹 = 242351379467
𝑚𝑅𝑒 = c(mode(n))
63726378937217 = c(mode(571016236361)) c𝑅 = 475587785901

c𝐹 c𝑅 are the information which need to be sent.

Information reception and decryption:

1.After receiving the message, the receiver decrypts it by the formula cFd = mF(mode(n)).
242351379467268711081841 = mF(mode(571016236361))
mF = 329873716977
cRd = mR(mode(n))
475587785901268711081841 = mR(mode(571016236361))
mR = 637263789372
After that, according to the above table, the base sequence corresponding to the number is obtained.
Here is the process
Step 1:For the information receivers, they should generate public keys and private keys, and exhibit the public keys.
Step 2:For information senders, they encrypt the primer information with the public key and send it to the receiver.
Step 3: For information receivers, after receiving encrypted primer information, they should utilize the private key to unlock.
Step 4: For information senders, the information to be sent is encrypted by CADS and stored on the filter paper. After that, they can send it to the recipient.
Step 5: For information receivers, they should get the DNA on the filter paper, use the primer information to find the true DNA information and decrypt it.

Code&keyboard password table part
Steps:

1、Running the script requires python3 installed.
2、Then it can be run via the command line or also in the python text editor (pycharm, Jupyter notebook, etc.).
3、After running, the prompt will pop up to let the user select the desired function: input 0 means encryption is required, input 1 means decryption, and input 2 means exit procedure
.

4、If you enter 0, the program will prompt the user to enter the text that needs to be encrypted. Here is a demonstration, assuming that the character to be encrypted is ‘iGem’. After the program obtains the content that needs to be encrypted, it will generate a set of public key (in (n, e) format) and key (in (n, e, d, p, q) format) and the encrypted ciphertext ( n, e, d, p, q are all parameters used in the encryption process). The program will prompt you to enter the name of the document you want to save the ciphertext and key (requires ending in .txt), and the program will automatically write the corresponding ciphertext and key into the user-created document. The details of this encryption will also be displayed as follows:

In this case, two files have been created in the same directory of this script:

Message.txt is used to record encrypted text, and prikey.txt is used to record keys.

5、After opening message.txt, it is found that the content cannot be displayed normally. This is normal. Do not modify the content without authorization. The values shown here are not the same as those returned in the terminal because the cipher text display format is different.

In this case, two files have been created in the same directory of this script:

6、If the number 1 is selected in the menu interface, the decryption mode is entered. At this point, the program will prompt the user to enter the document that needs to be decrypted. Please enter the file name with the suffix .txt and the ciphertext already written. The user then needs to enter the values of n, d, p, q: the key document corresponding to the ciphertext, copy these parameters and enter. (There is no need to enter e here because e is usually a fixed value of 65537 in all RSA encryption)

7、The content in the last b'' is the decrypted information. b means that the information is stored in bytes.
8、If you enter the number 2 in the menu interface, the program will exit:

# 用python实现非对称性加密
# 公钥(rsa_n,rsa_e)用于加密
# 公钥(rsa_n,rsa_d)用于解密
import rsa
import binascii
class RsaCrypt():
def __init__(self,pubkey,prikey):
self.pubkey = pubkey
self.prikey = prikey
def encrypt(self,text):
self.ciphertext = rsa.encrypt(text.encode(),self.pubkey)
return self.ciphertext
def decrypt(self,text):
deciphertext = rsa.decrypt(text,self.prikey)
return deciphertext
def show_menu():
prompt = """ Do you want to encrypt message or decrypt message?
(0) Encrypt
(1) Decrypt
(2) Exit
My choice:
""" prompt0 = 'Please enter the message you want to encrypt:'
prompt1 = 'Please enter the file you want to decrypt:'
prompt2 = 'Please enter the name of the file you want to save your encrypted message:'
prompt3 = 'Please enter the name of teh file you want to save your private key (as the form (n value, e value, d value, p value, q value)): '
while True:
choice = input(prompt).strip()
if choice not in ['0','1','2']:
print('Your choice is not available.Please try again:')
continue
if choice == '2':
print('The program has been terminated')
break
if choice == '0':
pubkey,prikey = rsa.newkeys(528)
rs_obj = RsaCrypt(pubkey,prikey)
text = input(prompt0).strip()
filename= input(prompt2)
prikey_filename= input(prompt3)
ency_text = rs_obj.encrypt(text)
print('The encrypted message is:')
print(ency_text)
# print(type(ency_text))
print('Your private key is (n value, e value, d value, p value, q value):')
print(prikey)
# print(type(prikey))
print('Your public key is (n value, e value):')
print('Your encrypted message has been written in file '+filename+' as hex values ')
print(pubkey)
# print(type(pubkey))
with open(filename, 'wb') as f1:
f1.write(ency_text)
with open(prikey_filename, 'w') as f1:
f1.write(str(prikey))
if choice == '1':
filename = input(prompt1).strip()
with open(filename, 'rb') as f1:
ency_text = f1.read().strip()
# ency_text = input('Please enter encrypted message:').strip('\n')
nvalue = int(input('Please enter n value:').strip())
dvalue = int(input('Please enter d value:').strip())
pvalue = int(input('Please enter p value:').strip())
qvalue = int(input('Please enter q value:').strip())
rs_obj = RsaCrypt(rsa.key.PublicKey(nvalue, 65537), rsa.key.PrivateKey(nvalue, 65537, dvalue, pvalue,
qvalue))
print('Decrypted message is:')
print(rs_obj.decrypt(ency_text))
if __name__ == '__main__':
show_menu()


# pubkey,prikey = rsa.newkeys(528)
# rs_obj = RsaCrypt(pubkey,prikey)
# text = message
# ency_text = rs_obj.encrypt(text)
# print(ency_text)
# print(rs_obj.decrypt(ency_text))