Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:37:57+00:00 2026-06-02T22:37:57+00:00

I am trying to decrypt a String, that I have encrypted elsewhere. Here is

  • 0

I am trying to decrypt a String, that I have encrypted elsewhere. Here is my code:

private void test() {

    try {
        String stringMessage="Sf3O7Lr2+WN5szGyLejL3CjuBRZtQ72+ZBmgVTgWnatQZxUElzaBqFa1p0SVBqe9VWVxCxdEkejMVtDGEr0UJSVSK8EB/fPI6v8JE8dIu0JN0mMs4xlowhITy0tQR+1pcBtDFjzOl33xxQcq5JuPezxRDxFIp+IVkD8FdpqlttEKf2Tvqw9tqsdgiBKb5xDvKrkIDQXdLBh1gbAVZDSJYGHRkcOA8vz2ty/PeooKkfDK6IOn7KBwOBgSRgQr/MLBF3Xk2vRWgVGRh/fRkzu21EWo99Q5moWKxWl3HW/bbgTBQTb097XP3NTID9kSPhCfL0BEfBxonuNse5GBoeRnCw==";
        //Convert String back to Byte[] and decrpt
        byte[] byteMessage = Base64.decodeBase64(stringMessage.getBytes("UTF-8"));
        System.out.println("ENCRYPTED MESSAGE byte Length: "+byteMessage.length);

        String decryptedMsg = decryptString(byteMessage, loadCASPrivateKey());
        System.out.println(decryptedMsg);
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
}

private static String decryptString(byte[] message, Key privateKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    byte[] cipherData = cipher.doFinal(message);
    return new String(cipherData, "UTF-8");
}

private PrivateKey loadCASPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    InputStream is = getClass().getResourceAsStream( "/keys/app-private.key" );
    if (is == null) {
        System.out.println("NULL");
    }
    byte[] encodedPrivateKey = new byte[(int) 2000];
    is.read(encodedPrivateKey);
    is.close();

    // Generate KeyPair.
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

    return privateKey;

}

This works 100% how I would like it to under my desktop JVM, however when I run it in the Android emulator I get:

04-24 22:42:21.011: I/System.out(1041):
��k���_��*�ݣ���93|@0�̍4�y)��Q�k�;*A����e�#��A�
�oiu:�����W5@$�w�j��
uS�R�Ocxٰ&����l
�w’/�d�8uA��ؔ�{�4$�U�0��{Ԑ��t!9��n��
��a��’Jdt2�t�T�D��k+k�;������ ����GF��\�rڼ��>]�y+^w�<��
��’E{�8R]�ZHyu��ζ��軟�ށ掱�{�A�#ȟ�

I assume my problem is down to encoding, but I’ve spent all day trying to work out what and am really stumped.

The string is originally encrypted using:

private void test() {
    String message="22223334490384903432221";
try {
    //Encrypt message
    byte[] encryptedMsg = Base64.encodeBase64(encryptString(message, temp.loadCASPublicKey()));
} catch (Exception e) {
e.printStackTrace();
        return;
}

}

private static byte[] encryptString(String message, Key publicKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    byte[] cipherData = cipher.doFinal(message.getBytes("UTF-8"));     
    return cipherData;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:37:59+00:00Added an answer on June 2, 2026 at 10:37 pm

    Thanks to GregS’s help in the comments. This is the solution that worked for me.

    private void test() {
    
                try {
                    String stringMessage="GEQRpAPA577ks/QveudNkk7H9DjItKGLDYW6xhH1YJGabCVzrkejkBh6S+APwEXxB84UV/q0sO5rqkgXWONJQ8CoMTfqXtUkAAwkYHSc86eGewkM8WpctA0AyNVFonOxDCXm84Uq8JRMzqskSH5VXHmMxvHIvpFgdhmt9Ir0cKWzoLsuvgfY9hfypfEyBXGZcoptQeKhsZxRGIlxbXhrFl/LqhC+F6vYtZ/j5pv2LUP38wh2rTCKnAQ+xvC+7wn5SVzt/Wbr/q7GjCoJuU9uFHQSS49KQDt+BzJL2XNwAMmdbC+XHYkEBBWxVSS+0hdSQxoaKVZZJk4hTnHwQlBAkw==";
                    //Convert String back to Byte[] and decrpt
                    byte[] byteMessage = Base64.decodeBase64(stringMessage.getBytes("UTF-8"));
                    System.out.println("ENCRYPTED MESSAGE byte Length: "+byteMessage.length);
    
                    String decryptedMsg = decryptString(byteMessage, loadCASPrivateKey());
                    System.out.println(decryptedMsg);
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
            }
    
            private static String decryptString(byte[] message, Key privateKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchProviderException {
                Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    
                Cipher cipher = Cipher.getInstance("RSA/None/NoPadding","BC");
                cipher.init(Cipher.DECRYPT_MODE, privateKey);
    
                byte[] cipherData = cipher.doFinal(message);
                return new String(cipherData, "UTF-8");
            }
    
            private PrivateKey loadCASPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
                InputStream is = getClass().getResourceAsStream( "/keys/app-private.key" );
                if (is == null) {
                    System.out.println("NULL");
                }
                byte[] encodedPrivateKey = new byte[(int) 1216];
                is.read(encodedPrivateKey);
                is.close();
    
                // Generate KeyPair.
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    
                PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
                PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
    
                return privateKey;
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to decrypt in JAVA a string that was encrypted in C#.
I am trying to decrypt data that has been encrypted previously in RSA (don't
I'm getting literally crazy trying to decrypt in JavaScript a string encrypted in PHP
I'm trying to sign a message using a private key that is encrypted, I
I'm trying to decrypt a variable that I've encrypted from my web server and
I've got a weird issue while trying to decrypt an encrypted text file. Basically
I have a directory and in it has several files. I'm trying to decrypt
I am trying encrypt and decrypt one column in table. My code is like
Trying to write a couple of functions that will encrypt or decrypt a file
I am trying to encrypt data that a serializable (xml) class contains. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.