java - 如何将字符串转换为 SecretKey

标签 java encryption

我想把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();
}

我尝试使用 BASE64Decoder 而不是 secretKey,但我遇到了无法指定 key 长度的问题。

编辑: 我想从另一个地方调用这个函数

 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 the secret key specs.
     SecretKey skey = key; //here is the error
   byte[] raw = skey.getEncoded();
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    // Instantiate the cipher
    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    System.out.println("msg is" + message + "\n raw is" + raw);
    byte[] encrypted = cipher.doFinal(message.getBytes());
    String cryptedValue = new String(encrypted);
    System.out.println("encrypted string: " + cryptedValue);
    return cryptedValue;
}

如果有人能提供帮助,我将不胜感激。

最佳答案

由于这些特殊原因,没有进行完整性检查

  1. 从用例来看需求并不明显。
  2. “AES/GCM/NoPadding” 模式仅从 Java 7 开始可用
  3. 这取决于用户是否要部署,例如HMAC 和/或 AESCMAC(推荐)。
  4. 至少需要一把额外的 key ,以及两次完整的通行证。

如果您在双方都实现了 GCM 模式 - 例如在 Java 6 上使用 Bouncy CaSTLe - 请使用它,因为它更安全(只要“IV”确实是唯一的)。更改实现应该非常容易。

关于加密的实现说明

  1. 由于填充 oracle 攻击(平均每字节需要 128 次尝试或更低,与算法或 key 大小无关),此实现在不受限制的客户端/服务器角色中使用时不安全。您将需要对加密数据使用 MAC、HMAC 或签名,并在解密之前对其进行验证以将其部署在客户端/服务器模式下。
  2. 如果解密失败,Decrypt 将返回 null。这只能表示填充异常,应该充分处理(我是否警告过填充 oracle 攻击?)
  3. 无效键将作为 InvalidArgumentException 返回。
  4. 所有其他与安全相关的异常都被“隐瞒”,因为这意味着 Java 运行时环境无效。例如,支持 “UTF-8”“AES/CBC/PKCS5Padding” 是每个 Java SE 实现的必需

一些其他的笔记

  1. 请不要反其道而行之,将字节直接插入加密方法的输入字符串中(例如使用new String(byte[]))。该方法可能会无提示地失败!
  2. 针对可读性进行了优化。如果您更喜欢速度和更好的内存占用,请选择 Base64 流和 CipherStream 实现。
  3. 您至少需要 Java 6 SE 或兼容版本才能运行此代码。
  4. 对于超过 128 位的 AES key 大小,加密/解密可能失败,因为您可能需要策略文件进行无限制加密(可从 Oracle 获得)
  5. 导出加密时注意政府法规。
  6. 此实现使用十六进制 key 而不是 base64 key ,因为它们足够小,而且十六进制更易于手动编辑/验证。
  7. 使用从 JDK 检索的十六进制和 base64 编码/解码,不需要任何外部库。
  8. Uber 简单易用,但当然不是很面向对象,没有缓存用于加密/解密的对象实例。随意重构。

好的,这是一些代码...

    public static String encrypt(final String plainMessage,
            final String symKeyHex) {
        final byte[] symKeyData = DatatypeConverter.parseHexBinary(symKeyHex);

        final byte[] encodedMessage = plainMessage.getBytes(Charset
                .forName("UTF-8"));
        try {
            final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            final int blockSize = cipher.getBlockSize();

            // create the key
            final SecretKeySpec symKey = new SecretKeySpec(symKeyData, "AES");

            // generate random IV using block size (possibly create a method for
            // this)
            final byte[] ivData = new byte[blockSize];
            final SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
            rnd.nextBytes(ivData);
            final IvParameterSpec iv = new IvParameterSpec(ivData);

            cipher.init(Cipher.ENCRYPT_MODE, symKey, iv);

            final byte[] encryptedMessage = cipher.doFinal(encodedMessage);

            // concatenate IV and encrypted message
            final byte[] ivAndEncryptedMessage = new byte[ivData.length
                    + encryptedMessage.length];
            System.arraycopy(ivData, 0, ivAndEncryptedMessage, 0, blockSize);
            System.arraycopy(encryptedMessage, 0, ivAndEncryptedMessage,
                    blockSize, encryptedMessage.length);

            final String ivAndEncryptedMessageBase64 = DatatypeConverter
                    .printBase64Binary(ivAndEncryptedMessage);

            return ivAndEncryptedMessageBase64;
        } catch (InvalidKeyException e) {
            throw new IllegalArgumentException(
                    "key argument does not contain a valid AES key");
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(
                    "Unexpected exception during encryption", e);
        }
    }

    public static String decrypt(final String ivAndEncryptedMessageBase64,
            final String symKeyHex) {
        final byte[] symKeyData = DatatypeConverter.parseHexBinary(symKeyHex);

        final byte[] ivAndEncryptedMessage = DatatypeConverter
                .parseBase64Binary(ivAndEncryptedMessageBase64);
        try {
            final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            final int blockSize = cipher.getBlockSize();

            // create the key
            final SecretKeySpec symKey = new SecretKeySpec(symKeyData, "AES");

            // retrieve random IV from start of the received message
            final byte[] ivData = new byte[blockSize];
            System.arraycopy(ivAndEncryptedMessage, 0, ivData, 0, blockSize);
            final IvParameterSpec iv = new IvParameterSpec(ivData);

            // retrieve the encrypted message itself
            final byte[] encryptedMessage = new byte[ivAndEncryptedMessage.length
                    - blockSize];
            System.arraycopy(ivAndEncryptedMessage, blockSize,
                    encryptedMessage, 0, encryptedMessage.length);

            cipher.init(Cipher.DECRYPT_MODE, symKey, iv);

            final byte[] encodedMessage = cipher.doFinal(encryptedMessage);

            // concatenate IV and encrypted message
            final String message = new String(encodedMessage,
                    Charset.forName("UTF-8"));

            return message;
        } catch (InvalidKeyException e) {
            throw new IllegalArgumentException(
                    "key argument does not contain a valid AES key");
        } catch (BadPaddingException e) {
            // you'd better know about padding oracle attacks
            return null;
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(
                    "Unexpected exception during decryption", e);
        }
    }

用法:

    String plain = "Zaphod's just zis guy, ya knöw?";
    String encrypted = encrypt(plain, "000102030405060708090A0B0C0D0E0F");
    System.out.println(encrypted);
    String decrypted = decrypt(encrypted, "000102030405060708090A0B0C0D0E0F");
    if (decrypted != null && decrypted.equals(plain)) {
        System.out.println("Hey! " + decrypted);
    } else {
        System.out.println("Bummer!");
    }

关于java - 如何将字符串转换为 SecretKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4551263/

相关文章:

java - 如何更改 Aerospike 使用的序列化器/解串器?

java - 如何连接多个不同格式的字符串然后拆分它们

凯撒密码 C 语言

c++ - 使用不可导出的私钥和 CryptoAPI 进行解密

java - 如何统计key的值并排序?

java - SOLR 使用过多内存(第 2 部分)

java - hibernate sql查询

ssl - ejabberd s2s 到 GTalk/Hangout with TLS

iphone - 在iPhone应用程序中实现HMAC加密算法

phpseclib 零字节填充