OpenSSL VPN Serveurs de messagerie |
OpenSSL/Rsa-java-opensslPour chiffrer
Les fichiers privateKey.p8 et publicKey.key transformés en Base64 deviennent respectivement les variables PVK et PUB du code ci-dessous. import java.security.*; import java.security.spec.*; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; public final class RSAUtilsCipher { static public String PVK= "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ+kB8MpkFt6YO84"+ "ZEu7ggnk6ECOhzeDL6rqg421nGqrzx37NeyhSZ/n0x+II91GBNMCuKH/x5VToPUG"+ "VG4i43ViBbEJFzsAyt0IGzU+fJkEkmTV/B6N9lPBwZvoyQPdnpRprn0PtbME4zMp"+ "sPIb/tFmrVRIyZKkl6zDvIB3acflAgMBAAECgYBYXd7iG/7JdEt1/7wc47VOrsQV"+ "ABA/HUHklR2u8ombznC+gtDNpFXxysxl1uErWdfPyeDT45bhxMIC/7b6pBtNfbhM"+ "Ig1Ej5ipD2Vr6+L5llHJZnYBFskFeCDcwdpV2/jlcz7h72YS5fOtcuUkA7UZ3Orz"+ "uiCY3Z3Qd5HhnRaGwQJBANHEZwWCilWHvfAPj1jr7tMF3fP/pJyoHDdwkj9YgCR5"+ "Hvhsx7b0UekTXUPMBUrFMORintSjKyx7ZUuu3ig4wnUCQQDC012+DBZJvha+zTii"+ "SUK5d7J4j8iOlkWzx7EZ015QGa9dHTr+YsJt0KTtQBXqz/J7EvLIrmkBjRwmyqZC"+ "6WGxAkA7p81cpclNlbMQqHwt37slzE9Y6uUXoD+WDK+q0DJxLPj/0X/iqgz5agZm"+ "F4mRwA6thNaCxnyqw9UE9xA7BBlZAkEAqXiTIq4A9USVJIP/r6tqj0q/xH/ORboS"+ "B8C9HWnvsF2nXaf/mSjvV/cWgAovBoK8urJQ3qBYVzEYO+YrmijJAQJAQrQBk7Bm"+ "mGWza8dn1SNATtmNXnSHOk1vWBQlnCZDla9BfpGLOhACRUttoTJObmrTdmRyr+h+"+ "MJpMBgE3IYK5Hg=="; static public String PUB= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCfpAfDKZBbemDvOGRLu4IJ5OhA"+ "joc3gy+q6oONtZxqq88d+zXsoUmf59MfiCPdRgTTArih/8eVU6D1BlRuIuN1YgWx"+ "CRc7AMrdCBs1PnyZBJJk1fwejfZTwcGb6MkD3Z6Uaa59D7WzBOMzKbDyG/7RZq1U"+ "SMmSpJesw7yAd2nH5QIDAQAB"; public static void main(String[] args) { String TextStream = "this is the input text"; byte[] Cipher; Base64 base64 = new Base64(); System.out.println("input:\n" + TextStream); Cipher = encrypt(TextStream); System.out.println("cipher:\n" + base64.encodeToString(Cipher)); System.out.println("decrypt:\n" + decrypt(Cipher)); } private static byte[] encrypt(String Buffer) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decodeBase64(PUB)); Key encryptionKey = keyFactory.generatePublic(pubSpec); //PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(PVK)); //Key encryptionKey = keyFactory.generatePrivate(privSpec); Cipher rsa; rsa = Cipher.getInstance("RSA"); rsa.init(Cipher.ENCRYPT_MODE, encryptionKey); return rsa.doFinal(Buffer.getBytes()); } catch (Exception e) { e.printStackTrace(); } return null; } private static String decrypt(byte[] buffer) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(PVK)); Key decryptionKey = keyFactory.generatePrivate(privSpec); //X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decodeBase64(PUB)); //Key decryptionKey = keyFactory.generatePublic(pubSpec); Cipher rsa; rsa = Cipher.getInstance("RSA"); rsa.init(Cipher.DECRYPT_MODE, decryptionKey); byte[] utf8 = rsa.doFinal(buffer); return new String(utf8, "UTF8"); } catch (Exception e) { e.printStackTrace(); } return null; } }
Pour Signerimport java.security.*; import java.security.spec.*; import org.apache.commons.codec.binary.Base64; public final class RSAUtilsSign { static public String PVK= "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ+kB8MpkFt6YO84"+ "ZEu7ggnk6ECOhzeDL6rqg421nGqrzx37NeyhSZ/n0x+II91GBNMCuKH/x5VToPUG"+ "VG4i43ViBbEJFzsAyt0IGzU+fJkEkmTV/B6N9lPBwZvoyQPdnpRprn0PtbME4zMp"+ "sPIb/tFmrVRIyZKkl6zDvIB3acflAgMBAAECgYBYXd7iG/7JdEt1/7wc47VOrsQV"+ "ABA/HUHklR2u8ombznC+gtDNpFXxysxl1uErWdfPyeDT45bhxMIC/7b6pBtNfbhM"+ "Ig1Ej5ipD2Vr6+L5llHJZnYBFskFeCDcwdpV2/jlcz7h72YS5fOtcuUkA7UZ3Orz"+ "uiCY3Z3Qd5HhnRaGwQJBANHEZwWCilWHvfAPj1jr7tMF3fP/pJyoHDdwkj9YgCR5"+ "Hvhsx7b0UekTXUPMBUrFMORintSjKyx7ZUuu3ig4wnUCQQDC012+DBZJvha+zTii"+ "SUK5d7J4j8iOlkWzx7EZ015QGa9dHTr+YsJt0KTtQBXqz/J7EvLIrmkBjRwmyqZC"+ "6WGxAkA7p81cpclNlbMQqHwt37slzE9Y6uUXoD+WDK+q0DJxLPj/0X/iqgz5agZm"+ "F4mRwA6thNaCxnyqw9UE9xA7BBlZAkEAqXiTIq4A9USVJIP/r6tqj0q/xH/ORboS"+ "B8C9HWnvsF2nXaf/mSjvV/cWgAovBoK8urJQ3qBYVzEYO+YrmijJAQJAQrQBk7Bm"+ "mGWza8dn1SNATtmNXnSHOk1vWBQlnCZDla9BfpGLOhACRUttoTJObmrTdmRyr+h+"+ "MJpMBgE3IYK5Hg=="; static public String PUB= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCfpAfDKZBbemDvOGRLu4IJ5OhA"+ "joc3gy+q6oONtZxqq88d+zXsoUmf59MfiCPdRgTTArih/8eVU6D1BlRuIuN1YgWx"+ "CRc7AMrdCBs1PnyZBJJk1fwejfZTwcGb6MkD3Z6Uaa59D7WzBOMzKbDyG/7RZq1U"+ "SMmSpJesw7yAd2nH5QIDAQAB"; public static void main(String[] args) { System.out.println("input:\n" + TextStream); boolean check; Base64 base64 = new Base64(); System.out.println("input: " + TextStream); byte[] Sign = sign(TextStream); System.out.println("Signature:\n" + base64.encodeToString(Sign)); check = verify(TextStream,Sign); System.out.println("check:\n" + check); } private static byte[] sign(String Buffer) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(PVK)); PrivateKey encryptionKey = keyFactory.generatePrivate(privSpec); Signature instance = Signature.getInstance("SHA1withRSA"); instance.initSign(encryptionKey); instance.update((Buffer).getBytes()); byte[] signature = instance.sign(); return signature; } catch (Exception e) { e.printStackTrace(); } return null; } private static boolean verify(String buffer,byte[] passwordCrypte) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decodeBase64(PUB)); PublicKey decryptionKey = keyFactory.generatePublic(pubSpec); Signature instance = Signature.getInstance("SHA1withRSA"); instance.initVerify(decryptionKey); instance.update(buffer.getBytes()); return instance.verify(passwordCrypte) ? true : false; } catch (Exception e) { e.printStackTrace(); } return false; } }
|