I have a pre-written code which is used to cipher the given plain text or vice-versa .
The class has 3 methods, where in 2 methods can be used for encrypting and decrypting respectively.
public class SqlCipherUtil {
private Cipher ecipher;
private Cipher dcipher;
public String encryptString(String pStrPlainText) {
try {
generateKey();
byte[] utf8 = pStrPlainText.getBytes("UTF8");
byte[] enc = this.ecipher.doFinal(utf8);
return new BASE64Encoder().encode(enc);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String decryptString(String pStrCipherText){
try {
generateKey();
byte[] dec = new BASE64Decoder().decodeBuffer(pStrCipherText);
byte[] utf8 = this.dcipher.doFinal(dec);
return new String(utf8, "UTF8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* This method is used to generate the encrypted key.
*/
private void generateKey() {
try {
byte[] decodedStr = new BASE64Decoder().decodeBuffer("rA/LUdBA/hA=");
SecretKey key = new SecretKeySpec(decodedStr, "DES");
this.ecipher = Cipher.getInstance("DES");
this.dcipher = Cipher.getInstance("DES");
this.ecipher.init(1, key);
this.dcipher.init(2, key);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The key present in the class cannot be changed to any other key
in line byte[] decodedStr = new BASE64Decoder().decodeBuffer("rA/LUdBA/hA=");
,
and it is giving an exception.
java.security.InvalidKeyException: Invalid key length: 9 bytes
at com.sun.crypto.provider.DESCipher.engineGetKeySize(DashoA13*..)
at javax.crypto.Cipher.b(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
I tried out the below code, and I am getting exactly 8 bytes in the array.
public static void main(String[] args) throws IOException {
byte[] decodedStr = new BASE64Decoder().decodeBuffer("rA/LUdBA/hA=");
for(byte b : decodedStr){
System.out.print(b);
System.out.print(" ");
}
}
}
Any other combination of the key will make the byte array size more than 8 or less than 7.
What is the concept behind getting the byte array size 8 ?
What should be done to use custom key combination or our custom generated keys ?
Please answer Both the questions.
if your target is encode and decode the string, Use Base64
.
public class PasswordCodecHandler {
Base64 codec = null;
public PasswordCodecHandler() {
codec = new Base64();
}
public String encode(String password) {
byte[] temp;
String encodedPassword = null;
temp = codec.encode(password.getBytes());
encodedPassword = new String(temp);
return encodedPassword;
}
public String decode(byte[] encodedPassword) {
byte[] temp;
String decodedPassword;
temp = codec.decode(encodedPassword);
decodedPassword = new String(temp);
return decodedPassword;
}
public static void main(String[] args) {
PasswordCodecHandler passwordCodecHandler = new PasswordCodecHandler();
String s1 = passwordCodecHandler.encode("password");
System.out.println(s1);
String s2 = passwordCodecHandler.encode("admin");
System.out.println(s2);
String s3 = passwordCodecHandler.encode("administrator");
System.out.println(s3);
String s4 = passwordCodecHandler.encode("123456");
System.out.println(s4);
}
}
For other data type : it can be java.lang.OutOfMemoryError
based on the your memory allocation size
/* Download apache common-io.xxx. jar*/
public class CodecHandler {
Base64 codec = null;
public CodecHandler() {
codec = new Base64();
}
public byte[] encode(byte[] decoded) {
return codec.encode(decoded);
}
public byte[] decode(byte[] encoded) {
return codec.decode(encoded);
}
public static void main(String[] args) throws IOException {
File file = new File("D:/Test.mp4");
byte[] fileByteArray = FileUtils.readFileToByteArray(file);
CodecHandler codecHandler = new CodecHandler();
byte[] encoded = codecHandler.encode(fileByteArray);
System.out.println("Byte Size : " + encoded.length);
byte[] decode = codecHandler.decode(encoded);
FileUtils.writeByteArrayToFile(new File("C:/Test.mp4"), decode);
}
}