我正在尝试找到一种好的设计模式,该模式允许我将用户的私人信息存储为加密的密文,同时使加密/解密对对象的用户来说是无缝的。
例如......假设我有一个 Patient 对象,该对象的属性是一些私人信息,例如社会安全号 (SSN)。我想将其作为加密值存储在数据库中,但允许应用程序代码使用以下语法获取/设置 SSN:
// Getting the unencrypted SSN
var currentSSN = selectedPatient.SSN;
// Setting the unencrypted SSN, but will be encrypted in Setter
selectedPatient.SSN = "555-55-5555";
Run Code Online (Sandbox Code Playgroud)
我尝试将加密/解密放在 getter 和 setter 中......
public string SSN
{
get
{
return MyEncryptionClass.Decrypt(this.SSN);
}
set
{
value = MyEncryptionClass.Encrypt(value);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:假设密钥和初始化向量均由加密/解密方法处理。我想重点关注获取/设置部分。
问题是我发现 SSN 以纯文本形式存储在数据库记录中,即使我在 Setter 中有 Encrypt 方法。我可以通过调试确认加密例程实际上返回了正确的密文,但它似乎没有存储在数据库记录中。我的想法是 Get/Set 有点循环。通过设置值,我调用解密方法,因此存储在记录中的内容实际上已被解密。
人们是否发现了一种有效的模式,可以让对象的使用者无缝地进行加密/解密。我想避免他们必须手动调用加密/解密方法。
编辑-我正在使用实体框架 v6
我有两个相互协作的类,但由于某种原因,其中一个类中的 set 方法不被视为在另一个类中定义。我目前正在学习 Dart(通过 Flutter),所以我想知道我是否可能遗漏了一些东西。
class ClassA {
List<ClassB> _bunchOfClassBs = [];
void doSomething() {
for(ClassB foo in _bunchOfClassBs) {
foo.addCount('bar'); // Undefined method
}
}
}
class ClassB {
int_counting = 0;
set addCount(int number) => _counting += number;
}
Run Code Online (Sandbox Code Playgroud) 我在kotlin 文档中找到了这段代码:
var stringRepresentation: String
get() = this.toString()
set(value) {
setDataFromString(value) // parses the string and assigns values to other properties
}
Run Code Online (Sandbox Code Playgroud)
我不明白this.toString()这里有什么。this指整个对象。为什么每次访问对象时都希望将其转换为字符串?真的应该这样field.toString()吗?(但这也是多余的)
我在c#中看到过这种代码:
private int id {get;set;}
Run Code Online (Sandbox Code Playgroud)
但我只会为该字段创建getter,如果有get和set,则与public字段相同,唯一的方法是:
public int getId(){return id;}
Run Code Online (Sandbox Code Playgroud)
如何在VS2010中自动生成getter
在Java中,建议在getter方法中返回字符串时创建另一个String.my_name是类的String字段.
public String getName()
{
String rString = my_name.toString();
return rString;
}
Run Code Online (Sandbox Code Playgroud) 我已经阅读了关于访问器方法的3种不同约定的社区wiki查询,并且看到以下约定并不令人惊讶:
const unsigned& amount() const { return _amount; }
unsigned& amount() { return _amount; }
Run Code Online (Sandbox Code Playgroud)
是的,它与无缝的完全不同,因为它能够完全避免括号() - 这会(我觉得)是想法 - 但它仍然是某种东西; 对?
我关于set和get方法的问题..虽然我知道如何使用以及为什么使用它,但我无法理解这些使用样式之间的主要区别......
public string a{get;set;}
public string a
{
get{return a;}
set{a=value;}
}
Run Code Online (Sandbox Code Playgroud) 我目前在PHP类示例中有以下__get/__ set方法:
class example{
/*Member variables*/
protected $a;
protected $b = array();
public function __get($name){
return $this->$name;
}
public function __set($name, $value){
$this->$name = $value;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,除了设置标准的受保护变量之外,我还希望能够在类中设置受保护的数组.我已经查看了一些其他问题,并找到了关于如何使用__get/__ set方法简单设置变量的一般建议,但没有任何允许使用这些魔术方法来设置BOTH数组和非数组的方法,即以下列方式:
$fun = new $example();
$fun->a = 'yay';
$fun->b['coolio'] = 'yay2';
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试用C++制作游戏.在我的代码中,我试图嵌套我的变量,以便我的主要没有很多包含.我现在的问题是我班上变量的值没有变化.单步执行代码会显示它设置值,但它不起作用.有谁知道发生了什么?先感谢您.
这是我到目前为止:
Location.h
#ifndef LOCATION_H
#define LOCATION_H
#include <string>
class Location
{
public:
Location(void);
Location(std::string name);
~Location(void);
std::string GetName();
void SetName(std::string value);
private:
std::string m_Name
};
#endif
Run Code Online (Sandbox Code Playgroud)
Location.cpp
#include "Location.h"
Location::Location(void): m_Name("") {}
Location::Location(std::string name): m_Name(name) {}
Location::~Location(void)
{
}
std::string Location::GetName()
{return m_Name;}
void Location::SetName(std::string value){m_Name = value;}
Run Code Online (Sandbox Code Playgroud)
PlayerStats.h
#ifndef PLAYERSTATS_H
#define PLAYERSTATS_H
#include "Location.h"
class PlayerStats
{
public:
PlayerStats(void);
~PlayerStats(void);
Location GetLocation();
void SetLocation(Location location);
private:
Location m_Location;
};
#endif
Run Code Online (Sandbox Code Playgroud)
PlayerStats.cpp
#include "PlayerStats.h"
PlayerStats::PlayerStats(void): m_Location(Location()) {}
PlayerStats::~PlayerStats(void)
{ …Run Code Online (Sandbox Code Playgroud) 最近,我在计算机科学课程中完成了一项任务,创建了一个由所述类构建的两个对象的类.
教授通过电子邮件批评的内容如下:"构造函数,getter和setter中的sysouts应该是主要方法."
他不会说英语,所以没有多大帮助.有人知道他在谈论我的代码时到底在说什么吗?这是我提交的代码:
public class Book {
int currentPage;
int nextPage;
int lastPage;
public Book(int pageNumber) {
currentPage = pageNumber;
System.out.println("You've opened the book to page " + currentPage + ".");
}
public void turnForward(int numTurned) {
nextPage = numTurned + currentPage;
System.out.println("You've turned forward to page " + nextPage + ".");
}
public void turnBack(int numTurned) {
lastPage = nextPage - numTurned;
if (lastPage <= 1) {
lastPage = 1;
System.out.println("You've turned back to the first page.");
}else { …Run Code Online (Sandbox Code Playgroud) getter-setter ×10
c# ×3
oop ×3
c++ ×2
getter ×2
java ×2
arrays ×1
class ×1
cloning ×1
coding-style ×1
cryptography ×1
dart ×1
encryption ×1
field ×1
kotlin ×1
php ×1
set ×1
setter ×1
string ×1
tostring ×1