خرید بک لینک

Vote count: 0

I found this source code for RSA encryption/decryption but i face a problem when i try to change 512 bit to 1024 bit. Can anyone help me which code i have to change to make sure it will run on 1024 bit. Please hep me.. tq

    public class RSA2 {

    private  Cipher cipher;

    // RSA keys will be generated when the client and server coect
    private PrivateKey myPrivateKey;
    private byte[] myPublicKey;
    private byte[] interlocutorPublicKey = null;

    // Strings are encoded / decoded in BASE64
    private BASE64Decoder b64decoder = new BASE64Decoder();
    private BASE64Encoder b64encoder = new BASE64Encoder();

    public RSA2() {
    try {

        cipher = Cipher.getInstance("RSA");
        Security.addProvider(new BouncyCastleProvider());

    } catch (Exception ex) {
        Logger.getLogger(RSA2.class.getName()).log(Level.SEVERE, null, ex);
    } 
    }

    // Generate the pair of public and private keys using 512 bytes
    public KeyPair generateKey() 
    {
    KeyPair keyPair = null;

    try 
    {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(512);
        keyPair = keyGen.generateKeyPair();

        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        myPublicKey = publicKey.getEncoded();
        setMyPrivateKey(privateKey);

    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(RSA2.class.getName()).log(Level.SEVERE, null, ex);
    }

    retu keyPair;
    }

    // Encrypts text using public key
    public String encrypt(String text, PublicKey key) throws Exception {
    cipher.init(Cipher.ENCRYPT_MODE, key);
    String cipherText = new String(cipher.doFinal(b64encoder.encode(text.getBytes()).getBytes()));
    BigInteger cipherTextBigInteger = new BigInteger(cipherText.getBytes());
    retu cipherTextBigInteger.toString();
    }

    // Use the public key to encrypt the interlocutor
    public String encrypt(String text) throws Exception {
    retu encrypt(text, bytesToPublicKey(interlocutorPublicKey));
    }

    // Decrypts text using private key
    public String decrypt(String encrypted64BigIntStr, PrivateKey key) throws Exception {
    BigInteger encrypted64BigInt = new BigInteger(encrypted64BigIntStr);
    String encrypted64Str = new String(encrypted64BigInt.toByteArray());
    cipher.init(Cipher.DECRYPT_MODE, key);
    String dectyptedText = new String(cipher.doFinal(encrypted64Str.getBytes()));               //<---- error bila buat 1024bit
    retu new String(b64decoder.decodeBuffer(dectyptedText));
    }

    // Use my private key to decrypt
    public String decrypt(String text) throws Exception {
    retu decrypt(text, myPrivateKey);
    }

    // Public Key the caller is sent in byte [ ] and converted into a PublicKey object
    public static PublicKey bytesToPublicKey(byte[] publicKeybytes) {
    PublicKey publicKey = null;
    try {

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeybytes);
        publicKey = keyFactory.generatePublic(publicKeySpec);

    } catch (InvalidKeySpecException ex) {
        Logger.getLogger(RSA2.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(RSA2.class.getName()).log(Level.SEVERE, null, ex);
    }

    retu publicKey;
    }

    // Test
    public static void main(String[] args) {
    try {

        RSA2 crypto = new RSA2();
        KeyPair keyPair = crypto.generateKey();

        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        byte[] publicKeyBytes = publicKey.getEncoded();
        byte[] privateKeyBytes = privateKey.getEncoded();

        System.out.println("Public key: " + new BigInteger(publicKeyBytes));
        System.out.println("Private key: " + new BigInteger(privateKeyBytes));

        String testeMsg = "As a seed knows how to make a flower ?";
        System.out.println("nText: " + testeMsg);

        String encrypted = crypto.encrypt(testeMsg, bytesToPublicKey(publicKeyBytes));
        System.out.println("Encrypted: " + encrypted);

        String decrypted = crypto.decrypt(encrypted, keyPair.getPrivate());             //<---- error bila buat 1024bit
        System.out.println("Decrypted: " + decrypted);

    } catch (Exception ex) {
        Logger.getLogger(RSA2.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    public byte[] getMyPublicKey() {
    retu myPublicKey;
    }

    public void setMyPublicKey(byte[] myPublicKey) {
    this.myPublicKey = myPublicKey;
    }

    public PrivateKey getMyPrivateKey() {
    retu myPrivateKey;
    }

    public byte[] getInterlocutorPublicKey() {
    retu interlocutorPublicKey;
    }

    public boolean hasInterlocutorPublicKey() {
    retu interlocutorPublicKey!=null;
    }

    public void setInterlocutorPublicKey(byte[] interlocutorPublicKey) {
    this.interlocutorPublicKey = interlocutorPublicKey;
    }

    public void setMyPrivateKey(PrivateKey aMyPrivateKey) {
    myPrivateKey = aMyPrivateKey;
    }
    }

I try to change 512bit to 1024bit but have error

    keyGen.initialize(512);   //<--- keyGen.initialize(1024);

this is the error for code:

    String dectyptedText = new String(cipher.doFinal(encrypted64Str.getBytes())); //<--- error in line 85

    String decrypted = crypto.decrypt(encrypted, keyPair.getPrivate());  //<-- error in line 134

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 5 خرداد 1395 ساعت: 2:07

صفحه بندی