在java中存储/恢复加密密钥以解密字符串的简单方法 [英] Easy way to store/restore encryption key for decrypting string in java

查看:560
本文介绍了在java中存储/恢复加密密钥以解密字符串的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  SecretKey aesKey = KeyGenerator.getInstance(AES)generateKey() ; 
StringEncrypter aesEncrypt = new StringEncrypter(aesKey,aesKey.getAlgorithm());
String aesEncrypted = aesEncrypt.encrypt(StringContent);

如果我打印出aesKey,我得到:javax.crypto.spec.SecretKeySpec@1708d / p>

所以对于加密,我想问用户的密钥,但不知道应该是什么格式和什么格式。
我的计划是这样的:

  SecretKey aesKey = javax.crypto.spec.SecretKeySpec@1708d; 
StringEncrypter aesEncrypt = new StringEncrypter(aesKey,aesKey.getAlgorithm());
String aesDecrypt = aesEncrypt.decrypt(aesEncrypted);

但似乎它不工作。在加密到控制台后,有没有一些简单的方法来打印出密钥,以便用户可以保存(或记住它),然后使用解密?



整个代码在这里: 无法从文本文件,对称密钥实现中解密cyphertext。在Java中
所以我很抱歉再次发布,但我不知道如果代码是可读的(我是新手)。

解决方案

我最近不得不这样做。而在这里的其他答案让我走向正确的方向,这可能会更容易。所以这里是我的分享的一天,几个帮助方法简单的AES键操纵。 (注意对Apache Commons和Codec的依赖)。



现在这是一个git repo: github.com/stuinzuri/SimpleJavaKeyStore

  import static org.apache .commons.codec.binary.Hex *。 
import static org.apache.commons.io.FileUtils。*;
import java.io. *;
import java.security.NoSuchAlgorithmException;
import javax.crypto。*;
import org.apache.commons.codec.DecoderException;

public static SecretKey generateKey()throws NoSuchAlgorithmException
{
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
keyGenerator.init(256); // 128 default; 192和256也可能
return keyGenerator.generateKey();
}

public static void saveKey(SecretKey key,File file)throws IOException
{
char [] hex = encodeHex(key.getEncoded());
writeStringToFile(file,String.valueOf(hex));
}

public static SecretKey loadKey(File file)throws IOException
{
String data = new String(readFileToByteArray(file));
byte []编码;
try {
encoded = decodeHex(data.toCharArray());
} catch(DecoderException e){
e.printStackTrace();
返回null;
}
返回新的SecretKeySpec(编码AES);
}


For encryption I use something like this:

SecretKey aesKey = KeyGenerator.getInstance("AES").generateKey();
StringEncrypter aesEncrypt = new StringEncrypter(aesKey, aesKey.getAlgorithm());
String aesEncrypted= aesEncrypt.encrypt(StringContent);

If I print out aesKey I get: "javax.crypto.spec.SecretKeySpec@1708d".

So for encryption I would like to ask user for key but dont know how and what format should it be. My plan was something like this:

SecretKey aesKey = javax.crypto.spec.SecretKeySpec@1708d;
StringEncrypter aesEncrypt = new StringEncrypter(aesKey, aesKey.getAlgorithm());
String aesDecrypt = aesEncrypt.decrypt(aesEncrypted);

But seems its not working. Is there some easy way to print out the key after encryption to console so user can save it(or remember it) and then Use for Decryption ?

Whole code is here:Cannot decrypt cyphertext from text file, symmetric key implement. in java So Im sorry for posting again but Im not sure If the code is even readable(I'm newbie).

解决方案

I've had to do this myself recently. And while the other answers here led me in the right direction, it could have been easier. So here is my "share" for the day, a couple of helper methods for simple AES key manipulation. (Note the dependency on Apache Commons and Codec.)

This is all in a git repo now: github.com/stuinzuri/SimpleJavaKeyStore

import static org.apache.commons.codec.binary.Hex.*;
import static org.apache.commons.io.FileUtils.*;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import org.apache.commons.codec.DecoderException;

public static SecretKey generateKey() throws NoSuchAlgorithmException
{
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(256); // 128 default; 192 and 256 also possible
    return keyGenerator.generateKey();
}

public static void saveKey(SecretKey key, File file) throws IOException
{
    char[] hex = encodeHex(key.getEncoded());
    writeStringToFile(file, String.valueOf(hex));
}

public static SecretKey loadKey(File file) throws IOException
{
    String data = new String(readFileToByteArray(file));
    byte[] encoded;
    try {
        encoded = decodeHex(data.toCharArray());
    } catch (DecoderException e) {
        e.printStackTrace();
        return null;
    }
    return new SecretKeySpec(encoded, "AES");
}

这篇关于在java中存储/恢复加密密钥以解密字符串的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆