java - android非对称加密代码错误

标签 java android encryption rsa

当我将字符串转换为获取字节时,它会生成错误 这是

    String f= "engineer hussein mawzi hello world";

    KeyPairGenerator kpg;
    KeyPair kp;
    PublicKey publicKey;
    PrivateKey privateKey;
    byte [] encryptedBytes1,decryptedBytes1;

    Cipher cipher,cipher1;   

          String encrypted1,decrypted1;  

    kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(512);
        kp = kpg.genKeyPair();
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();

          System.out.println("th"+publicKey);
             cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);




                    encryptedBytes1= cipher.doFinal(f.getBytes());

 encrypted1 = new String(encryptedBytes1);


// here is the message that i wnt to encrypte and send !!!!
                    System.out.println("here is mu test app"+encrypted1);


             System.out.println("EEncrypted?????"+encrypted1.length());
      cipher1 = Cipher.getInstance("RSA");
    cipher1.init(Cipher.DECRYPT_MODE, privateKey);

// here i want to recover the byte array of the message i extract it and decrypte it 
 byte[] by = encrypted1.getBytes();
    System.out.println(by.length);
    decryptedBytes1 = cipher1.doFinal(by);
    decrypted1 = new String(decryptedBytes1);
    System.out.println("DDecrypted?????" + decrypted1);

   System.out.println("DDecrypted?????" + decrypted1.length());
         }

最佳答案

您的问题可能与从 byte[]String 的转换有关。并非所有字节都映射到字符,那些未映射的字节会被悄悄删除。仅使用 byte[] 或执行例如Base 64 编码/解码。

关于java - android非对称加密代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23228030/

相关文章:

android - 每个列表项中的两个 View

java - 如何在android中从 'outside'强制更新主要 Activity ?

android - SQLiteOpenHelper onUpgrade() 混淆Android

linux - 是否可以在无需手动输入(密码)的情况下在自启动设备上实现完整的设备加密?

java - 无法创建 PoolableConnectionFactory (未知数据库 'XYZ_DEV' )

java - 如何在 IntelliJ Idea 的项目树中立即看到编译错误?

java - Android Studio 无法构建 gradle

java - 从现有 ArrayList 创建 ArrayList

c# - 如何使用 ConvertTo-SecureString

c# - 如何将私钥导入 c# RSAParameters 对象?