libKonogonka/src/main/java/libKonogonka/aesctr/AesCtrDecryptClassic.java

74 lines
2.5 KiB
Java
Raw Normal View History

2022-08-10 15:55:50 +03:00
/*
Copyright 2019-2023 Dmitry Isaenko
2022-08-10 15:55:50 +03:00
2022-08-10 20:20:10 +03:00
This file is part of libKonogonka.
2022-08-10 15:55:50 +03:00
2022-08-10 20:20:10 +03:00
libKonogonka is free software: you can redistribute it and/or modify
2022-08-10 15:55:50 +03:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2022-08-10 20:20:10 +03:00
libKonogonka is distributed in the hope that it will be useful,
2022-08-10 15:55:50 +03:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2022-08-10 20:20:10 +03:00
along with libKonogonka. If not, see <https://www.gnu.org/licenses/>.
2022-08-10 15:55:50 +03:00
*/
2023-01-21 13:47:33 +03:00
package libKonogonka.aesctr;
2022-08-10 15:55:50 +03:00
2023-01-21 13:47:33 +03:00
import libKonogonka.Converter;
2022-08-10 15:55:50 +03:00
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
2023-02-07 00:08:33 +03:00
import java.nio.Buffer;
2023-01-20 23:55:40 +03:00
import java.nio.ByteBuffer;
2022-08-10 15:55:50 +03:00
2023-01-21 13:47:33 +03:00
public class AesCtrDecryptClassic extends AesCtrDecrypt {
private final SecretKeySpec key;
private final byte[] ivArray;
private Cipher cipher;
2022-08-10 15:55:50 +03:00
public AesCtrDecryptClassic(String keyString, byte[] ivArray) throws Exception{
2023-01-21 13:47:33 +03:00
super();
this.key = new SecretKeySpec(Converter.hexStringToByteArray(keyString), "AES");
this.ivArray = ivArray;
2023-01-21 13:47:33 +03:00
reset();
2022-08-10 15:55:50 +03:00
}
2023-01-21 13:47:33 +03:00
@Override
public byte[] decryptNext(byte[] encryptedData) {
return cipher.update(encryptedData);
}
2023-01-21 13:47:33 +03:00
@Override
public void resetAndSkip(long blockCount) throws Exception{
reset(calculateCtr(blockCount * 0x200));
}
private byte[] calculateCtr(long offset){
BigInteger ctr = new BigInteger(ivArray);
BigInteger updateTo = BigInteger.valueOf(offset / 0x10L);
2023-01-20 23:55:40 +03:00
byte[] ctrCalculated = ctr.add(updateTo).toByteArray();
if (ctrCalculated.length != 0x10) {
ByteBuffer ctrByteBuffer = ByteBuffer.allocate(0x10);
2023-02-07 00:08:33 +03:00
((Buffer) ctrByteBuffer).position(0x10 - ctrCalculated.length);
2023-01-20 23:55:40 +03:00
ctrByteBuffer.put(ctrCalculated);
return ctrByteBuffer.array();
}
return ctrCalculated;
}
2023-01-21 13:47:33 +03:00
@Override
public void reset() throws Exception{
reset(ivArray.clone());
}
private void reset(byte[] updatedIvArray) throws Exception{
cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
IvParameterSpec iv = new IvParameterSpec(updatedIvArray);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
}
2022-08-10 15:55:50 +03:00
}