【发布时间】:2013-06-19 17:21:04
【问题描述】:
所以我有一个 android 应用程序和一个用 python 编写的谷歌应用程序引擎服务器。 android 应用需要向服务器发送一些明智的信息,我这样做的方式是通过 http post。
现在我一直在考虑在发送数据之前在 android 中加密数据,一旦它在 gae 服务器上就解密它。
这就是我在 java 中加密和解密的方式:
private static final String ALGO = "AES";
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
// String encryptedValue = new BASE64Encoder().encode(encVal);
byte[] decoded = Base64.encodeBase64(encVal);
return (new String(decoded, "UTF-8") + "\n");
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue =Base64.decodeBase64(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(Constant.keyValue, ALGO);
return key;
}
这就是我尝试在服务器上解密的方式(我还不知道如何进行加密..也许你们也可以帮忙)
def decrypt(value):
key = b'1234567891234567'
cipher = AES.new(key, AES.MODE_ECB)
msg = cipher.decrypt(value)
return msg
当我查看日志时,我得到的字符串测试是:xVF79DzOplxBTMCwAx+hoeDJhyhifPZEoACQJcFhrXA=,因为它不是 16 的倍数(为什么,我猜这是因为 java 加密)我得到了错误
ValueError: 输入字符串的长度必须是 16 的倍数
我做错了什么?
【问题讨论】:
-
你为什么不使用 ssl(又名 https)?这应该提供在手机和 App Engine 之间安全、私密地传输数据所需的所有加密。
-
@Fh。任何教程/资源,我会看到如何做到这一点?我是个新手 :)
-
更长的答案添加为答案:)
标签: java android python google-app-engine pycrypto