我正在尝试使用AES加密Android上的字符串.先前使用Diffie-Hellman算法确定对称密钥,并且似乎没问题(密钥长度为128位,见下文).
不过,我得到了一个 InvalidKeyException: "Key length not 128/192/256 bits."
码:
KeyAgreement keyAgree = KeyAgreement.getInstance("DH", "BC");
keyAgree.init(this.smartphonePrivKey);
keyAgree.doPhase(serverPubKey, true);
SecretKey key = keyAgree.generateSecret("AES");
System.out.println("Key Length: " + key.getEncoded().length);
System.out.println("Key Algorithm: "+ key.getAlgorithm());
System.out.println("Key Format: "+ key.getFormat());
byte[] encrypted = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
System.out.println("Allowed Key Length: "
+ cipher.getMaxAllowedKeyLength("AES"));
cipher.init(Cipher.ENCRYPT_MODE, key);
encrypted = cipher.doFinal("YEAH".getBytes("UTF8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) { …Run Code Online (Sandbox Code Playgroud) 问:我面临以下大问题:
从时间到另一个我发现以下异常:
Base-64 char数组的长度无效
我使用加密和解密:
public static string Encrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Encoding.UTF8.GetBytes(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
catch (Exception ex)
{
string message = ex.Message;
}
return string.Empty;
}
public static string Decrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
text = text.Replace(" ", "+")
Byte[] …Run Code Online (Sandbox Code Playgroud) 有几种方式(即使在这里),他们都提到保持密码在数据库上的最好方法是保存密码,而不是密码,但存储盐渍密码的哈希值.
我的问题很简单,把一些代码放在上面,这是正确的方法吗?
string username = "myUsr";
string password = "myPwd";
DateTime createDate = DateTime.UtcNow;
// Salt it
string saltedPwd = String.Concat(password, createDate.Ticks.ToString());
// Hash it
HMACSHA1 hash = new HMACSHA1(Encoding.Unicode.GetBytes(Helper.EncryptKey));
string encodedPwd = Convert.ToBase64String(
hash.ComputeHash(Encoding.Unicode.GetBytes(saltedPwd)));
// Create User in the database
db.CreateUser(username, encodedPwd, createDate);
Run Code Online (Sandbox Code Playgroud)
数据库用户表
user_id | username | password | create_date | last_access | active
Run Code Online (Sandbox Code Playgroud)
并在登录时使用再次执行该过程并检查是否encodedPwd与提供的盐渍密封密码相同.
我唯一担心的是,这是加密密码的最佳方法吗?是否可以使用创建日期(因为它总会改变,我读到salt每次编码密码时最好总是使用不同的...
或者应该是salt一个完全不同的变量?
我有一个PHP函数加密文本如下:
function encrypt($text)
{
$Key = "MyKey";
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $Key, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
Run Code Online (Sandbox Code Playgroud)
如何在Python中解密这些值?
我有一个解密/加密方法使用CCCrypt(),在iOS5上运行得非常好.现在我正在使用iOS6 SDK并且从未更改过我的代码,但似乎有些东西被破坏了.我仍然可以使用密钥对字符串进行加密并对其进行解密,但是如果我使用另一个密钥来解密相同的字符串,则即使在解密失败时,仍然CCCryptStatus会返回- 因为之后我的NSData未被填充.在iOS5上,我收到了错误消息-4303,我可以处理.任何想法现在可能出错了吗?CCCrypt()0(kCCSuccess)
我的代码:
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
if (encryptOrDecrypt == kCCDecrypt)
{
data = [GTMBase64 decodeData:data];
}
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(encryptOrDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
keyPtr,
kCCKeySizeAES256,
NULL ,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus != kCCSuccess){
// do something, …Run Code Online (Sandbox Code Playgroud) 我一直在尝试并且未能找到使用Entity Framework Code First加密SQL数据的好方法.我必须在前面加上我在Azure中托管并且无法访问本机SQL加密.
从SecurEntity获取一个页面,我已经完全实现了一种利用SaveChanges和ObjectMaterialized来处理实体加密/解密的方法,但在测试中我发现这一点太不可靠了.
以下是一些实现的示例:
public override int SaveChanges()
{
var pendingEntities = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager
.GetObjectStateEntries(EntityState.Added | EntityState.Modified)
.Where(en => !en.IsRelationship).ToList();
foreach (var entry in pendingEntities) //Encrypt all pending changes
EncryptEntity(entry.Entity);
int result = base.SaveChanges();
foreach (var entry in pendingEntities) //Decrypt updated entities for continued use
DecryptEntity(entry.Entity);
return result;
}
void ObjectMaterialized(object sender, ObjectMaterializedEventArgs e)
{
DecryptEntity(e.Entity);
}
Run Code Online (Sandbox Code Playgroud)
我已经看到其他帖子通过辅助属性手动加密/解密,如下所示:
public Value { get; set; }
[NotMapped]
public DecryptedValue
{
get { return Decrypt(this.Value); }
set { this.Value = …Run Code Online (Sandbox Code Playgroud) 我想知道最简单和最快的PHP代码行来检查mcrypt扩展是否可用/已安装.
有一个函数可以加密字符串,首先需要检查mcrypt是否可用.如果没有,它将执行系统上可用的替代加密解决方案.
谢谢!
我需要从*.cer文件中提取公钥(RSA).我想提取密钥并将其存储在一个.pem文件中,这样我就可以使用它的值来使用jsencrypt加密值.
以下命令将a转换.cer为.pem:
openssl x509 -inform der -in certificate.cer -out certificate.pem
Run Code Online (Sandbox Code Playgroud)
但它不会生成带有公钥的文件,而是生成带有文件内容的*.cer文件.
-----BEGIN CERTIFICATE-----
MIICPDCCAamgAwIBAg............
*lots of extra contents*
-----END CERTIFICATE-----
Run Code Online (Sandbox Code Playgroud)
我应该使用什么命令来提取公钥并将其存储在.pem文件中?
我使用以下代码将公钥和私钥转换为字符串
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
KeyPair keyPair = keyPairGen.genKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String publicK = Base64.encodeBase64String(publicKey.getEncoded());
String privateK = Base64.encodeBase64String(privateKey.getEncoded());
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试将其转换回公共广告私钥
PublicKey publicDecoded = Base64.decodeBase64(publicK);
Run Code Online (Sandbox Code Playgroud)
我收到错误,无法从byte []转换为公钥.所以我试过这样的
PublicKey publicDecoded = new SecretKeySpec(Base64.decodeBase64(publicK),"RSA");
Run Code Online (Sandbox Code Playgroud)
这会导致如下错误
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: Neither a public nor a private key
Run Code Online (Sandbox Code Playgroud)
看起来我在这里做了错误的密钥转换.任何帮助,将不胜感激.
我目前正在尝试使用NodeJS加密静态数据,我已经阅读了createCipher不推荐的Node API文档.
crypto.createCipher()的实现使用OpenSSL函数EVP_BytesToKey导出密钥,摘要算法设置为MD5,一次迭代,无盐.缺少盐允许字典攻击,因为相同的密码始终创建相同的密钥.低迭代计数和非加密安全散列算法允许非常快速地测试密码.
根据OpenSSL建议使用pbkdf2而不是EVP_BytesToKey,建议开发人员使用crypto.pbkdf2()自行派生密钥和IV,并使用crypto.createCipheriv()创建Cipher对象.
是createCipher仍然在静止数据加密一个可行的和安全的方式?该方法是否应被视为弃用?知情的攻击者是否可以对数据进行潜在解密?
应该createCipheriv始终优先使用解决方案createCipher吗?
任何其他细节或建议表示赞赏.