Reputation: 51
hashed: "89d1ed22aac58f5bbea53b2fde81a946" (String)(32 characters)
orginal: "test" (String)
Key: "encryptionkeyhere" (String)
16 bit key: 646C646873766D666C766D000000000 (Bytes)(Converted to 16 Bytes)
16 bit IV: 00000000000000000000000000000000 (Bytes)
084987B6C979950A11EBE33A5499B091D127CD208E95BAE5C6B5DE5FAE65AFB68EB5C083BB808FDFD98C16694E6FCA9F2E15DF5A63FBD7E4E7EFFB242D0D56B1A3C35F76F977A70F5A9A1EAD0FC3A9E61242CBA0AF848FFC5C8C4342F4011E0436D81F6B064E086C802E175F662C43A798ADC38D25684E99E926ED30B900FF89CA66760B7DBDFBF3087378620A69981FB87346512DC6596E7420763C238EC7E2015941F3E613070E737A0D191F4730C3CF70C270B5FA13E44407B7D6D7567B6126241777D80874320B969B8818371CEC91DF97AA42ABE5BCF015D9B17BDC18F2F3E1E7E25794A97C44F760B68F1D24FD96036DFC92AF400D53D1292C1FBDD0296AFE28401D48B2EA486C781B4729E55C8794505C7AC3AB2A35C7B893DE4128A3A59335BC76071E404A9B7D600C19CBB0CB640071C7AF9CB135FC46DD29080707ED30BC9CDA6AE65ACDD84CF52B299408D82333F1A8DBD1F58213671063D7C8C1BB9002563A04BD53B03D802888F299024AD8C9BE2A0749EF920362A4C86129401DB29C2D2A17A960C2038C0DC6CCD18F361A629BBDCA17BFF0EB1EB737AE9F00CAB8CFBE2050749E6F0D6EAB7DC849380E85BC55DA82AF7354D31D8431A47FB385CC5CA01FE1DE064AE3F2426A5135938AD923C4A28661CBDA7F1397326D7921158FB06A2B4A1C6F8D141B2749D8A8B20883A7A168D811057CDA88D595EEC694558C446673B708BABFFB5FA584B2AFE527A9C3B162BD38405FC08250AB5EF1D15C7ED94AB71796A5582E25ADFBF83D7BD146C9BF5FE68FA3ADEA4C0D77A4314D06336F6D503A8C097A0FD54051CDD49ED8ECA34E3150D61C01FBB53208C62D701BEFEB15419776B9FB18437FE2C1B4A9BABFDE3CE83457FF6F3F87F83B9B702450D0409886B9D9490BD665BB1CB3E5E460ACDD7BC51958A3870985D58E4585B
void CRijndael::Encrypt(char const* in, char* result, size_t n, int iMode)
{
if(false==m_bKeyInit)
throw exception(sm_szErrorMsg1);
//n should be > 0 and multiple of m_blockSize
if(0==n || n%m_blockSize!=0)
throw exception(sm_szErrorMsg2);
int i;
char const* pin;
char* presult;
if(CBC == iMode) //CBC mode, using the Chain
{
for(i=0,pin=in,presult=result; i<(int)( n/m_blockSize ); i++)
{
Xor(m_chain, pin);
EncryptBlock(m_chain, presult);
memcpy(m_chain, presult, m_blockSize);
pin += m_blockSize;
presult += m_blockSize;
}
}
else if(CFB == iMode) //CFB mode, using the Chain
{
for(i=0,pin=in,presult=result; i<(int)( n/m_blockSize ); i++)
{
EncryptBlock(m_chain, presult);
Xor(presult, pin);
memcpy(m_chain, presult, m_blockSize);
pin += m_blockSize;
presult += m_blockSize;
}
}
else //ECB mode, not using the Chain
{
for(i=0,pin=in,presult=result; i<(int)( n/m_blockSize ); i++)
{
EncryptBlock(pin, presult);
pin += m_blockSize;
presult += m_blockSize;
}
}
}
var key = buildKeyFromString(encryptionKey)
val iv = IvParameterSpec(key)
val secretKeySpec = SecretKeySpec(key, "AES")
val cipher = Cipher.getInstance("AES/CBC/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv)
return cipher.doFinal(encryptedByte)
String: "????58f5bbea53b2fde81a946" (String)(Wrong value)
Hex: "5C55005916125F540D170E353866356262656135336232666465383161393436"
Hashed: "89d1ed22aac58f5bbea53b2fde81a946" (String)(32 characters)
Upvotes: 4
Views: 729
Reputation: 51
I have use key
variable for both IV and secretkeyspec. So I've changed the IV to 16 bytes 0.
var key = buildKeyFromString(encryptionKey)
var ivBytes = 16 bytes zero
val iv = IvParameterSpec(ivBytes )
val secretKeySpec = SecretKeySpec(key, "AES")
val cipher = Cipher.getInstance("AES/CBC/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv)
return cipher.doFinal(encryptedByte)
Upvotes: 0
Reputation: 49131
The cause of the problem is that different IVs are used for encryption and decryption: The encryption uses a zero vector (0x00000000000000000000000000000000
) and the decryption uses the key as IV (0x646C646873766D666C766D0000000000
).
By the way, the posted ciphertext is too long. Since the plaintext is 32 bytes in size and no padding is used, the ciphertext is also 32 bytes in size and just corresponds to the first 32 bytes of the posted ciphertext (0x084987B6C979950A11EBE33A5499B091D127CD208E95BAE5C6B5DE5FAE65AFB6
).
Upvotes: 2