private void initKey() { try { /** * 获取秘钥 * 没有秘钥则产生一对秘钥,并用非明文方式保存秘钥 * */ String strKey = FileReadWrite.Read(); if (String.IsNullOrEmpty(strKey)) { Key = AesCryptoHelper.CreateKeyAndIv(); strKey = SerializationHelper.Serialization(Key); strKey = AesCryptoHelper.Encrypt(strKey, keyEncryptorKey); FileReadWrite.Write(strKey); } else { strKey = AesCryptoHelper.Decrypt(strKey, keyEncryptorKey); Key = SerializationHelper.Deserialization(strKey); } } catch (Exception ex) { var entity = LogEntityFactory.Create(String.Format("秘钥初始化失败:{0}", ex.ToString()), LogTypeFacotry.CreateExceptionLogType(), LogLevelFactory.CreateGravenessLogLevel()); log.SaveLog(entity); throw new Exception("秘钥初始化失败,秘钥文件被破坏!"); } }
public void SaveChanges() { /** * 序列化数据 * 对数据加密 * 将密文写入文件 * */ if (dataStor == null) { return; } String message = SerializationHelper.Serialization(dataStor); var bytes = Encryptor(Encoding.UTF8.GetBytes(message)); FileReadWrite.Write(Convert.ToBase64String(bytes)); }