2013-03-18 2 views
0
try{ 
    String plainData="my name is laksahan",cipherText,decryptedText; 
    KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 
    keyGen.init(128); 
    SecretKey secretKey = keyGen.generateKey(); 
    Cipher aesCipher = Cipher.getInstance("AES"); 
    aesCipher.init(Cipher.ENCRYPT_MODE,secretKey); 
    byte[] byteDataToEncrypt = plainData.getBytes(); 
    byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
    cipherText = new BASE64Encoder().encode(byteCipherText); 
System.out.println(cipherText); 
}catch(Exception e){ 

} 

У меня также есть код дешифрования, но я хочу расшифровать сообщение, используя вывод cipherText.Как дешифровать шифрование сообщений здесь?

eg; мой вывод зашифрованного текста - uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10=.

Я хочу, чтобы расшифровать это: uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10=

Спасибо заранее.

+2

ОК - теперь я могу прочитать код (PLES узнать, как использовать форматирование кода). Для начала измените 'catch (Исключение e) { }' to 'catch (Исключение e) { e.printStckTrace(); } ' –

ответ

1

попробовать это

byte[] data = new BASE64Decoder().decodeBuffer(cipherData); 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     aesCipher.init(Cipher.DECRYPT_MODE, secretKeyUsed while encrypting); 
     byte[] plainData = aesCipher.doFinal(data); 
     return new String(plainData); 
+0

Как я могу предоставить дешифрованный код, который у меня есть? – user2136160

+0

cipherText = новый BASE64Encoder(). Декодирование (расшифрованный код); – PSR

+0

еще путая сэр, вы можете это объяснить ... – user2136160

3

Попробуйте этот код, он прекрасно работает на моем pc.Good удачи!

import javax.crypto.Cipher; 
import javax.crypto.KeyGenerator; 
import javax.crypto.SecretKey; 
import sun.misc.BASE64Encoder; 
import sun.misc.BASE64Decoder; 

public class AESExample 
{ 
    public static void main(String[] args) 
    { 
     try 
     { 
      String plainData = "my name is laksahan", cipherText, decryptedText; 
      KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 
      keyGen.init(128); 
      SecretKey secretKey = keyGen.generateKey(); 
      cipherText = encrypt(plainData, secretKey); 
      System.out.println(cipherText); 
      decryptedText = decrypt(cipherText, secretKey); 
      System.out.println(decryptedText); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

    } 

    public static String encrypt(String plainData, SecretKey secretKey) throws Exception 
    { 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     aesCipher.init(Cipher.ENCRYPT_MODE, secretKey); 
     byte[] byteDataToEncrypt = plainData.getBytes(); 
     byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
     return new BASE64Encoder().encode(byteCipherText); 
    } 

    public static String decrypt(String cipherData, SecretKey secretKey) throws Exception 
    { 
     byte[] data = new BASE64Decoder().decodeBuffer(cipherData); 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     aesCipher.init(Cipher.DECRYPT_MODE, secretKey); 
     byte[] plainData = aesCipher.doFinal(data); 
     return new String(plainData); 
    } 

} 

Если вы хотите использовать ключ клиента, попробуйте следующий код, просто помните, длина ключа 128 бит. Кстати, я предпочитаю хранить мой ключ в файле хранилища ключей!

import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 

import sun.misc.BASE64Encoder; 
import sun.misc.BASE64Decoder; 

public class AESExample 
{ 

    public static void main(String[] args) 
    { 
     try 
     { 
      byte[]key={-4, -14, 106, -75, -9, 65, -95, 77, -52, 73, -87, -101, 80, 94, -59, -66}; 
      String plainData = "my name is laksahan", cipherText, decryptedText; 
      System.out.println(key.length); 
      cipherText = encrypt(plainData, key); 
      System.out.println(cipherText); 
      decryptedText = decrypt(cipherText, key); 
      System.out.println(decryptedText); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    public static String encrypt(String plainData, byte[] key) throws Exception 
    { 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); 
     aesCipher.init(Cipher.ENCRYPT_MODE, keySpec); 
     byte[] byteDataToEncrypt = plainData.getBytes(); 
     byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
     return new BASE64Encoder().encode(byteCipherText); 
    } 

    public static String decrypt(String cipherData, byte[] key) throws Exception 
    { 
     byte[] data = new BASE64Decoder().decodeBuffer(cipherData); 
     SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     aesCipher.init(Cipher.DECRYPT_MODE, keySpec); 
     byte[] plainData = aesCipher.doFinal(data); 
     return new String(plainData); 
    } 

} 
+0

Это полезно, но я хочу предоставить свой зашифрованный пароль и расшифровать его. если вы можете предоставить код примера, это будет так полезно для меня. – user2136160

+0

@ user2136160 Что вы подразумеваете под «предоставлением зашифрованного пароля и расшифровкой его»? –

+0

Вы имеете в виду, что где-то хранили свой пароль и используете пароль для шифрования или дешифрования данных? Я изменил свой ответ на код –

Смежные вопросы