Search code examples
javabouncycastlegost28147

Implement gost 28147-89 algorithm for encrypting and decripting in java using bouncycastle


I am trying to implement gost 28147-89 algorithm for encrypting and decrypting string.

In the bouncycastle documentation I didn't understand how to implement gost 28147. How can I make a simple class which encrypts and decrypts a string using the gost 28147-89 algorithm?


Solution

  • Quoting from GOST28147Test.java (basically an example class from the bouncy castle people for exactly that encryption scheme):

    key = new SecretKeySpec(keyBytes, "GOST28147");
    
    in = Cipher.getInstance("GOST28147/ECB/NoPadding", "BC");
    out = Cipher.getInstance("GOST28147/ECB/NoPadding", "BC");
    out.init(Cipher.ENCRYPT_MODE, key);
    in.init(Cipher.DECRYPT_MODE, key);
    
    //
    // encryption pass
    //
    bOut = new ByteArrayOutputStream();
    
    cOut = new CipherOutputStream(bOut, out);
    
    for (int i = 0; i != input.length / 2; i++)
    {
        cOut.write(input[i]);
    }
    

    And so on ...