我希望使用Callbacks方法在将值存储到我的数据库之前对其进行加密,然后在将值显示回应用程序之前对其进行解密.
我使用了文档中提供的一个示例.
在我,core.php我把以下内容:
Configure::write('Security.cipherCriptKey','su0HKssPmdbwgK6LdQLqzp0YmyaTI7zO');
Run Code Online (Sandbox Code Playgroud)
在我的模型中,我使用了两种方法:
beforeSave()
public function beforeSave($options = array()) {
$value=$this->data['Internship']['encryptedindb'];
$encrypted = Security::encrypt($value, Configure::read('Security.cipherCriptKey'));
$this->data['Internship']['encryptedindb'] = $encrypted;
return true;
}
Run Code Online (Sandbox Code Playgroud)afterFind()
public function afterFind($results, $primary = false) {
foreach ($results as $key => $val) {
if(isset($val['Internship']['encryptedindb'])){
$results['Internship']['encryptedindb'] = Security::decrypt($val['Internship']['encryptedindb'], Configure::read('Security.cipherCriptKey'));
}
return $results;
}
}
Run Code Online (Sandbox Code Playgroud)在beforeSave()似乎是做工精细,因为我可以在我的数据库加密的价值看.但是,在我看来,当我希望看到该字段的内容被解密时,它会将其显示为空字段.好像该afterFind()方法无法解密(它返回总是false).
以下是我的应用程序视图的屏幕截图:
和加密值的数据库:
我想在ssha中加密密码。存在一种方法吗?我找到了,但是在sha。
private String encrypt(final String plaintext) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage());
}
try {
md.update(plaintext.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage());
}
byte raw[] = md.digest();
String hash = (new BASE64Encoder()).encode(raw);
return hash;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的计算机科学课做一个名为"加密/解密"的项目
代码如下
import java.io.*;
import java.util.*;
import java.io.*;
import java.util.*;
public class Tester {
public static void main(String args[]) {
Scanner kbReader = new Scanner(System.in);
System.out.print("Enter a sentence that is to be encrypted: ");
String sntnc = kbReader.nextLine();
System.out.println("Original Sentence = " + sntnc);
Crypto myCryptObj = new Crypto();
String encryptdSntnc = myCryptObj.encrypt(sntnc);
System.out.println("Encrypted sentence = " + encryptdSntnc);
String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc);
System.out.println("Decrypted sentence = " + decryptdSntnc);
}
}
class Crypto {
public String encrypt(String sntnc) {
sntnc …Run Code Online (Sandbox Code Playgroud) 我是NodeJS的新手,我正在制作一个需要接受付款的应用程序.例如,用户必须输入他们的付款信息,但是在稍后的事件发生之前(可能是几天),付款将不会进行.所以,我需要在我的数据库中保存这些信息(我正在使用MongoDB)我想知道最好的方法是什么.我应该如何加密信用卡信息以将其存储在我的数据库中,并在事件发生时进行付款.我正在查看PayPal REST SDK,但我不确定这是否是最好的方法.什么是最好的库?
我知道这个问题很多,但是现在(2016年4月),为我的用户安全存储密码的首选方法是什么?我的数据不是超敏感的(它只是一个没有个人或财务细节的游戏,但拥有安全密码仍然很好).
今晚我已经阅读了大量的教程和问题,但目前还不清楚.我只想要一个相当简单易懂但相当安全的方法来获取创建的密码,将其存储在我的数据库中然后检索它.我已经尝试了几种方法,它们都工作正常,但后来有人说它不安全或者事情已经发生了变化.很高兴看到几个明确的功能来加密和解密密码.
编辑 - 澄清为什么新答案比stackoverflow上的先前答案更好.该语言已使用password_hash()和password_verify()进行了更新,极大地简化并改善了这种情况.以前的答案提供了手动解决方案,节省盐,使用不同的算法等.现在都不需要.
我的Java加密/解密算法的完整代码:
public class AESEncryptUtil {
private static AESEncryptUtil instance = new AESEncryptUtil();
private String password = "123456";
private Key key;
private Cipher cipher;
public AESEncryptUtil(){
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
key = new SecretKeySpec(enCodeFormat, "AES");
cipher = Cipher.getInstance("AES");
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] encrypt(String content) throws Exception {
byte[] byteContent = content.getBytes("utf-8");
instance.cipher.init(Cipher.ENCRYPT_MODE, instance.key);
byte[] result = instance.cipher.doFinal(byteContent);
return result;
}
public …Run Code Online (Sandbox Code Playgroud) 我不确定是否有人熟悉加密模块,但是我正在尝试加密代表字符串的变量。
例如:
string = input('String here')
Run Code Online (Sandbox Code Playgroud)
他们在模块页面上给出的示例是:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)
Run Code Online (Sandbox Code Playgroud)
一切都很好,但是当我尝试用一个变量替换“真正的秘密消息字符串”时,它不起作用。
如果用引号引起来,则只打印变量的名称(duh)
如果没有这样的引号:cipher_text = cipher_suite.encrypt(bstring),则表示未定义变量(也是duh)
但是,如果我只是将变量放入,它将给我一个错误: TypeError: data must be bytes.
有任何想法吗?谢谢!
从HSM检索私钥时解密数据时收到错误.
我在java.security中添加了sunpkcs11提供程序.因此,不要通过代码添加提供程序.文本已成功加密.但是,在解密加密文本时,我在下面的行中出现以下错误:
cipher.init(Cipher.DECRYPT_MODE, privateKey);
Run Code Online (Sandbox Code Playgroud)
我在这里失踪的是什么?
错误:
Caused by: java.security.InvalidKeyException: Private key must be instance of RSAPrivate(Crt)Key or have PKCS#8 encoding
at sun.security.pkcs11.P11RSAKeyFactory.implTranslatePrivateKey(P11RSAKeyFactory.java:101) [sunpkcs11.jar:1.7.0_85]
at sun.security.pkcs11.P11KeyFactory.engineTranslateKey(P11KeyFactory.java:132) [sunpkcs11.jar:1.7.0_85]
at sun.security.pkcs11.P11KeyFactory.convertKey(P11KeyFactory.java:65) [sunpkcs11.jar:1.7.0_85]
at sun.security.pkcs11.P11RSACipher.implInit(P11RSACipher.java:199) [sunpkcs11.jar:1.7.0_85]
at sun.security.pkcs11.P11RSACipher.engineInit(P11RSACipher.java:168) [sunpkcs11.jar:1.7.0_85]
at javax.crypto.Cipher.init(Cipher.java:1068) [jce.jar:1.7.0_85]
at javax.crypto.Cipher.init(Cipher.java:1012) [jce.jar:1.7.0_85]enter code here
Run Code Online (Sandbox Code Playgroud)
以下是代码:
import java.io.ByteArrayOutputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import javax.crypto.Cipher;
import javax.xml.bind.DatatypeConverter;
import sun.security.pkcs11.SunPKCS11;
public class App {
public static void main(String[] args) throws Exception {
try {
String passphrase = "mysecretkey";
SunPKCS11 provider …Run Code Online (Sandbox Code Playgroud) 我们正在尝试进行加密,支持java 7中的 AES/GCM/NoPadding 获得以下异常.
找不到任何支持AES/GCM/NoPadding的提供商
用于生成密码实例的代码示例如下.
SecretKeySpec eks = new SecretKeySpec(k, "AES");
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
c.init(Cipher.ENCRYPT_MODE, eks, new GCMParameterSpec(128, iv));
Run Code Online (Sandbox Code Playgroud) Realm使用AES-256进行加密和解密。而且,我正在尝试使用Android KeyStore生成/存储密钥,但是按照此页面-https://developer.android.com/training/articles/keystore.html#SecurityFeatures,Android仅在API 23和以上。
有人可以给我指出一个示例或其他有关如何使用加密领域来支持API 4.0及更高版本的信息吗?
谢谢。
encryption ×10
java ×5
php ×2
react-native ×2
aes ×1
aes-gcm ×1
android ×1
cakephp ×1
cakephp-2.x ×1
credit-card ×1
cryptography ×1
hsm ×1
javascript ×1
module ×1
mongodb ×1
mysql ×1
node.js ×1
python ×1
realm ×1
replace ×1
rsa ×1
security ×1
ssha ×1
string ×1