Recent Changes - Search:

Accueil

OpenSSL

SyncML

Apache Portable Runtime

Libxml2

Net-snmp

CUrl

Boost

Perl

ZLib

Samba

VPN

Serveurs de messagerie

edit

OpenSSL/Rsa-java-openssl

Pour chiffrer

C:\>openssl genrsa -out privateKey.pem 1024
Loading 'screen' into random state - done
Generating RSA private key, 1024 bit long modulus
...................++++++
....................++++++
e is 65537 (0x10001)

C:\>openssl rsa -in privateKey.pem -out publicKey.key -pubout -outform der
writing RSA key

C:\>openssl pkcs8 -in privateKey.pem -out privateKey.p8 -outform der -nocrypt -topk8

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;
        }


}

C:\>javac RSAUtilsCipher.java
C:\>java RSAUtilsCipher
input:
this is the input text
cipher:
Rk0Ihq0Ks6P46EIWrTCuL5SV1iq31Nnml7ovNAgKskQolvo7P35AVFMo6fDcvrguGg+aDOprCsjf
gZbvIstxRGl1xROHJc6IVu0tufSRrGv3H57hOyk/E4em6j3l5WV300GIL81ygek8csGRx3LGZyt1
4kp09p1uSoPkLtEwomU=

decrypt:
this is the input text

Pour Signer

import 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;
        }
}
 

C:\>javac RSAUtilsSign.java
C:\>java RSAUtilsSign
input:
this is the input text
Signature:
l/mQPS04XmwMtqaBr0d46MJYMIDHUPeRzl8uXdDgrf9aXubFKu+UwlqES0NGs9QgrNS32RGtolUB
BN3LZtTshyrsi7RjY0bStrLdn0fLNVk14NONjGJio6NM5dPeJCNeW4u1QtPH9YnHznd2kR6LOQuZ
MowGQhvclB/WK68d4XY=

check:
true

Edit - History - Print - Recent Changes - Search
Page last modified on June 01, 2010, at 02:43 PM