我在python中製作了2個代碼,另外一個用於android(eclipse)中的加密和解密。 現在我想用python加密我的數據併發送給android來解密它。加密蟒蛇/解密android
如何使兩個不同的平臺來加密/解密數據? 每個平臺都有自己的加密和解密方式,所以我怎麼讓他們互相交談,發送數據和android提取被傳輸的確切信息呢?
需要幫助!
我在python中製作了2個代碼,另外一個用於android(eclipse)中的加密和解密。 現在我想用python加密我的數據併發送給android來解密它。加密蟒蛇/解密android
如何使兩個不同的平臺來加密/解密數據? 每個平臺都有自己的加密和解密方式,所以我怎麼讓他們互相交談,發送數據和android提取被傳輸的確切信息呢?
需要幫助!
Python代碼:
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
if not out_filename:
out_filename = in_filename + '.enc'
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
encryptor = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))
def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
if not out_filename:
out_filename = os.path.splitext(in_filename)[0]
with open(in_filename, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
iv = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, iv)
with open(out_filename, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(origsize)
Android Code:
public static final int SALT_LENGTH = 20;
public static final int PBE_ITERATION_COUNT = 1000;
private static final String RANDOM_ALGORITHM = "SHA1PRNG";
private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String TAG = Act.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
String password = "password";
String plainText = "plaintext message to be encrypted";
// byte[] salt = generateSalt();
byte[] salt = "dfghjklpoiuytgftgyhj".getBytes();
Log.i(TAG, "Salt: " + salt.length + " " + HexEncoder.toHex(salt));
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256);
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM);
SecretKey tmp = factory.generateSecret(pbeKeySpec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
byte[] key = secret.getEncoded();
Log.i(TAG, "Key: " + HexEncoder.toHex(key));
// PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
// byte[] encryptionSalt = generateSalt();
// Log.i(TAG, "Encrypted Salt: " + encryptionSalt.length + " " + HexEncoder.toHex(encryptionSalt));
// PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);
// byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
Log.i(TAG, encryptionCipher.getParameters() + " ");
byte[] iv = generateIv();
IvParameterSpec ivspec = new IvParameterSpec(iv);
encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
byte[] encryptedText = encryptionCipher.doFinal(plainText.getBytes());
Log.i(TAG, "Encrypted: " + HexEncoder.toHex(encryptedText));
Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
decryptionCipher.init(Cipher.DECRYPT_MODE, secret, ivspec);
byte[] decryptedText = decryptionCipher.doFinal(encryptedText);
Log.i(TAG, "Decrypted: " + new String(decryptedText));
} catch (Exception e) {
e.printStackTrace();
}
}
private byte[] generateSalt() throws NoSuchAlgorithmException {
SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
byte[] salt = new byte[SALT_LENGTH];
random.nextBytes(salt);
return salt;
}
private byte[] generateIv() throws NoSuchAlgorithmException {
SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
byte[] iv = new byte[16];
random.nextBytes(iv);
return iv;
}
}
我在android代碼中有一些probs實際上..你可以呃plz有什麼問題呢? ? 「HexEncoder.toHex()」給我一個錯誤,說明它沒有被定義......同樣,generateSalt()方法告訴它它沒有被使用。 所以需要幫助..和Thx! – SGLGG 2011-06-08 21:03:15
TLS可以用於在相容的方式來安全地傳輸數據。
如果還使用HTTP協議進行通信,然後已經有可能隱藏你的所有血淋淋的細節高層庫;只需提供客戶端/服務器證書並提出適當的請求。
它可能重新實現了從許多不良的安全功能,如forward secrecy救你。
如果你滿意的使用AES CBC mcrypt的,有一個在http://laurentcharignon.com/blog/?p=37 「跨語言加密/ AES與CBC(Python的/的Java/PHP)解密」 描述了一個簡單的解決方案。
是GenerateSalt()是一個解決方案嗎? – SGLGG 2011-06-06 22:56:16
發佈進行加密/解密的代碼。 – 2011-06-06 23:31:17
你使用什麼加密算法? – jterrace 2011-06-07 02:56:21