Tôi thấy một tỷ lệ phần trăm nhỏ người dùng sản xuất ngẫu nhiên báo cáo ngoại lệ này liên quan đến việc mã hóa/giải mã chuỗi bằng Xamarin.Android nhưng tiếc là tôi không thể tạo lại nó.CryptographicException: Đệm PKCS7 không hợp lệ
Điều gì có thể gây ra điều này và/hoặc làm cách nào tôi có thể tạo lại ngoại lệ để tôi có thể tìm ra giải pháp sửa chữa/giải pháp?
[CryptographicException: Bad PKCS7 padding. Invalid length 147.]
Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException(PaddingMode padding, Int32 length, Int32 position):0
Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt(System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount):0
Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock(System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount):0
System.Security.Cryptography.CryptoStream.FlushFinalBlock():0
com.abc.mobile.shared.Security+PasswordEncoder.DecryptWithByteArray(System.String strText, System.String strEncrypt):0
EDIT: Dưới đây là đoạn code tôi đang sử dụng để mã hóa/giải mã
private string EncryptWithByteArray(string inPassword, string inByteArray)
{
byte[] tmpKey = new byte[20];
tmpKey = System.Text.Encoding.UTF8.GetBytes(inByteArray.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputArray = System.Text.Encoding.UTF8.GetBytes(inPassword);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(tmpKey, mInitializationVector), CryptoStreamMode.Write);
cs.Write(inputArray, 0, inputArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
private string DecryptWithByteArray (string strText, string strEncrypt)
{
try
{
byte[] tmpKey = new byte[20];
tmpKey = System.Text.Encoding.UTF8.GetBytes (strEncrypt.Substring (0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Convert.FromBase64String (strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream (ms, des.CreateDecryptor (tmpKey, mInitializationVector), CryptoStreamMode.Write);
cs.Write (inputByteArray, 0, inputByteArray.Length);
try {
cs.FlushFinalBlock();
} catch (Exception ex) {
throw(ex);
}
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
}
EDIT 2:
Chìa khóa mã hóa luôn là ID thiết bị địa phương. Đây là cách tôi đang nhận được điều này:
TelephonyManager telephonyMgr = Application.Context.GetSystemService(Context.TelephonyService) as TelephonyManager;
string deviceId = telephonyMgr.DeviceId == null ? "UNAVAILABLE" : telephonyMgr.DeviceId;
Dưới đây là một ví dụ về cách nó được gọi là:
string mByteArray = GetDeviceId();
string mEncryptedString = EncryptWithByteArray(stringToEncrypt, mByteArray);
string mDecryptedString = DecryptWithByteArray(mEncryptedString, mByteArray);
Mật mã khối và phương thức hoạt động nào? Bạn có thể cung cấp một bản sao của thông báo không giải mã được không? 147 có vẻ như một chiều dài kỳ lạ. Nó * thường * phải là bội số của độ dài khối mã khối (thường là 16). Tuy nhiên, các chế độ như CTS và CTR không có yêu cầu đó. Giả sử một chế độ như CBC, sau đó nó gần như trông giống như một thông điệp không đầy đủ đang được xử lý. – jww
Tôi đã thêm mã vào originalquestion =) –
dựa trên chỉnh sửa của bạn và mã sử dụng DES, 147 *** là sai. Kích thước khối của DES là 8 byte, vì vậy thông báo phải là bội số của 8 byte. Tìm 3 byte bị thiếu (nó phải là 152 byte), và vấn đề của bạn được giải quyết. Ngoài ra, bạn nên sử dụng một 'byte []', không phải là một 'Chuỗi', cho dữ liệu được mã hóa. Dữ liệu được mã hóa có thể bao gồm byte 0x00, có thể gây ra sự cố. Hoặc Base64 dữ liệu để nó có thể xử lý các byte NULL. – jww