wood burning stoves 2.0*
The moose likes Security and the fly likes 3des with 2 different keys in java getting null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "3des with 2 different keys in java getting null" Watch "3des with 2 different keys in java getting null" New topic
Author

3des with 2 different keys in java getting null

t sathya narayana
Greenhorn

Joined: Jan 23, 2014
Posts: 19
import java.security.spec.*;
import javax.crypto.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESedeEncryption {
public static void main(String[] args) {

SecretKey k1 = generateDESkey();
SecretKey k2 = generateDESkey();

String firstEncryption = desEncryption("plaintext", k1);
System.out.println("firstEncryption Value : "+firstEncryption);
String decryption = desDecryption(firstEncryption, k2);
System.out.println("decryption Value : "+decryption);
String secondEncryption = desEncryption(decryption, k1);
System.out.println("secondEncryption Value : "+secondEncryption);

}

public static SecretKey generateDESkey() {
KeyGenerator keyGen = null;
try {
keyGen = KeyGenerator.getInstance("DESede");
} catch (Exception ex) {
}
keyGen.init(112); // key length 56
SecretKey secretKey = keyGen.generateKey();
return secretKey;
}

public static String desEncryption(String strToEncrypt, SecretKey desKey) {
try {
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, desKey);
BASE64Encoder base64encoder = new BASE64Encoder();
byte[] encryptedText = cipher.doFinal(strToEncrypt.getBytes());
String encryptedString =base64encoder.encode(encryptedText);
return encryptedString;
} catch (Exception ex) {
}
return null;
}

public static String desDecryption(String strToDecrypt, SecretKey desKey) {
try {
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, desKey);
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encryptedText = base64decoder.decodeBuffer(strToDecrypt);
byte[] plainText = cipher.doFinal(encryptedText);
String decryptedString= bytes2String(plainText);
return decryptedString;
} catch (Exception ex) {
}
return null;
}

private static String bytes2String(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i <bytes.length; i++) {
stringBuffer.append((char) bytes[i]);
}
return stringBuffer.toString();
}
}
while i'm running the above code i'm getting null values. please help.

output:

firstEncryption Value : jAihaGgiOzBSFwBWo3gpbw==

decryption Value : null

secondEncryption Value : null
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
Obviously because there's an exception that
is ignored by the code. You should never put empty catch blocks into your code. At least print the stack trace to where you will see it.


Ping & DNS - my free Android networking tools app
t sathya narayana
Greenhorn

Joined: Jan 23, 2014
Posts: 19
Ulf Dittmer wrote:Obviously because there's an exception that
is ignored by the code. You should never put empty catch blocks into your code. At least print the stack trace to where you will see it.



getting error:

firstEncryption Value : ygGPwCllarWvSH8td55j/w==
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.DESedeCipher.engineDoFinal(DESedeCipher.java:
294)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at DESedeEncryption.desDecryption(DESedeEncryption.java:145)
at DESedeEncryption.main(DESedeEncryption.java:107)
decryption Value : null
java.lang.NullPointerException
at DESedeEncryption.desEncryption(DESedeEncryption.java:130)
at DESedeEncryption.main(DESedeEncryption.java:109)
secondEncryption Value : null
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
Start by working with byte arrays - leave out the base64 stuff.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

That is not how 2 key 3DES is computed. Both your encryption and decryption stages use PKCS5 padding so you use PKCS5 padding at each stage. You only need PKCS5 padding for for the first stage of encryption and last stage of decryption. The other stages need no padding.

Note - the best way to perform 3DES with ECB and PKCS5 padding is to use "DESede/ECB/PKCS5Padding" as the Cipher specification and build the 24 bytes (56*3 key bits + 24 parity bits) of the 168 bit key as the concatenation of the first key, the second key and then the first key again.

Note 1 - DES is deprecated in favour of AES.

Note 2 - ECB is not considered secure as it allow forging of ciphertext by splicing ciphertext from two or more other ciphertext.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: 3des with 2 different keys in java getting null
 
Similar Threads
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8
Length of Encrypted String Longer than Original String... Help
2-key TDES and Java
error coming while decrypting...
BadPaddingException using AES