encrypt icon indicating copy to clipboard operation
encrypt copied to clipboard

Not compitable with my java aes encryption.(help)

Open heinzan opened this issue 4 years ago • 6 comments

 public class AES256 {
private static AES256 instance;
private String ips;
private Key keySpec;

public AES256(String key) {
	byte[] keyBytes = new byte[16];
	byte[] b = key.getBytes(StandardCharsets.UTF_8);
	System.arraycopy(b, 0, keyBytes, 0, keyBytes.length);
	SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
	ips = key.substring(0, 16);
	this.keySpec = keySpec;
	instance = this;
}

public static AES256 getDefault() {
	return instance;
}

public String encrypt(String str) {
	Cipher cipher;
	try {
		cipher = Cipher.getInstance("AES/CTR/NoPadding");
		cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(ips.getBytes(StandardCharsets.UTF_8)));

		byte[] encrypted = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));
		String Str = Base64.getEncoder().withoutPadding().encodeToString(encrypted);
		return Str;
	}
	catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException |      
      InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
	}
	return null;
}

public String decrypt(String str) {
	Cipher cipher;
	try {
		cipher = Cipher.getInstance("AES/CTR/NoPadding");
		cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(ips.getBytes(StandardCharsets.UTF_8)));

		byte[] byteStr = Base64.getDecoder().decode(str.getBytes());
		String Str = new String(cipher.doFinal(byteStr), StandardCharsets.UTF_8);

		return Str;
	}
	catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | 
      InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
	}
	return null;
}
    }

How use this plugin in my project.

heinzan avatar Aug 14 '20 10:08 heinzan

anybody here.

heinzan avatar Aug 18 '20 04:08 heinzan

I also waiting for the solution. Please try to update the solution.

pankajjangid avatar Aug 27 '20 10:08 pankajjangid

@pankajjangid I implemented using native platform.That method is fine for me.

heinzan avatar Aug 28 '20 08:08 heinzan

Hi,

I generated secure_key, and secure_iv using secure-random command-line tool, and used in below code.

This below code working fine for encrypt/decrypt data.

public String encrypt(String message) throws NoSuchAlgorithmException,
			NoSuchPaddingException, IllegalBlockSizeException,
			BadPaddingException, InvalidKeyException,
			UnsupportedEncodingException, InvalidAlgorithmParameterException {

		byte[] srcBuff = message.getBytes("UTF8");
		byte[] keybytes = Base64.decode(Constant.eK, Base64.DEFAULT);
		byte[] ivBytes = Base64.decode(Constant.eIv, Base64.DEFAULT);
		SecretKeySpec skeySpec = new SecretKeySpec(keybytes, "AES");
		IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
		Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
		ecipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
		byte[] dstBuff = ecipher.doFinal(srcBuff);
		String base64 = Base64.encodeToString(dstBuff, Base64.DEFAULT);
		Log.e(TAG,"encrypted Value "+base64);
		return base64;
	}

public String decrypt(String encrypted) throws NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException,
			InvalidAlgorithmParameterException, IllegalBlockSizeException,
			BadPaddingException, UnsupportedEncodingException {
		byte[] keybytes = Base64.decode(secure_key, Base64.DEFAULT);
		byte[] ivBytes = Base64.decode(secure_iv, Base64.DEFAULT);
		SecretKeySpec skeySpec = new SecretKeySpec(keybytes, "AES");
		IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
		Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
		ecipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
		byte[] raw = Base64.decode(encrypted, Base64.DEFAULT);
		byte[] originalBytes = ecipher.doFinal(raw);
		String original = new String(originalBytes, "UTF8");
		Log.e(TAG,"decrypted Value "+original);
		return original;
	}

Hope it helps somebody.

Thanks

praveenb avatar Mar 10 '21 12:03 praveenb

@praveenb i changed Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); to Cipher ecipher = Cipher.getInstance("AES/SIC/PKCS7Padding"); because in the flutter code the default mode is "SIC" but im getting error when i use it in java can you help me?

PrincEddy avatar Mar 12 '21 11:03 PrincEddy

Cipher.getInstance("AES/CTR/PKCS7PADDING") should work. CTR = SIC.

(EDIT: Be sure to import javax.crypto.Cipher)

This ignores the fact that this may still be a flawed approach. https://github.com/leocavalcante/encrypt/issues/225#issue-953138094

OGmetamonkey avatar Jul 27 '21 13:07 OGmetamonkey