Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

I am new to cryptography so forgive me if I am doing anything stupid. I am following the Cryptography in JAVA book. I am creating my own kerberos. I am stuck at a point.

I have Message class which will take care of every interaction between client, KDC, FS, and AS.

    private final String messageA;
    private final String messageB;
    private final Type type;
    private final long timeGenerated;
    private boolean processed;

messageA and messageB are the strings which are encrypted using "AES/CTR/NoPadding" and encoded with base64(bouncy castle)

I have a ticket class which is a ticket representation of TGT.

private String userID;
private String ip;
private String ticketID;
private Long validity;
private final Type type;
private final long timeGenerated;
private Key key;

Now, here is the problem,

I am serializing the ticket object, encrypting it, encoding it using base64 and setting that string to messageA or messageB and same procedure in reverse order when I want to get it back.

The encryption and decryption code is:

//Encryption
    Cipher cipherFinal = Cipher.getInstance("AES/CTR/NOPADDING");
cipherFinal.init(Cipher.ENCRYPT_MODE,TGS_KEY, 
Operation.generateIV(cipherFinal), 
Operation.random);
String finalString = new String(Base64.encode
(cipherFinal.doFinal
(Operation.getByteArrayFromObject(ticket))));

    //Decryption 
Cipher cipherFinal = Cipher.getInstance("AES/CTR/NOPADDING");
cipherFinal.init(Cipher.DECRYPT_MODE, TGS_KEY, 
Operation.generateIV(cipherFinal),
Operation.random);
byte[] decryptedText = cipherFinal.doFinal(messageA.getBytes());

Ticket ticket = (Ticket)Operation.getObjectFromByteArray(decryptedText);

I am getting different byte[] than the original one.

    BYTE ARR TO BE ENCRYPTED : rO0ABXNyABJjb20ubXkuZGF0YS5...
    BYTE ARRAY RECOVERED :     7fK+CUqePrJAeTN8K7GLpXxhsyA...

The problem is with the serialization, the serialized byte[] is really big in size for a single object like 532 in length.

My question is, is there any fixed length of blocks it decrypts or maximum allowed length or i am doing anything wrong in encryption or decryption?

Should I make any changes with the code?

Please help. Thanks in advance.

share|improve this question
1  
The IV you use for encryption needs to be transmitted to the box doing decryption and used in decryption. In your code you are instead generating a new IV at at each end. This is why the decryption doesn't give back the proper plaintext. –  mikeazo Feb 21 at 13:21
    
Thank you so much... You saved me. It's working fine now. –  ADi Feb 22 at 0:00
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.