我想用自己的密钥使用AES加密字符串.但是我遇到了密钥的位长问题.你能查看我的代码,看看我需要修改/改变什么.
public static void main(String[] args) throws Exception {
String username = "bob@google.org";
String password = "Password1";
String secretID = "BlahBlahBlah";
String SALT2 = "deliciously salty";
// Get the Key
byte[] key = (SALT2 + username + password).getBytes();
System.out.println((SALT2 + username + password).getBytes().length);
// Need to pad key for AES
// TODO: Best way?
// Generate the secret key specs.
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
// Instantiate the cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encrypted = cipher.doFinal((secrectID).getBytes()); …Run Code Online (Sandbox Code Playgroud) 我想加密文件并将其存储在SD卡中.我想解密该加密文件并再次将其存储在SD卡中.我试图通过打开文件流加密文件并加密但是它不起作用.我想知道如何做到这一点.
我正在开发一个Android应用程序,它将从库中选择一些照片并隐藏它们,
我可以从图库中选择特定的图片并将其存储在我的应用程序中并从图库中删除它,但是如果他在sdcard中打开我的应用程序文件夹,那么一个人可以看到这些图片,所以我如何存储它们以便即使一个人检查我的SD卡然后他应该无法检测到那些照片?
所以我想在一个方法来解密消息,但它不工作,因为我需要做cipher.init(Cipher.ENCRYPT_MODE, secret)之前,我尝试添加new IvParameterSpec(iv)到cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));.否则,它只返回一个NullPointerException我想知道是否可以在方法中执行此操作而不是一直写入它.我真的不能想到一个解决方案,这就是为什么我在这里.加密工作正常但不解密.
项目运行: JRE 7
加密代码:
public static String encrypt(String str) {
try {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(str.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret); //<--- Need to do this before writing IvPerameterSpec,
// But I think that it's not possible if …Run Code Online (Sandbox Code Playgroud) 我正在制作一个 Android 应用程序,我想阻止用户打开某个文件夹。在该文件夹中,用户可以存储图像或视频文件。如果我能用密码保护那个文件夹就太好了。
哪个是最好的方法?