· 7 years ago · Nov 22, 2018, 01:48 PM
1package com.android.test;
2
3import java.io.UnsupportedEncodingException;
4import java.security.InvalidKeyException;
5import java.security.NoSuchAlgorithmException;
6import java.security.spec.InvalidKeySpecException;
7import java.security.spec.KeySpec;
8//import java.util.Random;
9
10import javax.crypto.Cipher;
11import javax.crypto.NoSuchPaddingException;
12import javax.crypto.SecretKey;
13import javax.crypto.SecretKeyFactory;
14import javax.crypto.spec.DESedeKeySpec;
15import javax.crypto.spec.IvParameterSpec;
16
17import android.util.Base64;
18
19public final class Prueba3DES {
20 private static final String ALGOR = "DESede";
21 private static final String ALGOR_TRANSFORM = "DESede/CBC/PKCS7Padding";
22 private static final int BASE64_OPTIONS = Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP;
23 private static final byte[] IVIV = {48,12,1,0x3a,17, (byte)0xb1, 0x2e, 103};
24
25
26 //public Prueba3DES() {
27 //inicializar IVIV
28 //IVIV =
29 //final Random rgen= new Random();
30 //rgen.nextBytes(IVIV);
31 //}
32
33 public final BoolString tryCifrar(String inString, String strKey) {
34 boolean success = true;
35 String err="";
36 String outString="error cifrando"; //BoolString.value
37
38 try {
39 //byte[] byteKey=strKey.getBytes("UTF8");
40 byte[] byteKey=strKey.getBytes("ASCII");
41 if (byteKey.length != 24) {
42 success = false;
43 err = "Key is " + byteKey.length + " bytes. Key must be exactly 24 bytes in length.";
44 throw new Exception(err); //could also return here
45 }
46 final KeySpec ks=new DESedeKeySpec(byteKey);
47 final SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGOR);
48 final SecretKey sk = skf.generateSecret(ks);
49
50 //Random rgen=new Random();
51 ////for (int i=0;i<8; i++) IVIV[i]=(byte)rgen.nextInt(256);
52 //rgen.nextBytes(IVIV);
53 final IvParameterSpec iv = new IvParameterSpec(IVIV); //new byte[8]);
54 final Cipher cph = Cipher.getInstance(ALGOR_TRANSFORM); //"DESede"
55 cph.init(Cipher.ENCRYPT_MODE, sk, iv);
56 final byte[] byteInString= inString.getBytes("UTF-8"); //ASCII
57 final byte[] byteEncoded= cph.doFinal(byteInString);
58 outString= Base64.encodeToString(byteEncoded, BASE64_OPTIONS);
59 }
60 catch (UnsupportedEncodingException e) {err="Unalbe to convert key to byte array."; success=false;}
61 catch (InvalidKeyException e) {err="Unable to generate KeySpec from key"; success=false;}
62 catch (NoSuchAlgorithmException e) {err="Unable to find algorith."; success=false;}
63 catch (InvalidKeySpecException e) {err="Invalid Key Specification."; success=false;}
64 catch (NoSuchPaddingException e) {err="No such padding."; success=false;}
65 catch (IllegalArgumentException e) {err="Illegal argument."; success=false;}
66 catch (Exception e) {err=e.getMessage(); success=false;}
67 return new BoolString(success, err, outString);
68 }
69
70
71 public final BoolString tryDescifrar(String inString, String strKey) {
72 boolean success= true;
73 String err="";
74 String outString="error descrifrando";
75
76 try {
77 //La cadena viene codificada con Base64. Deshacer esto y guardar array de bytes en buffEntrada
78
79 final byte[] buffEntrada= Base64.decode(inString, BASE64_OPTIONS);
80 //err= array2HexStr(buffEntrada);
81
82 //Generar IV y key, en base a random
83 //byte[] IVIV= new byte[8]; //= {48,12,1,0x3a,17,(byte) 0xb1, 0x2e, 103};
84 //Random rgen=new Random();
85 //rgen.nextBytes(IVIV);
86
87 final byte[] byteKey=strKey.getBytes(); //ASCII
88
89 //Descifrar
90 final KeySpec ks= new DESedeKeySpec(byteKey);
91 final SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGOR);
92 final SecretKey sk = skf.generateSecret(ks);
93
94 final IvParameterSpec iv = new IvParameterSpec(IVIV); //new byte[8]);
95 final Cipher cph = Cipher.getInstance(ALGOR_TRANSFORM);
96 cph.init(Cipher.DECRYPT_MODE, sk, iv);
97 final byte[] byteDecoded= cph.doFinal(buffEntrada);
98 outString= new String(byteDecoded, "UTF-8");
99 }
100 catch (Exception e) { err=e.getMessage(); success=false;}
101
102 return new BoolString(success, err, outString);
103 }
104
105/*
106 public static final String array2HexStr(byte[] arr) throws UnsupportedEncodingException {
107 final byte[] HEX_CHARS = {
108 (byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7',
109 (byte)'8',(byte)'9',(byte)'a',(byte)'b',(byte)'c',(byte)'d',(byte)'e',(byte)'f' };
110 byte[] hex = new byte[2*arr.length];
111 int index=0;
112 int v;
113 for(byte b:arr) {
114 v=b & 0xff;
115 hex[index++]=HEX_CHARS[v>>>4];
116 hex[index++]=HEX_CHARS[v & 0x0f];
117 }
118 return new String(hex, "ASCII");
119 }
120*/
121
122}