我正在尝试创建一个连接到数据库的Android应用程序,有不同的用户,他们都有加密密码。我创建了一个用于连接数据库的C#应用程序,它还使用以下算法创建这些密码:
String PKey = "key";
int intLenght = textBox1.Text.Length - 1;
string NewString = null;
NewString = textBox1.Text;
byte[] keyArray;
byte[] ToEncryptArray = UTF8Encoding.UTF8.GetBytes(NewString);
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(PKey));
hashmd5.Clear();
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.EBC;
tdes.Padding = PaddingMode.ANSIX923;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(ToEncryptArray, 0, ToEncryptArray.Length);
tdes.Clear();
string res = System.Convert.ToBase64String(resultArray, 0, resultArray.Length);
textBox2.Text = res;
我已经在android中复制了这个:
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(PKey.getBytes("UTF-8")); // <-- note encoding
byte[] toEncryptByte = encString.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("DESEDE/EBC/X9.23PADDING");
SecretKeySpec myKey = new SecretKeySpec(hash,"DESede");
cipher.init(Cipher.ENCRYPT_MODE, myKey);
byte[] encryptedPlainText = cipher.doFinal(toEncryptByte);
String encrypted = Base64.encodeToString(encryptedPlainText, 0);
return encrypted;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e){
e.printStackTrace();
} catch (InvalidKeyException e){
e.printStackTrace();
} catch (IllegalBlockSizeException e){
e.printStackTrace();
} catch (BadPaddingException e){
e.printStackTrace();
}
return null;
}
问题是Android似乎不支持EBC ANSIX923,因为它会抛出错误:
No provider found for DESEDE/EBC/X9.23PADDING
我试图避免在C#中更改填充模式或密码模式,因为我必须替换所有密码。
作为测试,我尝试了PKCS7填充,它对两者都有效。
Android是否存在EBC ANSIX923填充?
谢谢!