Click here to Skip to main content
15,888,271 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What can be achieved to this encrypted format?

My android app send encrypted messages to the c# server but server want encrypted messages actually like this unknown formatchinese words.

筊맲躥慢૬䣍毤_箬첑�沆ň䇷㨡鬩

But android encrypt messages like this format (on7vQhgNeVDVDu4evL0HZ5UbC2C1oZdamfU9XBLGZQZ13MLQKu2speIWNaldsfcGfPS)

I use RSA algorithm with same public/private keys in c sharp and android. where am i interrupted?

Something wrong with this approach with this unknown format.

Its seems android have problem with class of encryption I don't know what else need to use for this issue

Thank you for advice

Android code
Java
public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acti);
    try {
        generateKey();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

public  static void generateKey() throws Exception {



    String modulusString = 
"tx94IV9NAutFU1HQjXmkLzknJ5vatO
FyhD90Un3u5oiOc4
e9fT1bsM0af3OqNMCTRLPuQJ2JQokY+3T0icJqHgG/aHvbmvDvRKn2QrVxAFt8EN6
jp/S6+dRe1B/6eJbVRJJpeekLslqGqdQgr+5ocD+ZPjiE2iL6sGGyAYz+lOJtSr9N4ZcD
4kNikI3J9kZDNO78rEqQuX7flh0RS79N63MJ9xX9fBuqHFIud3KKKbqHiA
SQoaU1rWqZ2VIdqfXzreZMYHpHYioVzyrbk
/wdQQV2ibmJFAsa5aiKSP+g9rF4xYoPAistePDwn4O
+wARGlMsu7RYVAIeUM77l+w6ugw==";
    String ExponentString = "AQAB";
    byte[] modulusBytes = Base64.decode(modulusString.getBytes("UTF-8"), Base64.DEFAULT);
    byte[] exponentBytes = Base64.decode(ExponentString.getBytes("UTF-8"),Base64.DEFAULT);
    BigInteger modulus = new BigInteger(1, modulusBytes);
    BigInteger publicExponent = new BigInteger(1, exponentBytes);


    KeyFactory fact = KeyFactory.getInstance("RSA");
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
    String INPUT = "GAVDOOL";

    RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, publicExponent);
    PublicKey pubKey = fact.generatePublic(rsaPubKey);
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);

  //  byte[] plainBytes = clearTextPassword.getBytes();
    byte[] cipherData = cipher.doFinal(INPUT.getBytes());
    String encryptedStringBase64 = Base64.encodeToString(cipherData, Base64.DEFAULT);
    System.out.println("Encrypted?????"+encryptedStringBase64);
    System.out.println(encryptedStringBase64.length());

          }
           }
Posted
Updated 9-Sep-15 4:17am
v2
Comments
Sergey Alexandrovich Kryukov 9-Sep-15 10:12am    
RSA has nothing to do with data "formats"; it always deals with arrays of bytes. It's up to you how you serialize and deserialize your data.
—SA
Member 10693183 9-Sep-15 10:26am    
I think my server need to be remove Encoding.Unicode format and then get encrypted message, and android not necessary for changing code,
What are in your mind?
Sergey Alexandrovich Kryukov 9-Sep-15 10:30am    
It's not related to encryption. Just use some consistence encoding and format on both sides. The problem is way too trivial. Avoid using "Encoding.Unicode", which is not "Unicode" at all, but is the particular Unicode encoding UTF-16LE. Why not using UTF-8, which also supports all Unicode sub-sets and is the Web de-facto standard?
—SA
Member 10693183 9-Sep-15 10:40am    
I try using UTF-8 in android but had the same results,just my c# server use Encoding. Is it better remove that or not?
And second question, Are you sure android code is correct that i suspected to that?
Sergey Alexandrovich Kryukov 9-Sep-15 10:55am    
Use all formats/encodings consistently! Note that "Unicode" may mean be UTF-16LE on one system and UTF-16BE on another (do you know what is that?) Use system-independent UTF-8.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900