【发布时间】:2016-12-29 19:47:36
【问题描述】:
我已经在这里搜索了几乎所有关于这个主题的主题,但我仍然无法弄清楚哪里出了问题。我在我的 android 应用程序上创建了 3 个EditText:
EditText 插入密钥/密码以加密和解密的位置;
EditText2 显示加密文本的地方;
EditText3 显示解密文本的位置。
由于它仍然是一个早期测试,我已将消息或字符串作为应用程序中的变量放入 Crypt。
问题是加密给出了类似河豚算法的东西,所以没有问题(它以 2 == 结尾,所以我认为它工作正常)。我还尝试在解密之前对字符串进行解码,或者使用加密中的原始byte[],但没有任何好的结果。解密不会返回原始字符串文本,而是提供比加密文本更大的内容。我对河豚模式没有偏好,所以我从 Blowfish/CFB/NoPadding 开始。
str_key、str2 和 str3 暂时被宣布为公开。 str2 将为EditText2 字段设置文本,str3 将为EditText3 字段设置文本。
输出示例:
Example of output generated from the app
代码如下:
public void encrypt(){
//encrypt
EditText mEdit = (EditText)findViewById(R.id.editText);
str_key = (String) mEdit.getText().toString();
int iterationCount = 1000;
int keyLength = 256;
int saltLength = keyLength / 8;
SecureRandom random = new SecureRandom();
byte[] salt = new byte[saltLength];
random.nextBytes(salt);
KeySpec keySpec = new PBEKeySpec(str_key.toCharArray(), salt,
iterationCount, keyLength);
SecretKeyFactory keyFactory = null;
try {
keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] keyBytes = new byte[0];
try {
keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
SecretKey key = new SecretKeySpec(keyBytes, "Blowfish");
Cipher cipher = null;
try {
cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
if ( cipher == null || key == null) {
//throw new Exception("Invalid key or cypher");
str2="error";
}
else {
byte[] iv = new byte[cipher.getBlockSize()];
random.nextBytes(iv);
IvParameterSpec ivParams = new IvParameterSpec(iv);
try {
cipher.init(Cipher.ENCRYPT_MODE, key,ivParams);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
try {
raw = cipher.doFinal(message.getBytes("UTF-8"));
} catch (IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) {
e.printStackTrace();
}
str2 = Base64.encodeToString(raw,Base64.DEFAULT);
}
}
这里是解密函数:
public void decrypt(){
int iterationCount = 1000;
int keyLength = 256;
int saltLength = keyLength / 8;
SecureRandom random = new SecureRandom();
byte[] salt = new byte[saltLength];
random.nextBytes(salt);
KeySpec keySpec = new PBEKeySpec(str_key.toCharArray(), salt, iterationCount, keyLength);
SecretKeyFactory keyFactory = null;
try {
keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] keyBytes = new byte[0];
try {
keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
SecretKey key = new SecretKeySpec(keyBytes, "Blowfish");
Cipher cipher2 = null;
try {
cipher2 = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
iv = new byte[cipher2.getBlockSize()];
random.nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
try {
cipher2.init(Cipher.DECRYPT_MODE, key, ivSpec );
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
byte[] decryptedBytes = null;
byte[] app= Base64.decode(str2,Base64.DEFAULT);
try {
decryptedBytes = cipher2.doFinal(app);
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
str3 = Base64.encodeToString(decryptedBytes,Base64.DEFAULT);
}
【问题讨论】:
标签: java android encryption blowfish