1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
| public class Fichier {
private static Vector<Bdd> v1;
private static Vector<Table> v2;
private static Vector<Champ> v3;
public static void main(String[] args) throws IOException, InterruptedException {
InputStream flux=new FileInputStream("D:\\bdd.sql");
InputStreamReader lecture=new InputStreamReader(flux);
BufferedReader buff=new BufferedReader(lecture);
String ligne;
v1 = new Vector<Bdd>();
v2 = new Vector<Table>();
v3 = new Vector<Champ>();
int n = 10;
while ((ligne=buff.readLine())!=null)
{
if(ligne.contains("CREATE DATABASE"))
{
String tab[]= ligne.split(" ");
String chaine=Random.Random_chaine(n);
//la condition qui verifie que random me génère pas le mm code
while(v1.contains(chaine))
chaine=Random.Random_chaine(n);
System.out.println(tab[2]+":"+chaine);
Bdd T1= new Bdd();
T1.BddAvant=tab[2];
T1.BddObfus=chaine;
v1.add(T1);
//System.out.println(v1.elementAt(0).BddObfus);
while(!(ligne.contains("SET autocommit=@old_autocommit")))
{
while((ligne.contains("CREATE TABLE"))&&(!(ligne.contains("CREATE TABLE IF"))))
{
String tabT[]= ligne.split(" ");
String chaineT=Random.Random_chaine(n);
//la condition qui verifie que random me génère pas le mm code
while(v2.contains(chaineT))
chaineT=Random.Random_chaine(n);
//afficher la table et tables obfusquée
System.out.println("La table \n");
System.out.println(tabT[2]+":"+chaineT);
//ajouter les infos au vecteur des tables
Table T2= new Table();
T2.nomBdd=tab[2];
T2.tabAvant=tabT[2];
T2.tabObfus=chaineT;
v2.add(T2);
ligne=buff.readLine();
//recupperer les champs
System.out.println("les champs \n");
while(!(ligne.startsWith(")"))){
if((!(ligne.contains("PRIMARY")))&&(!(ligne.contains("KEY")))&&(!(ligne.contains("CONSTRAINT"))))
{
String tabC[]=ligne.split("`");
String chaineC=Random.Random_chaine(n);
//la condition qui verifie que random me génère pas le mm code
while(v3.contains(chaineC))
chaineC=Random.Random_chaine(n);
//afficher le champs et le champs obfusqué
System.out.println(tabC[1]+":"+chaineC);
Champ T3 = new Champ();
T3.champAvant=tabC[1];
T3.champsObfus=chaineC;
T3.nomBdd=tab[2];
T3.nomTab=tabT[2];
v3.add(T3);
}
ligne=buff.readLine();
}
ligne=buff.readLine();
}
ligne=buff.readLine();
}
}}
//le création du nouveau fichier sql
InputStream flux1=new FileInputStream("D:\\bdd.sql");
InputStreamReader lecture1=new InputStreamReader(flux1);
BufferedReader buffer1=new BufferedReader(lecture1);
String ligne1;
FileWriter nouveau = null;
nouveau = new FileWriter("D:\\nouveau.sql");
while ((ligne1=buffer1.readLine())!=null)
{
while((ligne1.startsWith("SET")))
{
nouveau.write(ligne1+"\n");
System.out.println(ligne1+"\n");
ligne1=buffer1.readLine();
}
if(ligne1.contains("CREATE DATABASE"))
{
int k;
for(k=0 ; k<v1.size();k++)
{
if((ligne1.contains((CharSequence) v1.elementAt(k).BddAvant)))
{
System.out.println(ligne1+"\n");
ligne1 =ligne1.replaceAll(v1.elementAt(k).BddAvant,v1.elementAt(k).BddObfus);
nouveau.write(ligne1+"\n");
ligne1=buffer1.readLine(); //sauter la ligne
// tant qu'on est pas arrivé à la fin de la bdd j'ai mis par rapport au mot Dump completed je suis pas sûre
while(!(ligne1.contains("Dump completed ")))
{
if(ligne1.contains("USE"))
{
if((ligne1.contains((CharSequence) v1.elementAt(k).BddAvant)))
{
System.out.println(ligne1+"\n");
ligne1 =ligne1.replaceAll(v1.elementAt(k).BddAvant,v1.elementAt(k).BddObfus);
nouveau.write(ligne1+"\n");
ligne1=buffer1.readLine();
}
}
// on modifie toute les lignes où il y a les tables
while((ligne1.contains("TABLE")))
{
int k2;
for(k2=0 ; k2<v2.size();k2++)
{
if((ligne1.contains((CharSequence) v2.elementAt(k2).tabAvant))&&(v2.elementAt(k2).nomBdd.contentEquals(v1.elementAt(k).BddAvant)))
{
ligne1 =ligne1.replaceAll(v2.elementAt(k2).tabAvant,v2.elementAt(k2).tabObfus);
// on modifie les champs si ils sont après les tables dans le cas de la création par exemple
for(int k3=0 ; k3<v3.size();k3++)
{
if((ligne1.contains((CharSequence) v3.elementAt(k3).champAvant))&&(v3.elementAt(k3).nomBdd.contentEquals(v1.elementAt(k).BddAvant))&&(v3.elementAt(k3).nomTab.contentEquals(v2.elementAt(k2).tabAvant)) )
ligne1 =ligne1.replaceAll(v3.elementAt(k3).champAvant,v3.elementAt(k3).champsObfus);
}
nouveau.write(ligne1+"\n");
System.out.println(ligne1+"\n");
ligne1=buffer1.readLine();
for(int k3=0 ; k3<v3.size();k3++)
{
while((ligne1.contains((CharSequence) v3.elementAt(k3).champAvant))&&(v3.elementAt(k3).nomBdd.contentEquals(v1.elementAt(k).BddAvant))&&(v3.elementAt(k3).nomTab.contentEquals(v2.elementAt(k2).tabAvant)) )
{
ligne1 =ligne1.replaceAll(v3.elementAt(k3).champAvant,v3.elementAt(k3).champsObfus);
// break;
nouveau.write(ligne1+"\n");
System.out.println(ligne1+"\n");
ligne1=buffer1.readLine();
}
}
while((ligne1.contains("PRIMARY"))||(ligne1.contains("KEY")))
{
for(int k3=0 ; k3<v3.size();k3++)
{
if((ligne1.contains((CharSequence) v3.elementAt(k3).champAvant))&&(v3.elementAt(k3).nomBdd.contentEquals(v1.elementAt(k).BddAvant))&&(v3.elementAt(k3).nomTab.contentEquals(v2.elementAt(k2).tabAvant)) )
{
ligne1 =ligne1.replaceAll(v3.elementAt(k3).champAvant,v3.elementAt(k3).champsObfus);
System.out.println(ligne1+"\n");
}
}
nouveau.write(ligne1+"\n");
ligne1=buffer1.readLine();
}
if(ligne1.contains("CONSTRAINT")){
for(k2=0 ; k2<v2.size();k2++) {
for(int k3=0 ; k3<v3.size();k3++)
{
while((ligne1.contains((CharSequence) v3.elementAt(k3).champAvant))&&(v3.elementAt(k3).nomBdd.contentEquals(v1.elementAt(k).BddAvant))&&(v3.elementAt(k3).nomTab.contentEquals(v2.elementAt(k2).tabAvant)) )
{
ligne1 =ligne1.replaceAll(v3.elementAt(k3).champAvant,v3.elementAt(k3).champsObfus);
nouveau.write(ligne1+"\n");
ligne1=buffer1.readLine();
}
}
}
}
nouveau.write(ligne1+"\n");
}
}
ligne1=buffer1.readLine();
}
nouveau.write(ligne1+"\n");
ligne1=buffer1.readLine();
while(ligne1.contains("INSERT INTO"))
{
for(int k2=0 ; k2<v2.size();k2++)
{
if((ligne1.contains((CharSequence) v2.elementAt(k2).tabAvant))&&(v2.elementAt(k2).nomBdd.contentEquals(v1.elementAt(k).BddAvant)))
ligne1 =ligne1.replaceAll(v2.elementAt(k2).tabAvant,v2.elementAt(k2).tabObfus);
}
nouveau.write(ligne1+"\n");
ligne1=buffer1.readLine();
}
}
}
}
}
}
buff.close();
buffer1.close();
nouveau.close();
}}
la classe random:
public class Random {
// function to generate a random string of length n
static String Random_chaine(int n)
{
// choisir un caractère à paritr de cette chaine
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"abcdefghijklmnopqrstuvxyz";
// créer StringBuffer taille de str
// StringBuilder une chaine modifiable
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; i++) {
// générer un nombre random entre
// 0 à la taille de str
int index = (int)(str.length() * Math.random());
// ajouter un caractere un par un à la fin de sb
// charAt return le caractere a la position index
sb.append(str.charAt(index));
}
return sb.toString();
}
static String compare(String str1, String str2) {
int n=10;
if(str1==str2)
str1=Random.Random_chaine(n);
return str1;
}
}
les classes de vecteur:
public class Bdd {
String BddAvant;
String BddObfus;
}
public class Table {
String tabAvant;
String tabObfus;
String nomBdd;
}
public class Champ {
String champAvant;
String champsObfus;
String nomBdd;
String nomTab;
} |
Partager