我有一个类似的代码:
$cipher_alg = MCRYPT_RIJNDAEL_128;
$decrypted_string = mcrypt_decrypt($cipher_alg, $key,
$encrypted_string , MCRYPT_MODE_CBC, trim(hex2bin(trim($hexiv))));
Run Code Online (Sandbox Code Playgroud)
我担心在解码过程中mcrypt_decrypt会在后面或前面引入一个无偿的空格或空字符$decrypted_string.
我应该修剪它吗?
注意:我可以运行代码并找到它.但由于我无法运行足够的样本来证明(或反驳)我的观点,我想要一些具体的理论答案,可能是基于mcrypt_decrypt算法的内部工作.我问的另一个原因是我相信这会帮助别人.
注2:尽管有以下答案(现已删除,只有10K用户可以看到),但这里的示例似乎使用修剪来获取正确的解密字符串.
我对这类事情没有经验,所以我甚至不确定关键字(因此标题).基本上我需要一个双向功能
encrypt(w,x,y) = z
decrypt(z) = w, x, y
Where w = integer
x = string (username)
y = unix timestamp
Run Code Online (Sandbox Code Playgroud)
和z =是一个8位数字(可能包括字母,规格还没有.)
我希望z不容易猜测并且易于验证.速度不是一个大问题,安全也不是.跟踪一对一关系是主要要求.任何资源或方向将不胜感激.
编辑
谢谢你的答案,学到了很多东西.因此,为了澄清,8个字符是唯一的硬性要求,以及链接W < - > Z的能力.用户名(Y)和时间戳(Z)将被认为是锦上添花.
如果可能的话,我想以数学的方式做这个,而不是做一些数据库查找.
如果我今晚必须完成这个,我可以找到一个合适的哈希算法并使用一个查找表.我只是想扩大我对这类事物的理解,看看我是否可以用数学方法做到这一点.
我正在使用C#中的对称加密/解密例程.我知道之前有关于这个主题的一些问题,但大多数答案似乎是关于加密的哲学,而不是给出实际的代码.
更新:我真的很想看到一些代码,而不仅仅是链接.非常感谢!
此代码生成一对公钥/私钥:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair keypair = keyGen.genKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
Run Code Online (Sandbox Code Playgroud)
我想知道的是你通常如何存储公钥:
选项1:存储字节
byte[] privateKeyBytes = privateKey.getEncoded();
byte[] publicKeyBytes = publicKey.getEncoded();
// ... write to file
// convert bytes back to public/private keys
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
Run Code Online (Sandbox Code Playgroud)
我不喜欢的是将代码绑定到具体的实现,如PKCS8EncodedKeySpec和X509EncodedKeySpec.
选项2:存储模数和指数
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(publicKey, RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = …Run Code Online (Sandbox Code Playgroud) 我有一个简单的类来尝试包装加密以便在我的程序中的其他地方使用.
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
public final class StupidSimpleEncrypter
{
public static String encrypt(String key, String plaintext)
{
byte[] keyBytes = key.getBytes();
byte[] plaintextBytes = plaintext.getBytes();
byte[] ciphertextBytes = encrypt(keyBytes, plaintextBytes);
return new String(ciphertextBytes);
}
public static byte[] encrypt(byte[] key, byte[] plaintext)
{
try
{
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec spec = new SecretKeySpec(getRawKey(key), "AES");
cipher.init(Cipher.ENCRYPT_MODE, spec);
return cipher.doFinal(plaintext);
}
catch(Exception e)
{
// some sort of problem, return null because we can't encrypt it.
Utility.writeError(e); …Run Code Online (Sandbox Code Playgroud) 我正在编写一个开源的C#应用程序,我需要能够为每个用户存储已保存的登录信息.通常我只会加密密码,然后将其存储在用户设置文件中,但我担心由于代码是开源的,它有点无法加密它.因为所有人都要做的就是查看代码并获取加密密钥.
当然,它至少会比以纯文本存储的密码更难.但有没有像加密密码的好方法,但即使你有源码,它至少仍然非常难以解密?也许这样做至少几乎不可能在除了加密的计算机之外的任何计算机上解密?
编辑:澄清...我正在存储客户端密码,而不是密码来验证他们的服务登录.它是我无法控制的预先存在的Web服务的客户端.我只是想在本地存储密码以便自动登录...就像任何聊天客户端一样.
编辑2:完全对不起之前不清楚.但是密码必须在某些时候以明文检索并且哈希不是一个选项:(即使服务会让我传递密码哈希,有点打败目的,因为哈希会像密码一样好:P
我试图解密"~9?8?m???=?T?G"从后端服务器收到的字符串,该服务器使用OpenSSL使用AES-256-CBC加密字符串.有代码块:
public static String decryptText(String textToDecrypt) {
try {
byte[] base64TextToDecrypt = Base64.encodeBase64(textToDecrypt.getBytes("UTF-8"));
byte[] guid = "fjakdsjkld;asfj".getBytes("UTF-8");
byte[] iv = new byte[16];
System.arraycopy(guid, 0, iv, 0, guid.length);
IvParameterSpec ips = new IvParameterSpec(iv);
byte[] secret = DECRYPTION_SECRET_HASH.getBytes("UTF-8");
SecretKeySpec secretKey = new SecretKeySpec(secret, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// decryption pass
cipher.init(Cipher.DECRYPT_MODE, secretKey, ips);
byte[] converted = cipher.doFinal(base64TextToDecrypt);
System.out.println(new String(converted));
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Decipher error for " + textToDecrypt, e);
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我到达时
byte[] …Run Code Online (Sandbox Code Playgroud) 1.我有java函数加密xml文件并返回加密的String.
/// Java Class
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Crypt {
public static String key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
public static byte[] key_Array = Base64.decodeBase64(key);
public static String encrypt(String strToEncrypt)
{
try
{
//Cipher _Cipher = Cipher.getInstance("AES");
//Cipher _Cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
//Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
Key SecretKey = new SecretKeySpec(key_Array, "AES");
Cipher _Cipher = Cipher.getInstance("AES");
_Cipher.init(Cipher.ENCRYPT_MODE, SecretKey);
return Base64.encodeBase64String(_Cipher.doFinal(strToEncrypt.getBytes()));
}
catch (Exception e)
{
System.out.println("[Exception]:"+e.getMessage());
}
return null;
}
public static void main(String[] args) {
StringBuilder sb …Run Code Online (Sandbox Code Playgroud) 我已经运行了一个简单的测试,通过加密循环中的字节缓冲区来测量Java 9中的AES-GCM性能.结果有点令人困惑.原生(硬件)加速似乎有效 - 但并非总是如此.进一步来说,
为什么?
我的测试代码如下所示:
int plen = 1024*1024;
byte[] input = new byte[plen];
for (int i=0; i < input.length; i++) { input[i] = (byte)i;}
byte[] nonce = new byte[12];
...
// Uses SunJCE provider
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
byte[] key_code = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
SecretKey key = new SecretKeySpec(key_code, "AES");
SecureRandom random = new SecureRandom();
long total = 0;
while (true) {
random.nextBytes(nonce);
GCMParameterSpec …Run Code Online (Sandbox Code Playgroud) 我有一个发送文件的iMessage应用程序.使用CloudKit将数据保存在iCloud服务器中.一切都很好.
现在我想添加一个新的安全级别.我想在将数据发送到iCloud之前对其进行加密.
问题是使用公钥/私钥对是不可能的:
以下所有选项都不足以使数据安全:
iCloud可能是安全的,但用户的数据可能是个人/敏感的,所以我想添加一个新的安全层+这对我来说是一个有趣的练习.
任何人都可以帮助找到正确的加密方法吗?