serialize 파일의 암호를 해독 할 때 내 앱 사용자 중 일부가 오류를보고합니다. 이 기본 값 CipherMode.CBC 및 PaddingMode.PKCS7 것, 그래서 내가 모드와 패딩을 설정하지CryptographicException : PKCS7 패딩이 잘못되었습니다. 길이가 잘못되었습니다. 137
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;
}
}
: 여기
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)
내가 사용하고 코드입니다. 나는 또한 SerialKEY와 SerialIV가 change.in 사실이 아니라는 것을 확인하기 위해 체크한다. 몇몇 파일이 직렬화되지만 그들 중 하나만이 문제에 직면한다.
내가 시도했던 특례를 reduproduce하려고 :/
- 편집 직렬화 파일을 조금
- 사용하는 다른 패딩 또는 암호화 모드를 변경하거나 SerialIV는 암호화/
를 해독하지만 예외 얻을 수없는합니다 :
을만 같은 다른 오류 얻을 : 나는 또한 구글 검색
Unexpected binary element: 100
을, 유래 일부 유사한 문제를 발견
CryptographicException: Bad PKCS7 padding
하지만 유용한 제안을받을 did't.
모든 매개 변수를 명시 적으로 설정하는 것이 좋습니다. – zaph