2016-01-06 53 views
-1

以下是用於生成加密和解密數據密鑰的管理代碼。相同的密鑰用於加密和解密當嘗試解密加密數據時,AES加密給出了奇怪的符號

public class AdminKeyGenerator { 

    private static final String ALGORITHM = "AES"; 


    public static SecretKey generateKey() throws NoSuchAlgorithmException, 
      NoSuchPaddingException { 

     KeyGenerator genarator = KeyGenerator.getInstance(ALGORITHM); 
     SecretKey secretkey = genarator.generateKey(); 

     return secretkey; 

    } 

    public static void saveKey(SecretKey key) { 

     File keyFile = new File("/home/thamiz/workspace/keyFile.txt"); 
     try{ 

      keyFile.createNewFile(); 
      FileWriter keyWriter = new FileWriter(keyFile); 
      BufferedWriter buffKey = new BufferedWriter(keyWriter); 

      char[] hex = encodeHex(key.getEncoded()); 

      buffKey.write(hex); 
      buffKey.flush(); 
      buffKey.close(); 


     }catch(FileNotFoundException e){ 
      System.out.println("Decryption fails"); 

     } catch (IOException e) { 
      System.out.println("Decryption fails"); 
     } 

    } 



    public static SecretKey loadKey() throws DecoderException{ 


     File keyFile = new File("/home/thamiz/workspace/keyFile.txt"); 
     String data = null; 
     byte[] encoded = null; 

     try{ 
     FileReader keyReader = new FileReader(keyFile); 
     BufferedReader buffKeyRead = new BufferedReader(keyReader); 

     data = buffKeyRead.readLine(); 
     encoded = decodeHex(data.toCharArray()); 
     buffKeyRead.close(); 


     }catch(IOException e){ 
      System.out.println("Decryption fails"); 
     } 


     return new SecretKeySpec(encoded, ALGORITHM); 



    } 


    public static void main(String[] args) throws NoSuchAlgorithmException, 
      NoSuchPaddingException { 

     SecretKey secretkey = AdminKeyGenerator.generateKey(); 

     AdminKeyGenerator.saveKey(secretkey); 



    } 


Following is the client side code. client used the admin key and encrypt his card details. 


private static String debitcardType; 
private static int debitCardNumber; 
private static int debitcardCVV; 
    private static Date debitcardExpiryDate; 


    public static void storeCardDetails() throws ParseException { 

    Scanner in = new Scanner(System.in); 
    boolean anotherCardDetail = false; 
    SimpleDateFormat expiry = new SimpleDateFormat("dd-MM-yyyy"); 

    do { 

     System.out.println("Enter your debit card type:"); 
     debitcardType = in.next(); 

     System.out.println("Enter your debit card number:"); 
     debitCardNumber = in.nextInt(); 

     System.out.println("Enter your debit card cvv number:"); 
     debitcardCVV = in.nextInt(); 

     System.out.println("Enter your debit card expiry date in the format of dd-MM-yyyy:"); 
     String date = in.next(); 
     debitcardExpiryDate = expiry.parse(date); 

     System.out.println("Do you want to enter another card detail"); 
     System.out.println("Enter 1 for another card details and 0 for exit "); 
     int option = in.nextInt(); 
     if (!(option == 1) && (option == 0)) { 
      anotherCardDetail = true; 
      System.out.println("Exit from entering card details"); 
     } 

    } while (!anotherCardDetail); 

} 

     private static String encryptCardDetails(int cardNumber, int cardCVV,Date expiryDate, SecretKey key) 
     { 

    Cipher cipherencrypt = Cipher.getInstance("AES"); 

    byte[] plainCardNumber = String.valueOf(cardNumber).getBytes(); 
    byte[] plainCardCVV = String.valueOf(cardCVV).getBytes(); 
    byte[] plainExpiryDate = expiryDate.toString().getBytes(); 

    cipherencrypt.init(cipherencrypt.ENCRYPT_MODE, key); 
    byte[] encryptedCardNumber = cipherencrypt.doFinal(plainCardNumber); 
    byte[] encryptedCardCVV = cipherencrypt.doFinal(plainCardCVV); 
    byte[] encryptedExpiryDate = cipherencrypt.doFinal(plainExpiryDate); 

    String encryptedCard = Base64.encodeBase64String(encryptedCardNumber) 
      + Base64.encodeBase64String(encryptedCardCVV) 
      + Base64.encodeBase64String(encryptedExpiryDate); 

    return encryptedCard; 

} 


    public static String decryptCardDetails(File file, SecretKey key) { 

    byte[] finalString1 = null; 
    byte[] finalString2 = null; 
    byte[] finalString3 = null; 

    String final1 = null,final2 = null, final3 = null; 
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); 

    try{ 
    FileReader decryptFile = new FileReader(file); 
    BufferedReader buff = new BufferedReader(decryptFile); 

    String decryptcontent = buff.readLine(); 

    Scanner scan = new Scanner(decryptcontent).useDelimiter(","); 

    String cardType = scan.next(); 
    System.out.println(cardType); 
    String decryptString = scan.next(); 
    System.out.println(decryptString); 

    byte[] decrypt1 = Base64.decodeBase64(decryptString); 
    cipher.init(Cipher.DECRYPT_MODE, key); 

    byte[] decryptedText = cipher.doFinal(decrypt1); 

    final3 = new String(decryptedText, "UTF-8"); 

    decryptedCardNumber.toString()+decryptedCardCVV.toString()+decryptedExpiryDate.toString(); 

    }catch(IOException e) { 
     System.out.println("Decryption fails"); 
    } 

    return final3; 


} 

public static void main(String[] args) { 

    File cardDetailsFile = new File("/home/thamiz/workspace/cardFile.txt"); 

    try { 
     cardDetailsFile.createNewFile(); 

     FileWriter fileOut = new FileWriter(cardDetailsFile); 
     BufferedWriter buffer = new BufferedWriter(fileOut); 

     ClientCardDetails.storeCardDetails(); 

     AdminKeyGenerator.generateKey(); 


     String card = ClientCardDetails.encryptCardDetails(debitCardNumber, debitcardCVV, 
       debitcardExpiryDate, AdminKeyGenerator.generateKey()); 

     buffer.write(debitcardType); 
     buffer.write(","); 
     buffer.write(card); 
     buffer.newLine(); 
     buffer.flush(); 

     SecretKey key = AdminKeyGenerator.loadKey(); 
     String card1 = ClientCardDetails.decryptCardDetails(cardDetailsFile, key); 
     System.out.println("Decryption sucessful"); 
     System.out.println(card1); 


    } catch (ParseException e) { 
     System.out.println("Enter incorrect card details"); 
    } catch (IOException e) { 
     System.out.println("Encryption fails"); 
    } 

} 

上述程序給出以下輸出

主 SNIakr8Iguta7i0nqB + ​​QPG == VjmLfQj/YLUJUQnhJCYrgg == 7U1XrYRf8vqw + 5nUK8IgFFH8KqLsEKBXlIi9AnxVwzo = 解密SUCESSFUL ジ P H L

+0

'decrypt'方法返回什麼? –

回答

1

在「客戶端代碼」中,您要撥打AdminKeyGenerator.generateKey()兩次,並在AdminKeyGeneratormain()中撥打一次。每次通話都會返回一個新的唯一密鑰。

具體地說,

String card = ClientCardDetails.encryptCardDetails(
    debitCardNumber, 
    debitcardCVV, 
    debitcardExpiryDate, 
    AdminKeyGenerator.generateKey() 
); 

使用用於加密的新生成的密鑰。然後鑰匙被丟棄。沒有辦法再次解密結果。

確保你只產生一個關鍵,然後保存鍵(負載)的使用,對於每個加密和解密一個關鍵。

另外,您在一個地方有Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");,在另一個地方有Cipher cipherencrypt = Cipher.getInstance("AES");。使用完全相同的密碼配置進行加密和解密!