Androidフォンで実行されるPy4a(SL4aのPython)アプリケーションを開発しています。このアプリケーションは機密データを収集し、smtplib モジュールを使用して電子メールで送信します。必要な保護を確保するために、その情報を暗号化する必要があります。電話は安全でないデバイスと見なされるため、公開鍵暗号化を使用する必要があり、受信者の公開鍵のみを電話に保存する必要があります。
標準の Py4a ディストリビューションには、公開鍵暗号化をサポートするssl
との 2 つのパッケージが含まれていますgdata
。残念ながら、すぐに使用できる機能を提供するものはなく、秘密鍵を使用してより長い情報を暗号化できます。実際、ランダムな一時対称鍵を生成し、その鍵で情報を暗号化し、最後に受信者の公開鍵でこの鍵のみを暗号化する必要があることはわかっています。ただし、安全なソリューションを得るために考慮しなければならない詳細がいくつかあります...
ここで私の質問が来ます。Py4aに適した単純な暗号化ライブラリはありますか(つまり、Py4aですでに利用可能な暗号化ライブラリに基づいています-sslやgdata.Cryptoなど)、使いやすい公開鍵暗号化を提供しますか?
2013.06.13 更新
Py4a の gdata ライブラリでいくつかの実験を行いました。最後に、次の「クイック&ダーティ」ソリューションを取得しました。
import gdata.tlslite.utils.keyfactory as rk
#Generate the recipient's RSA key
sec=rk.generateRSAKey(1024)
#obtain the publickey, which will be stored
#in the sender mobile phone
pubxml=sec.writeXMLPublicKey()
print pubxml
#Create the public key from XML
pub=rk.parseXMLKey(pubxml)
#
#Now lets simulate the sender
#It has only access to "pub"
#
import gdata.tlslite.utils.PyCrypto_AES as sk
import gdata.tlslite.utils.cipherfactory as cf
#Generate random key and initioalization vectors
key=sk.getRandomBytes(32)
iv=sk.getRandomBytes(16)
#Here we should check if the key and iv are reasonable
#Now we accept them as they are
#Text to encrypt
txt1="Strictly secret unknown text!"
#Pad the text to the length N*16
padlen=16-(len(txt1) % 16)
if padlen:
txt1=txt1.ljust(len(txt1)+padlen, " ")
#Create the AES key
ak=cf.createAES(key.tostring(),iv.tostring())
#Encrypt text
ctxt1=ak.encrypt(txt1)
#Encrypt key and initialization vector with recipients publickey
ckey1=pub.encrypt(key+iv)
#
# Now we simulate the recipient
# It has its secret key 'sec', and received encrypted key
# and iv from the sender in ckey1. It also receives ctxt1
#
pkey1=sec.decrypt(ckey1)
pkey=pkey1[0:32]
piv=pkey1[32:48]
# Now we decipher the text
pak=cf.createAES(pkey.tostring(),piv.tostring())
ptxt1=pak.decrypt(ctxt1)
# Print the deciphered text
print ptxt1
おそらく、この解決策は最適とは言えませんが、少なくとも機能します。