【问题标题】:How to generate a 128 bit key to use in AES algorithm如何生成用于 AES 算法的 128 位密钥
【发布时间】:2014-06-15 04:30:50
【问题描述】:

我正在使用下面提到的帖子中的代码来加密和解密我的应用程序的 Java 和 Java 脚本模块之间的值。

Compatible AES algorithm for Java and Javascript

在上面的帖子中,他们使用 128 位密钥值。我想使用自己的密钥,而不是硬编码 128 位密钥值。

我的问题是我可以将任何随机字符串转换为 128 位键值。

如果可以将任何字符串转换为 128 位值,请发布一些示例。

【问题讨论】:

标签: java javascript android


【解决方案1】:

字符用 8 位表示。因此要形成 128 位密钥,请创建一个具有 16 个字符 (16*8=128) 的字符串,例如“abcdefgh12345678”。

要将此密钥屏蔽为 base64,您可以使用 Apache commons-codec Base64.encodeBase64...@see http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html

【讨论】:

    【解决方案2】:

    我从谷歌找到的东西,并在我的项目中使用:

    private final static String algorithm = "PBKDF2WithHmacSHA1";
    
    private final static String HEX = "0123456789ABCDEF";
    
    private static final String CP_ALGORITH = "AES";
    private static final String CP_KEY = "PUTsomeKEYinHere";
    
    public static String cipher(String cipherKey, String data) throws NoSuchAlgorithmException, 
                        InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, 
                        IllegalBlockSizeException, BadPaddingException {
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
        KeySpec spec = new PBEKeySpec(cipherKey.toCharArray(), cipherKey.getBytes(), 128, 256);
        SecretKey tmp = skf.generateSecret(spec);
        SecretKey key = new SecretKeySpec(tmp.getEncoded(), CP_ALGORITH);
        Cipher cipher = Cipher.getInstance(CP_ALGORITH);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return toHex(cipher.doFinal(data.getBytes()));
    }
    
    public static String decipher(String cipherKey, String data) throws NoSuchAlgorithmException, 
                            InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, 
                            IllegalBlockSizeException, BadPaddingException {
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
        KeySpec spec = new PBEKeySpec(cipherKey.toCharArray(), cipherKey.getBytes(), 128, 256);
        SecretKey tmp = skf.generateSecret(spec);
        SecretKey key = new SecretKeySpec(tmp.getEncoded(), CP_ALGORITH);
        Cipher cipher = Cipher.getInstance(CP_ALGORITH);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(toByte(data)));
    }
    
    private static byte[] toByte(String data) throws NullPointerException{
        int len = data.length()/2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++)
            result[i] = Integer.valueOf(data.substring(2*i, 2*i+2), 16).byteValue();
        return result;
    }
    
    private static String toHex(byte[] doFinal) {
        StringBuffer result = new StringBuffer(2*doFinal.length);
        for (int i = 0; i < doFinal.length; i++) {
            result.append(HEX.charAt((doFinal[i]>>4)&0x0f)).append(HEX.charAt(doFinal[i]&0x0f));
        }
        return result.toString();
    }
    

    用法:

    cipher(CP_KEY, STRINGtoCIPHER);
    
    decipher(CP_KEY, YOURcipheredSTRING)
    

    我把所有这些都放在一个带有静态字段和方法的共享类中,所以我可以在我的应用程序的任何地方使用它。我用它来存储我共享偏好的 SessionID,效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 2016-05-26
      • 1970-01-01
      • 2010-11-24
      • 2013-06-16
      • 2017-02-12
      • 1970-01-01
      相关资源
      最近更新 更多