我需要实现256位AES加密,但我在网上找到的所有示例都使用"KeyGenerator"生成256位密钥,但我想使用自己的密码.如何创建自己的密钥?我已经尝试将其填充为256位,但后来我得到一个错误,说密钥太长了.我确实安装了无限管辖区补丁,所以那不是问题:)
IE浏览器.KeyGenerator看起来像这样......
// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available
// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
Run Code Online (Sandbox Code Playgroud)
编辑
我实际上是将密码填充到256个字节,而不是位,这太长了.以下是我现在使用的一些代码,我对此有更多的经验.
byte[] key = null; // TODO
byte[] input = null; // TODO
byte[] output = null;
SecretKeySpec keySpec = null;
keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
output = cipher.doFinal(input)
Run Code Online (Sandbox Code Playgroud)
您需要自己做的"TODO"位:-)
我正在开发一个Android应用程序,并且在发送到数据库之前有一些我想要加密的字符串.我想要一些安全,易于实现的东西,每次传递相同的数据时都会产生相同的东西,并且最好会产生一个字符串,无论传递给它的字符串有多大,它都会保持不变.也许我正在寻找哈希.
我需要快速而简单的方法来加密/解密 "很多" 字符串数据.我试过jasypt,但它在我的Android手机上崩溃了.我有大约2000条记录(字符串).
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword("password");
String myEncryptedText = textEncryptor.encrypt(input);
Run Code Online (Sandbox Code Playgroud)
还有其他方法吗?我不需要极高的安全性,它需要快速!
我有以下加密数据的程序.
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Test {
private static final String ALGORITHM = "AES";
private static final byte[] keyValue = "ADBSJHJS12547896".getBytes();
public static void main(String args[]) throws Exception {
String encriptValue = encrypt("dude5");
decrypt(encriptValue);
}
/**
* @param args
* @throws Exception
*/
public static String encrypt(String valueToEnc) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
System.out.println("valueToEnc.getBytes().length "+valueToEnc.getBytes().length);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
System.out.println("encValue length" + encValue.length);
byte[] encryptedByteValue = …Run Code Online (Sandbox Code Playgroud) 我是密码学的新手.我希望学习如何加密和解密文件中的文本......当我在网上查阅相关文章时.我怀疑在同一文本上多次加密时加密文本对于单个文本是否相同?任何人都可以清楚我的怀疑吗?
我想将String转换为secretKey
public void generateCode(String keyStr){
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available
// Generate the secret key specs.
secretKey skey=keyStr; //How can I make the casting here
//SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用BASE64Decoder而不是secretKey,但我面临的一个问题是我无法指定密钥长度.
编辑: 我想从另一个地方调用此功能
static public String encrypt(String message , String key , int keyLength) throws Exception {
// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(keyLength); // 192 and 256 bits may not be available
// Generate …Run Code Online (Sandbox Code Playgroud) 我正在制作一个简单的程序,它在文本框中输入文本,并获取另一个文本框中的密码,然后对其进行某种简单加密并将其保存到文件中.之后,用户应该能够再次打开文件并提供用于加密的密码,并且应该吐出原始文本.
现在我正在拿绳子.将其分隔为char数组,然后对密码执行相同操作.之后,我获取密码,将所有这些字符转换为整数,找到所有字符的平均值,并将其用作原始文本中字符的偏移量.有一些像:
textChars[1]= (char)((int)textChars[1]+offset);
Run Code Online (Sandbox Code Playgroud)
然后我可以反过来加密字符串:
encryptedChars[1]= (char)((int)encryptedChars[1]-offset);
Run Code Online (Sandbox Code Playgroud)
问题是字符在不同平台上具有不同的值,因此有时偏移会将字符变成一些疯狂的数字(如负值),这只会将字符变成问号.
我查看了标准Java API中的加密库,但是如果每次启动程序时随机生成密钥,我会感到困惑.
我需要的是两个函数,它们看起来像是String encrypt(String text,String Password)用密码加密的文本作为解密它的密钥,并且String decrypt(String encryptedText, String Password)会吐出原始文本(如果密码是垃圾则会乱码)
任何帮助都非常感谢,这实际上只是一个个人项目,所以我不需要任何花哨的加密方法.