0

The code encrypted with Rijndael-128, but I need to encrypt with Rijndael-256.
I don't know how to change.

Crypto:

public class Crypto {
private static final String mEngine = "AES";
private static final String mCryptoEncrypt = "AES/ECB/ZeroBytePadding";
private String mKey;
public Crypto(String key) {
    this.mKey = key;
}
public byte[] cipher(byte[] data, int mode, String crypto)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException {
    SecretKeySpec sks =new SecretKeySpec(mKey.getBytes(Charset
            .forName("UTF-8")), mEngine);

    Cipher c = Cipher.getInstance(crypto);
    c.init(mode, sks);
    return c.doFinal(data);
}

public String encrypt(byte[] data) throws InvalidKeyException,
        NoSuchAlgorithmException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException,
        InvalidAlgorithmParameterException {
    return Base64.encodeToString(cipher(data, Cipher.ENCRYPT_MODE, mCryptoEncrypt), Base64.DEFAULT);
}

}


MainActivity:

private static final String STORE_UID = "test";
private static final String AES_KEY = "0123456789123456";
Crypto crypto = new Crypto(AES_KEY);     
String encode = crypto.encrypt(STORE_UID.getBytes(Charset.forName("UTF-8")));

Content:test
Key:0123456789123456
Result:vw4sykPYP90szFDu9RA4hA==

It encrypted with Rijndael-256 that should be "WEszhnxsZvq/TtK8lnwfBs91A/S8XT9oHAGC0lCRAEM="

Zih-Yang Lin
  • 115
  • 1
  • 1
  • 6
  • Don't confuse AES with Rijndael. Rijndael-256 is not AES anymore. – Artjom B. Dec 21 '15 at 09:50
  • ECB mode is not secure. See [Block Cypher Modes](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29) for a literal illustration. Use CBC or CTR modes for security; GCM mode if you also need built-in authentication. – rossum Dec 21 '15 at 11:30

0 Answers0