• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Encryption and Decryption Using JNCryptor

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys

I'm making an application which will use encrypt data password from iOS , Now I need to decrypt password and validate password in java server code,
So for Encryption and Decryption I'm using JNCryptor , uses link https://github.com/RNCryptor/JNCryptor
Note : Encrypt data is working and I'm getting problem with decryptData.
Here below I shared code,

public class SecurityJNCryptor {

byte[] salt = new String("12345678").getBytes();
static final String HMAC_ALGORITHM = "HmacSHA256";
byte[] hmac = new String(HMAC_ALGORITHM).getBytes();
static String pass = "Java@123";
SecretKey decryptionKey;
SecretKey hmacKey;


public SecurityJNCryptor() throws InvalidKeySpecException, CryptorException{
AES256JNCryptor aesCiphertext = new AES256JNCryptor();
decryptionKey = aesCiphertext.keyForPassword(pass.toCharArray(), salt);
hmacKey = aesCiphertext.keyForPassword(pass.toCharArray(), hmac);

}


public String encriptorData(String password){
byte [] cipher = null;
String base64EncryptedData = null;
AES256JNCryptor cryptor = new AES256JNCryptor();
byte[] plaintext = "Hello , World!".getBytes();
try{
cipher = cryptor.encryptData(plaintext, password.toCharArray());
System.out.println(cipher);
base64EncryptedData = new sun.misc.BASE64Encoder().encodeBuffer(cipher);
System.out.println("Encrypted Data " + base64EncryptedData);

}catch(CryptorException exception){

}
return base64EncryptedData;

}

public String decryptData(String data) throws InvalidHMACException, CryptorException{
byte [] cipher = null;
AES256JNCryptor cryptor = new AES256JNCryptor();
cipher = cryptor.decryptData(data.getBytes(), decryptionKey , hmacKey);
return Base64.encode(cipher);

}

public static void main(String[] args) throws InvalidKeySpecException, InvalidHMACException, CryptorException {
SecurityJNCryptor decryptor = new SecurityJNCryptor();
String a = decryptor.encriptorData(pass);
String b = decryptor.decryptData(a);
System.out.println(b);

}

while running this code I'm getting below exception.
Exception in thread "main" java.lang.IllegalArgumentException: Salt value must be 8 bytes.
at org.cryptonode.jncryptor.Validate.isTrue(Validate.java:27)
at org.cryptonode.jncryptor.AES256JNCryptor.keyForPassword(AES256JNCryptor.java:156)
at com.playstore.util.SecurityJNCryptor.<init>(SecurityJNCryptor.java:27)
at com.playstore.util.SecurityJNCryptor.main(SecurityJNCryptor.java:59)

 
Get me the mayor's office! I need to tell him about this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic