Skip to content

Commit

Permalink
Fixed error where key-based encryption output a version number of 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjones committed Jan 19, 2014
1 parent ca974e3 commit 478a4cb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public byte[] encryptData(byte[] plaintext, SecretKey encryptionKey,
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(iv));
byte[] ciphertext = cipher.doFinal(plaintext);

AES256v2Ciphertext output = new AES256v2Ciphertext(iv, ciphertext);
AES256v3Ciphertext output = new AES256v3Ciphertext(iv, ciphertext);

Mac mac = Mac.getInstance(HMAC_ALGORITHM);
mac.init(hmacKey);
Expand Down
84 changes: 43 additions & 41 deletions src/main/java/org/cryptonode/jncryptor/AES256v2Ciphertext.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
/* Copyright 2014 Duncan Jones
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.cryptonode.jncryptor;


/**
* A format compatible with version 2.
*/
class AES256v2Ciphertext extends AES256Ciphertext {

@Override
int getVersionNumber() {
return 2;
}

AES256v2Ciphertext(byte[] encryptionSalt, byte[] hmacSalt, byte[] iv,
byte[] ciphertext) {
super(encryptionSalt, hmacSalt, iv, ciphertext);
}

AES256v2Ciphertext(byte[] iv, byte[] ciphertext) {
super(iv, ciphertext);
}

AES256v2Ciphertext(byte[] data) throws InvalidDataException {
super(data);
}
}
/* Copyright 2014 Duncan Jones
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.cryptonode.jncryptor;


/**
* A format compatible with version 2.
*/
class AES256v2Ciphertext extends AES256Ciphertext {

static final int EXPECTED_VERSION = 2;

@Override
int getVersionNumber() {
return EXPECTED_VERSION;
}

AES256v2Ciphertext(byte[] encryptionSalt, byte[] hmacSalt, byte[] iv,
byte[] ciphertext) {
super(encryptionSalt, hmacSalt, iv, ciphertext);
}

AES256v2Ciphertext(byte[] iv, byte[] ciphertext) {
super(iv, ciphertext);
}

AES256v2Ciphertext(byte[] data) throws InvalidDataException {
super(data);
}
}
82 changes: 42 additions & 40 deletions src/main/java/org/cryptonode/jncryptor/AES256v3Ciphertext.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
/* Copyright 2014 Duncan Jones
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cryptonode.jncryptor;

/**
* Version 3 format.
*/
class AES256v3Ciphertext extends AES256Ciphertext {

public AES256v3Ciphertext(byte[] data) throws InvalidDataException {
super(data);
}

public AES256v3Ciphertext(byte[] encryptionSalt, byte[] hmacSalt, byte[] iv,
byte[] ciphertext) {
super(encryptionSalt, hmacSalt, iv, ciphertext);
}

public AES256v3Ciphertext(byte[] iv, byte[] ciphertext) {
super(iv, ciphertext);
}

@Override
int getVersionNumber() {
return 3;
}

}
/* Copyright 2014 Duncan Jones
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cryptonode.jncryptor;

/**
* Version 3 format.
*/
class AES256v3Ciphertext extends AES256Ciphertext {

static final int EXPECTED_VERSION = 3;

public AES256v3Ciphertext(byte[] data) throws InvalidDataException {
super(data);
}

public AES256v3Ciphertext(byte[] encryptionSalt, byte[] hmacSalt, byte[] iv,
byte[] ciphertext) {
super(encryptionSalt, hmacSalt, iv, ciphertext);
}

public AES256v3Ciphertext(byte[] iv, byte[] ciphertext) {
super(iv, ciphertext);
}

@Override
int getVersionNumber() {
return EXPECTED_VERSION;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public void testEncryptionAndDecryption() throws Exception {

AES256JNCryptor cryptor = new AES256JNCryptor();
byte[] ciphertext = cryptor.encryptData(plaintext, password.toCharArray());

// Check version
assertEquals(AES256v3Ciphertext.EXPECTED_VERSION, ciphertext[0]);

byte[] plaintext2 = cryptor.decryptData(ciphertext, password.toCharArray());
Assert.assertArrayEquals(plaintext, plaintext2);
}
Expand Down Expand Up @@ -228,6 +232,10 @@ public void testKeyBasedEncryptionAndDecryption() throws Exception {

AES256JNCryptor cryptor = new AES256JNCryptor();
byte[] ciphertext = cryptor.encryptData(plaintext, encryptionKey, hmacKey);

// Check version
assertEquals(AES256v3Ciphertext.EXPECTED_VERSION, ciphertext[0]);

byte[] newPlaintext = cryptor.decryptData(ciphertext, encryptionKey,
hmacKey);
assertArrayEquals(plaintext, newPlaintext);
Expand Down

0 comments on commit 478a4cb

Please sign in to comment.