Я пытаюсь расшифровать запрос, зашифрованный с использованием алгоритма AES, с помощью секретного ключа AES, зашифрованного RSA. encryptedText - это текст, который мне нужно декодировать с помощью ключа encryptedAESKeyStringinString .
String encryptedText = "motnHul049AJSIo5RiOr7Njg3gV85LSR7DxxTjT6UlN_Vdi0q7hw9XDTUyiJ7XCtaObJDOWPwXGeT73JQKwN-GW6Pg6fkgyRlbmqNM23_DjqFQG7NqhhwdRfVJ4FfGnB1sZMcsSi0YwtKVOfTQu11coQm-LnWSW3o05-A4B7X4";
// Генерировать публичные c и личные ключи с использованием RSA
Key privateKey = getPrivate("KeyPair/key.jks");
System.out.println("Private key :" + privateKey);
Key publicKey = getPublic("KeyPair/key.jks");
System.out.println("Public key :" + publicKey);
// Зашифрованный секретный ключ AES с помощью RSA publi c key
String encryptedAESKeyStringinString = "SGvzL3jbTucB6mFSrGDHGI19OKuLe2u0miGnmb6EfgIHgiWqGfvGI5hd2U8-owYKThORbY6IZWyUsFKFc2CsegMKGkUBbnKHz-BUbrC5jNPHeW2LxNW_SLKZMmVciwWqHtcsQkUhGn1ZpEwgYp7NccP2qQJ1K5wY0G22ssoXsr6_gf4aMAafpP_EU8bV6yfOziaNWdOI0mkBshC6uYi2xibCBFWhP0HYouxEjuoLa7oXDSr1-Pol7S4tFibv8P92GlEqHuspjkBEk-crBVEMwPYZ5CjlVg0a8NXofvRYJIPOWHFDP03ALnKUmhGDv5pq7qbyhM-GwLFDncOU466VTw";
// Расшифровка ключа AES
String decryptedAESKeyString = decryptAESKey(encryptedAESKeyStringinString, privateKey);
System.out.println("Decrypted AES key with RSA "+decryptedAESKeyString);
// Теперь расшифровываем данные с помощью ключ дешифрованного AES!
String decryptedText = decryptTextUsingAES(encryptedText, decryptedAESKeyString);
System.out.println("decrypted: " + decryptedText);
// метод decryptTextUsingAES ()
public static String decryptTextUsingAES(String encryptedText, String aesKeyString) throws Exception {
//byte[] decodedKey = Base64.getUrlDecoder().decode(aesKeyString.getBytes("UTF-8"));
byte[] decodedKey = Base64.getUrlDecoder().decode(aesKeyString);
System.out.println("decodedKey->"+decodedKey);
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
//byte[] encPass = Base64.decodeBase64(aesKeyString.getBytes("UTF-8"));
//SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
//SecretKey aesKey = new SecretKeySpec(aesKeyString.getBytes("UTF-8"), "AES");
System.out.println("AES key :"+ originalKey);
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
aesCipher.init(Cipher.DECRYPT_MODE, originalKey);
byte[] bytePlainText = aesCipher.doFinal(Base64.getUrlDecoder().decode(encryptedText));
return new String(bytePlainText);
}
// метод decryptAESKey ()
private static String decryptAESKey(String encryptedAESKey, Key privateKey) throws Exception {
byte[] encBytes = Base64.getUrlDecoder().decode(encryptedAESKey.trim().getBytes("UTF-8"));
System.out.println("encBytes=="+encBytes);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(cipher.doFinal(encBytes));
}
// Getting publi c и закрытый ключ
public static Key getPrivate(String filename) throws Exception {
String password = "123456";
FileInputStream is = new FileInputStream(filename);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(is, password.toCharArray());
String alias = "rib_pub_priv_ob";
Key key = keystore.getKey(alias, password.toCharArray());
return key;
}
// https://docs.oracle.com/javase/8/docs/api/java/security/spec/X509EncodedKeySpec.html
public static Key getPublic(String filename) throws Exception {
String password = "123456";
FileInputStream is = new FileInputStream(filename);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(is, password.toCharArray());
Enumeration aliases = keystore.aliases();
//LOG.info("Loaded aliases..........."+aliases);
String aliasName = null;
if (aliases.hasMoreElements()) {
aliasName = (String)aliases.nextElement();
}
//String alias = "cib.icicibank.com";
String alias = "rib_pub_priv_ob";
Key key = keystore.getCertificate(alias).getPublicKey();
// Get certificate of public key
java.security.cert.Certificate cert = keystore.getCertificate(alias);
// Get public key
PublicKey publicKey = cert.getPublicKey();
//System.out.println("Publickey->"+publicKey);
return key;
}
// Вывод
Private key success
Private key :sun.security.rsa.RSAPrivateCrtKeyImpl@ffd2dcac
Public key success
Public key :Sun RSA public key, 2048 bits
modulus: 18213722380296769419176290383978262259711510121564750763778723301212945343757780419548592815669767026269432578044929215041238216136582555682984918038025714579636338218912893485917045613716952330026484856385185507103281153339648026845099146147505703575013284525178116844264542711761691005405946223414048773983620990969094260489552973361717030169183023097170355217065004855506605954167676904790917459796930427392967698905896378678786481959179236540862580781084688615759803403218899513555331743203995382479570969849672080854328076415494976227353539015464213594405750368178401939001834233806304211309666500156295537454433
public exponent: 65537
encBytes==[B@2d6d8735
Decrypted AES key with RSA Pf03zku5GNElJlOXAZkYRg6LbeZttkMKCdL
decodedKey->[B@de0a01f
AES key :javax.crypto.spec.SecretKeySpec@17c6b
Exception in thread "main" java.security.InvalidKeyException: Invalid AES key length: 26 bytes
at com.sun.crypto.provider.AESCrypt.init(AESCrypt.java:87)
at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:94)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:591)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:467)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
at javax.crypto.Cipher.implInit(Cipher.java:802)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1249)
at javax.crypto.Cipher.init(Cipher.java:1186)
at demo123.AESwithRSA.decryptTextUsingAES(AESwithRSA.java:144)
at demo123.AESwithRSA.main(AESwithRSA.java:79)