开发者

slowAES encryption and java decryption

I've tried to implement the same steps as discussed in AES .NET

but with no success , i can't seem to get java and slowAes to play toghter ... attached is my code i'm sorry i can't add more this is my first time trying to deal with encryption would appreciate any help

private static final String ALGORITHM = "AES";
private static final byte[] keyValue = getKeyBytes("12345678901234567890123456789012");

private static final byte[] INIT_VECTOR = new byte[16];
private static IvParameterSpec ivSpec = new IvParameterSpec(INIT_VECTOR);

public static void main(String[] args) throws Exception {
    String encoded = encrypt("watson?");
    System.out.println(encoded);
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGORITHM);
    // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    // key = keyFactory.generateSecret(new DESKeySpec(keyValue));
    return key;
}

private static byte[] getKeyBytes(String key) {
    byte[] hash = DigestUtils.sha(key); // key.getBytes()
    byte[] saltedHash = new byte[16];
    Syste开发者_如何学JAVAm.arraycopy(hash, 0, saltedHash, 0, 16);
    return saltedHash;
}

public static String encrypt(String valueToEnc) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, key,ivSpec);
    byte[] encValue = c.doFinal(valueToEnc.getBytes());
    String encryptedValue = new BASE64Encoder().encode(encValue);
    return encryptedValue;
}

public static String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

the bytes returned are different thanks in advance .


You encrypt data using AES in CBC mode and PKC5Padding but you decrypt using just plain AES. In your decrypt method you need to create the Cipher using "AES/CBC/PKCS5Padding" just like you do in the encrypt method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜