Chiffrer Déchiffrer ma touche pas java généré
Je suis nouveau à la cryptographie, j'ai donc une question:
Comment puis-je créer mon propre clé (disons comme une chaîne "1234"). Parce que j'ai besoin de chiffrer une chaîne avec une clé (défini par moi), enregistrer la chaîne cryptée dans une base de données et lorsque je veux l'utiliser, de le prendre à partir de la base de données et le déchiffrer avec la clé connu par moi.
J'ai ce code :
import java.security.InvalidKeyException;
import java.security.*;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
public class LocalEncrypter {
private static String algorithm = "PBEWithMD5AndDES";
//private static Key key = null;
private static Cipher cipher = null;
private static SecretKey key;
private static void setUp() throws Exception {
///key = KeyGenerator.getInstance(algorithm).generateKey();
SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
String pass1 = "thisIsTheSecretKeyProvidedByMe";
byte[] pass = pass1.getBytes();
SecretKey key = factory.generateSecret(new DESedeKeySpec(pass));
cipher = Cipher.getInstance(algorithm);
}
public static void main(String[] args)
throws Exception {
setUp();
byte[] encryptionBytes = null;
String input = "1234";
System.out.println("Entered: " + input);
encryptionBytes = encrypt(input);
System.out.println(
"Recovered: " + decrypt(encryptionBytes));
}
private static byte[] encrypt(String input)
throws InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] inputBytes = input.getBytes();
return cipher.doFinal(inputBytes);
}
private static String decrypt(byte[] encryptionBytes)
throws InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException {
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes =
cipher.doFinal(encryptionBytes);
String recovered =
new String(recoveredBytes);
return recovered;
}
}
Exception in thread "main" java.security.spec.InvalidKeySpecException: Invalid key spec
at com.sun.crypto.provider.PBEKeyFactory.engineGenerateSecret(PBEKeyFactory.java:114)
at javax.crypto.SecretKeyFactory.generateSecret(SecretKeyFactory.java:335)
at LocalEncrypter.setUp(LocalEncrypter.java:22)
at LocalEncrypter.main(LocalEncrypter.java:28)
OriginalL'auteur user1069937 | 2011-11-28
Vous devez vous connecter pour publier un commentaire.
Un KeyGenerator génère des clés aléatoires. Puisque vous connaissez le secret de la clé, ce que vous avez besoin est un SecretKeyFactory. Obtenir une instance de votre algorithme (DESede), puis d'appeler ses generateSecret méthode avec un exemple de DESedeKeySpec comme argument:
Voici un exemple complet qui fonctionne. Comme je l'ai dit, DESedeKeySpec doit être utilisé avec le DESede algorithme. À l'aide d'un DESede clé avec PBEWithMD5AndDES n'a pas de sens.
Voir mon edit, puis. Vous devriez vous nous avez montré ce que vous avez essayé dans votre question, et nous a dit ce qui n'a pas de travail.
je les ai converti en une chaîne de caractères dans un bye[] , utilisé secretkeyfactory , mais maintenant il me donne l'erreur : Aucune installé fournisseur prend en charge cette clé: (null) , ------ SecretKeyFactory usine = SecretKeyFactory.getInstance("DESede"); String rapidair1 = "123456789132456789123456789"; byte [] = rapidair1.getBytes(); SecretKey key = factory.generateSecret(nouveau DESedeKeySpec(pass));
DESede est pris en charge nativement par le JRE: Voir docs.oracle.com/javase/6/docs/technotes/guides/security/.... Merci d'éditer votre question avec le code, la trace de pile d'exception, et potentiellement installés les fournisseurs.
j'ai modifié ceci :private static String algorithme = "PBEWithMD5AndDES"; et maintenant, je reçois l'erreur : Invalid Key Spec
OriginalL'auteur JB Nizet
Bien, j'ai trouvé la solution après la combinaison d'ici et d'ailleurs. La cryptés va être au format Base64 Chaîne pour la sécurité de la sauvegarde en fichier xml.
OriginalL'auteur winsetter