Java:如何使用带有CFB和无填充的128位实现AES

时间:2011-06-06 13:00:32

标签: java encryption

有人可以告诉我这个问题吗? 我需要知道如何使用至少128位的CFB和无填充来加密和解密。

非常感谢一些代码或链接。 (我已经看过谷歌,但没有幸运的坚韧)。

更新:

工作正常!

public byte[] crypt() {
    byte[] crypt = null;
    try {
        final Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding", "SunJCE");
        final SecretKey skeySpec = KeyGenerator.getInstance("AES").generateKey();
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        crypt = cipher.doFinal(new byte[]{0, 1, 2, 3});


    } catch (Exception ex) {
         throw new RuntimeException(ex);
    }
        return crypt;
}

返回null ..为什么?

public String decrypt(byte[] text) {
    byte[] crypt = null;
    String plainText = null;
    try {
        final Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding", "SunJCE");
        final SecretKey skeySpec = KeyGenerator.getInstance("AES").generateKey();
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        crypt = cipher.doFinal(text);
        plainText = new String(crypt);



    } catch (Exception ex) {
          throw new RuntimeException(ex);
    }
        return plainText;
}

祝你好运, Valter Henrique。

1 个答案:

答案 0 :(得分:3)

放手一搏:

final Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding", "SunJCE");
final SecretKey skeySpec = KeyGenerator.getInstance("AES")
        .generateKey();
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
System.out.println(Arrays.toString(cipher.doFinal(new byte[] { 0, 1, 2,
            3 })));