Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  AES IV Update  (Read 730 times)
0 Members and 1 Guest are viewing this topic.
Offline Renoria

Junior Member




...


« Posted 2009-03-19 06:10:59 »

I currently have this class for encrypting/decrypting AES. How do I update the IV? I already have an updateIV() function but it doesn't seem to update the REAL IVParameterSpec.

Thanks.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package aescipher;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author Ido
 */

public class Encryption {
    public static enum Mode {
        ENCRYPT_MODE, DECRYPT_MODE;
    }
   
    public static final String CIPHER = "AES/CBC/PKCS5Padding";
   
    protected Cipher cipher;
    protected Mode mode;
    protected byte[] iv;
    protected IvParameterSpec ivSpec;
    protected byte[] key;
   
    /**
     * Creates this Encryptor.
     * @param cipherMode Mode to use
     * @param key 128 bit AES key to use
     * @param iv 128 bit [16 byte] AES iv to use
     */

    public Encryption(Mode cipherMode, byte[] key, byte[] iv) {
        try {
            this.key = key;
            cipher = Cipher.getInstance(CIPHER);
            ivSpec = new IvParameterSpec(iv);
            SecretKeySpec aesKey = new SecretKeySpec(key, "AES");
            this.mode = cipherMode;
            if (cipherMode == Mode.ENCRYPT_MODE) {
                cipher.init(Cipher.ENCRYPT_MODE, aesKey, ivSpec);
            } else {
                cipher.init(Cipher.DECRYPT_MODE, aesKey, ivSpec);
            }
        } catch (InvalidKeyException ex) {
          Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAlgorithmParameterException ex) {
          Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.iv = iv;
    }
   
    public byte[] crypt(byte[] in) {
        try {
          updateIV();
          System.out.print("New IV: ");
          printBytes(this.cipher.getIV());
          return cipher.doFinal(in);
        } catch (IllegalBlockSizeException ex) {
          Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
          Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
   
    public void printBytes(byte[] b) {
        System.out.print("(");
        for (int i = 0; i < b.length; i++) {
            System.out.print("0x" + Integer.toHexString(b[i]));
            if (i < b.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println(")");
    }
   
    public void updateIV() {
        for (int i = 0; i < this.iv.length; i++) {
            if (i < iv.length/2) { //16/2
               iv[i] ^= iv[i + 1];
                if (iv[i] > 127) {
                    iv[i] = 0x0A;
                }
                iv[i] ^= iv[i + 3];
                if (iv[i] > 127) {
                    iv[i] = 0x0D;
                }
            } else { //16/2
               iv[i] ^= iv[i - 3];
                if (iv[i] > 127) {
                    iv[i] = 0x0C;
                }
                iv[i] ^= iv[i - 2];
                if (iv[i] > 127) {
                    iv[i] = 0x1D;
                }                
            }
        }
        //cipher.update(iv, 0, iv.length);
   }
}
Offline Renoria

Junior Member




...


« Reply #1 - Posted 2009-03-24 12:31:56 »

Never mind, I figured it out Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (9 views)
2014-10-02 14:36:20

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (48 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!