I have a encryption method GetDecryptedSSN(). I tested it’s correctness by the following test. It works fine
        //////////TEST 2//////////
        byte[] encryptedByteWithIBMEncoding2 = DecryptionServiceHelper.GetEncryptedSSN("123456789");
        string clearTextSSN2 = DecryptionServiceHelper.GetDecryptedSSN(encryptedByteWithIBMEncoding2);
But when I do a conversion to ASCII String and then back, it is not working correctly. What is the problem in the conversion logic?
        //////////TEST 1////////// 
        //String  -- > ASCII Byte --> IBM Byte -- > encryptedByteWithIBMEncoding
        byte[] encryptedByteWithIBMEncoding = DecryptionServiceHelper.GetEncryptedSSN("123456789");
        //encryptedByteWithIBMEncoding -->  Encrypted Byte ASCII
        string EncodingFormat = "IBM037";
        byte[] encryptedByteWithASCIIEncoding = Encoding.Convert(Encoding.GetEncoding(EncodingFormat), Encoding.ASCII,
                encryptedByteWithIBMEncoding);
        //Encrypted Byte ASCII - ASCII Encoded string 
        string encodedEncryptedStringInASCII = System.Text.ASCIIEncoding.ASCII.GetString(encryptedByteWithASCIIEncoding);
        //UpdateSSN(encodedEncryptedStringInASCII);
        byte[] dataInBytesASCII = System.Text.ASCIIEncoding.ASCII.GetBytes(encodedEncryptedStringInASCII);
        byte[] bytesInIBM = Encoding.Convert(Encoding.ASCII, Encoding.GetEncoding(EncodingFormat),
                dataInBytesASCII);
        string clearTextSSN = DecryptionServiceHelper.GetDecryptedSSN(bytesInIBM);
Helper Class
 public static class DecryptionServiceHelper
{
    public const string EncodingFormat = "IBM037";
    public const string SSNPrefix = "0000000";
    public const string Encryption = "E";
    public const string Decryption = "D";
    public static byte[] GetEncryptedSSN(string clearTextSSN)
    {
        return GetEncryptedID(SSNPrefix + clearTextSSN);
    }
    public static string GetDecryptedSSN(byte[] encryptedSSN)
    {
        return GetDecryptedID(encryptedSSN);
    }
    private static byte[] GetEncryptedID(string id)
    {
        ServiceProgram input = new ServiceProgram();
        input.RequestText = Encodeto64(id);
        input.RequestType = Encryption;
        ProgramInterface inputRequest = new ProgramInterface();
        inputRequest.Test__Request = input;
        using (MY_Service operation = new MY_Service())
        {
            return ((operation.MY_Operation(inputRequest)).Test__Response.ResponseText);
        }
    }
    private static string GetDecryptedID(byte[] id)
    {
        ServiceProgram input = new ServiceProgram();
        input.RequestText = id;
        input.RequestType = Decryption;
        ProgramInterface request = new ProgramInterface();
        request.Test__Request = input;
        using (MY_Service operationD = new MY_Service())
        {
            ProgramInterface1 response = operationD.MY_Operation(request);
            byte[] encodedBytes = Encoding.Convert(Encoding.GetEncoding(EncodingFormat), Encoding.ASCII,
                response.Test__Response.ResponseText);
            return System.Text.ASCIIEncoding.ASCII.GetString(encodedBytes);
        }
    }
    private static byte[] Encodeto64(string toEncode)
    {
        byte[] dataInBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
        Encoding encoding = Encoding.GetEncoding(EncodingFormat);
        return Encoding.Convert(Encoding.ASCII, encoding, dataInBytes);
    }
}
REFERENCE:
 
     
     
    