Valter Silva
Valter Silva

Reputation: 16656

Java: How implement AES with 128 bits with CFB and No Padding

could someone give me any lead to this problem ? I need to know how to encrypt and decrypt with AES with at least 128 bits with CFB and No Padding.

Some code or links will be very appreciated. (i already look on google, but no lucky tough).

UPDATE:

Works fine!

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;
}

Returns null .. why ?

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;
}

Best regards, Valter Henrique.

Upvotes: 2

Views: 4527

Answers (1)

artbristol
artbristol

Reputation: 32407

Give this a go:

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 })));

Upvotes: 3

Related Questions