Нужно реализовать шифрование по госту, нашел пример и на его основе наваял следующее -
public class gost28147_89 {
final private byte[] key;
public gost28147_89(String keyString) {
byte[] key = new byte[0];
try {
key = keyString.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new ThrowFabric.ForbiddenException("Кодировка UTF-8 не поддерживается");
}
if(key.length == 32)
this.key = key;
else{
this.key = null;
}
}
public String encrypt(String data) {
if(key.length != 32) return "";
return encrypt(data, true);
}
public String encrypt(String data, Boolean isEncrypt) {
if(key.length != 32) return "";
String result;
byte[] dataBytes;
try {
dataBytes = data.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new ThrowFabric.ForbiddenException("Кодировка UTF-8 не поддерживается");
}
BufferedBlockCipher cipher = new BufferedBlockCipher(new GOST28147Engine());
cipher.init(isEncrypt, new KeyParameter(key));
byte[] dataoOutBytes = new byte[cipher.getOutputSize(dataBytes.length)];
int len = cipher.processBytes(dataBytes, 0, dataBytes.length, dataoOutBytes, 0);
try {
cipher.doFinal(dataoOutBytes, len);
} catch (InvalidCipherTextException e) {
throw new ThrowFabric.ForbiddenException("Некорректный текст шифра");
}
result = Arrays.toString(dataoOutBytes);
return result;
}
public String decrypt(String data){
if(key.length != 32) return "";
return encrypt(data, false);
}
}
doFinal
вылетает с data not block size aligned\norg.bouncycastle.crypto.BufferedBlockCipher.doFinal(Unknown Source)
Кто нибудь работал с этой библиотекой? Не могу найти никакого описания как с ней работать...
UPDATE
Сделал по вашему примеру + подогнал блоки по размеру кратные 8. Шифрование/Дешифрование заработало, но выход после шифрования всегда на один блок(8 байт) больше входного массива.
byte[] dataBytes, tmpBytes;
tmpBytes = data.getBytes();
if(tmpBytes.length % 8 != 0){
dataBytes = new byte[tmpBytes.length + (8 - tmpBytes.length % 8)];
System.arraycopy(tmpBytes, 0, dataBytes, 0, tmpBytes.length);
} else{
dataBytes = tmpBytes;
}
BlockCipher engine = new GOST28147Engine();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
cipher.init(isEncrypt, new KeyParameter(key1));
byte[] cipherText = new byte[cipher.getOutputSize(dataBytes.length)];
int outputLen = cipher.processBytes(dataBytes, 0, dataBytes.length, cipherText, 0);
try {
cipher.doFinal(cipherText, outputLen);
} catch (InvalidCipherTextException e) {
throw new ThrowFabric.BadRequestException(e.getMessage());
}
result = new String(cipherText);
return result;