Please help me find examples of implementing GOST 28147-89 encryption in java. I found the bouncycastle library, smoked GOST28147Engine in it, but I do not have enough experience to write a working encryption program with subsequent decryption.

Closed due to the fact that the question is too general for the participants Dmitriy Simushev , Vladimir Martyanov , YuriSPb , D-side , Abyx 29 Mar '16 at 14:31 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • in the library, aren't these methods that you need implemented? - redL1ne

1 answer 1

An example of a class that can work with a string and a byte [] array.

Call:

MnpGOST28147Encription encriptor = new MnpGOST28147Encription(); String originalString = "String to encode"; String encoded = encriptor.Encode(originalString, key); String decoded = encriptor.Decode(encoded, key, Hex.decode(originalString).length); 

The class itself:

 import org.bouncycastle.crypto.CryptoException; import org.bouncycastle.crypto.engines.GOST28147Engine; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.util.encoders.Hex; public class MnpGOST28147Encription { public String Encode(String str, byte[] key) { return processEncoding(true, str, key); } public String Decode(String str, byte[] key) { return processEncoding(false, str, key); } public String Decode(String str, byte[] key, int length) { return processEncoding(false, str, key, length); } public byte[] Encode(byte[] str, byte[] key) { return processEncoding(true, str, key); } public byte[] Decode(byte[] str, byte[] key) { return processEncoding(false, str, key); } public byte[] Decode(byte[] str, byte[] key, int length) { return trimBytes(processEncoding(false, str, key), length); } public String processEncoding(boolean ende, String str, byte[] key) { byte[] bytes = processEncoding(ende, Hex.decode(str), key); return new String(Hex.encode(bytes)); } public String processEncoding(boolean ende, String str, byte[] key, int length) { byte[] bytes = trimBytes(processEncoding(ende, Hex.decode(str), key), length); return new String(Hex.encode(bytes)); } public byte[] processEncoding(boolean ende, byte[] inBytes, byte[] key) { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher( new GOST28147Engine())); cipher.init(ende, new KeyParameter(key)); byte[] outBytes = new byte[cipher.getOutputSize(inBytes.length)]; int len = cipher.processBytes(inBytes, 0, inBytes.length, outBytes, 0); try { cipher.doFinal(outBytes, len); } catch (CryptoException e) { System.out.println("Exception: " + e.toString()); } return outBytes; } public byte[] trimBytes(byte[] bytes, int length) { byte[] outBytesTrimmed = new byte[length]; for (int i = 0; i < length; i++) { outBytesTrimmed[i] = bytes[i]; } return outBytesTrimmed; } } 
  • Please tell me if this is a solution to the problem or did you want to add a question? - Nicolas Chabanovsky
  • This is a solution to the problem, but I can only answer every other day. - San