Java AES 256 encryption

rupen

I have the below java code to encrypt a string which uses a 64 character key. My question is will this be a AES-256 encryption?

String keyString = "C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E";
byte[] keyValue = hexStringToByte(keyString);
Key key = new SecretKeySpec(keyValue, "AES");
Cipher c1 = Cipher.getInstance("AES");
c1.init(Cipher.ENCRYPT_MODE, key);

String data = "Some data to encrypt";
byte[] encVal = c1.doFinal(data.getBytes());
String encryptedValue = Base64.encodeBase64String(encVal);


/* Copied the below code from another post in stackexchange */
public static byte[] hexStringToByte(String hexstr) 
{
  byte[] retVal = new BigInteger(hexstr, 16).toByteArray();
  if (retVal[0] == 0) 
  {
    byte[] newArray = new byte[retVal.length - 1];
    System.arraycopy(retVal, 1, newArray, 0, newArray.length);
    return newArray;
  }
  return retVal;
}

The following is the code after incorporating suggestions from divanov and laz.

String keyString = "C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E";
byte[] keyValue = DatatypeConverter.parseHexBinary(keyString);
Key key = new SecretKeySpec(keyValue, "AES");
Cipher c1 = Cipher.getInstance("AES");
c1.init(Cipher.ENCRYPT_MODE, key);

String data = "Some data to encrypt";
byte[] encVal = c1.doFinal(data.getBytes());
String encryptedValue = Base64.encodeBase64String(encVal);
divanov

Yes, it will as 64 characters are 32 bytes and 256 bits and any sequence of 256 bits can be used as an AES-256 key.

I suggest you to use DatatypeConverter.parseHexBinary (or similar utility from library of your choice) to convert hexadecimal strings into byte arrays.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AES 256 encryption - Qt equivalent for Java

From Dev

Pr0blem with AES256 encryption in java

From Dev

AES-256 Password Based Encryption/Decryption in Java

From Dev

What size of initialization vector needed for AES-256 encryption in java?

From Dev

AES-256-CTR Encryption in node JS and decryption in Java

From Dev

Recode old AES 256 encryption from Java to Ruby

From Dev

AES 256 encryption PHP with Padding

From Dev

AES Encryption 256 ECB Mode

From Dev

AES 256 Encryption is not matching with result

From Dev

AES256 JAVA encryption doesn't match C# encryption

From Dev

AES Encryption and Decryption with Java

From Dev

Java encryption using AES

From Dev

Android Java AES Encryption

From Dev

Does this use 256-bit AES encryption?

From Dev

AES 256 Encryption/Decryption without IV

From Dev

AES 256 encryption in C++ and Qt 5

From Dev

Encryption AES 256 Input Plaintext Length Issue

From Dev

Replicate Oracle AES 256 encryption to C#

From Dev

implementing AES256 encryption into IOS

From Dev

AES 256 encryption in C++ and Qt 5

From Dev

How to use 256 bit aes encryption in android

From Dev

AES 256 encryption mysql vs php method

From Dev

PBE AES_256 encryption incompatible between java 8 u65 and u71

From Dev

AES 256 bit encryption - java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long

From Dev

PBE AES_256 encryption incompatible between java 8 u65 and u71

From Dev

How to handle BadPaddingException During AES256 encryption in C# and decryption in Java

From Dev

AES 256 bit encryption with 256 block size in iOS

From Dev

AES 128 bit & AES 256 bit encryption decryption

From Dev

Java Encryption Cast-256

Related Related

HotTag

Archive