【发布时间】:2016-11-15 11:13:09
【问题描述】:
我的一些应用用户在解密序列化文件时报错。
Exception LocalTime: 07/08/2016 21:22:16 ServerTime: 07/08/2016 21:22:16 508 CryptographicException: Bad PKCS7 padding. Invalid length 137.
Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (PaddingMode padding, Int32 length, Int32 position)
Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
System.Security.Cryptography.CryptoStream.Read (System.Byte[] buffer, Int32 offset, Int32 count)
System.IO.BinaryReader.FillBuffer (Int32 numBytes)
System.IO.BinaryReader.ReadInt32 ()
System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadArrayOfPrimitiveType (System.IO.BinaryReader reader, System.Int64& objectId, System.Object& val)
System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info)
System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (System.IO.BinaryReader reader)
System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders,System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream)
这是我正在使用的代码:
public void Serialiable(){
if (!Directory.Exists (DirectoryPath)) {
Directory.CreateDirectory(DirectoryPath);
}
FileStream fs = new FileStream (FilePath, FileMode.OpenOrCreate);
CryptoStream cryptStream = new CryptoStream(fs, Encryptor, CryptoStreamMode.Write);
BinaryFormatter formatter = new BinaryFormatter ();
try{
formatter.Serialize(cryptStream,this);
}catch(System.Exception e){
Debug.LogError("Failed to serialize. Reason: "+e.Message);
}finally{
cryptStream.Close();
fs.Close();
}
}
public SerializableBase Deserialize(){
SerializableBase t = null;
if (File.Exists (FilePath)) {
FileStream fs = new FileStream (FilePath, FileMode.Open);
CryptoStream cryptStream = new CryptoStream(fs, Decryptor,CryptoStreamMode.Read);
try {
BinaryFormatter formatter = new BinaryFormatter ();
t = (SerializableBase)formatter.Deserialize (cryptStream);
t.Refresh();
} catch(System.Exception e){
Debug.LogError("Failed to deserialize. Reason: "+e.Message);
t = null;
}
finally {
if(cryptStream!=null){
cryptStream.Close();
}
fs.Close ();
}
}
return t;
}
[NonSerialized]
ICryptoTransform _Encryptor;
ICryptoTransform Encryptor {
get{
if(_Encryptor==null){
_Encryptor = RMCrypto.CreateEncryptor(RuntimeGlobalVariables.SerialKEY,RuntimeGlobalVariables.SerialIV);
}
return _Encryptor;
}
}
[NonSerialized]
ICryptoTransform _Decryptor ;
ICryptoTransform Decryptor {
get{
if(_Decryptor==null){
_Decryptor = RMCrypto.CreateDecryptor(RuntimeGlobalVariables.SerialKEY,RuntimeGlobalVariables.SerialIV);
}
return _Decryptor;
}
}
[NonSerialized]
RijndaelManaged _RMCrypto;
RijndaelManaged RMCrypto
{
get
{
if (_RMCrypto == null)
{
_RMCrypto = new RijndaelManaged();
}
return _RMCrypto;
}
}
string DirectoryPath
{
get
{
return Application.persistentDataPath + "/dat";
}
}
string FilePath {
get{
return DirectoryPath + "/" + FileName;
}
}
我没有设置 MODE 和 PADDING,所以它将是默认值 CipherMode.CBC 和 PaddingMode.PKCS7。我还检查了 SerialKEY 和 SerialIV 以确认它不会改变。事实上,有几个序列化文件但只有一个遇到了问题。
我试图减少异常。我已经尝试过:
- 用记事本编辑序列化文件并稍作改动
- 使用不同的 PADDING 或 MODE 来加密/解密
- 使用不同的 SerialKEY 或 SerialIV 进行加密/解密
但我无法得到异常:
CryptographicException: Bad PKCS7 padding. Invalid length 137.
只会得到另一个错误,例如:
Unexpected binary element: 100
我也用谷歌搜索,在stackoverflow中发现了一些类似的问题:
CryptographicException: Bad PKCS7 padding
但我没有得到有用的建议。
【问题讨论】:
-
最好显式设置所有参数。
标签: c# android encryption