CopyPastor

Detecting plagiarism made easy.

Score: 0.8138383030891418; Reported for: String similarity Open both answers

Possible Plagiarism

Reposted on 2014-01-16
by Arpana

Original Post

Original - Posted on 2014-01-16
by Arpana



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Even though its too late but still I'm giving my solution for others.Please refer below working code to encrypt and decrypt data.

public static byte[] encrypt(String value) { byte[] encrypted = null; try { byte[] raw = new byte[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'}; Key skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[cipher.getBlockSize()]; IvParameterSpec ivParams = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, skeySpec,ivParams); encrypted = cipher.doFinal(value.getBytes()); System.out.println("encrypted string:" + encrypted.length); } catch (Exception ex) { ex.printStackTrace(); } return encrypted; }
public static byte[] decrypt(byte[] encrypted) { byte[] original = null; Cipher cipher = null; try { byte[] raw = new byte[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'}; Key key = new SecretKeySpec(raw, "AES"); cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //the block size (in bytes), or 0 if the underlying algorithm is not a block cipher byte[] ivByte = new byte[cipher.getBlockSize()]; //This class specifies an initialization vector (IV). Examples which use //IVs are ciphers in feedback mode, e.g., DES in CBC mode and RSA ciphers with OAEP encoding operation. IvParameterSpec ivParamsSpec = new IvParameterSpec(ivByte); cipher.init(Cipher.DECRYPT_MODE, key, ivParamsSpec); original= cipher.doFinal(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return original; }
To solve problem few changes you have to do in your code just make return type of encrypt() API of your class is byte[] array and in decrypt() API of your class pass byte[] array so by doing this you can solve input length multiple of 16 exception.
Please refer below working code :
public static byte[] encrypt(String value) { byte[] encrypted = null; try { byte[] raw = new byte[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'}; Key skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[cipher.getBlockSize()]; IvParameterSpec ivParams = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, skeySpec,ivParams); encrypted = cipher.doFinal(value.getBytes()); System.out.println("encrypted string:" + encrypted.length); } catch (Exception ex) { ex.printStackTrace(); } return encrypted; }
public static byte[] decrypt(byte[] encrypted) { byte[] original = null; Cipher cipher = null; try { byte[] raw = new byte[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'}; Key key = new SecretKeySpec(raw, "AES"); cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //the block size (in bytes), or 0 if the underlying algorithm is not a block cipher byte[] ivByte = new byte[cipher.getBlockSize()]; //This class specifies an initialization vector (IV). Examples which use //IVs are ciphers in feedback mode, e.g., DES in CBC mode and RSA ciphers with OAEP encoding operation. IvParameterSpec ivParamsSpec = new IvParameterSpec(ivByte); cipher.init(Cipher.DECRYPT_MODE, key, ivParamsSpec); original= cipher.doFinal(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return original; }

        
Present in both answers; Present only in the new answer; Present only in the old answer;