如何使用RSA算法为android中的sms提供安全性

时间:2013-02-24 18:14:08

标签: android rsa encryption

我想通过使用一个密钥(公共)进行加密来发送加密的Sms数据,并使用不同的私钥来解密这个短信...具体来说我想使用RSA算法场景......

所以你能告诉我如何使用android中的不同键加密和解密数据......

我使用DES算法,但它使用相同的密钥......

任何人都可以建议如何做到这一点....我的空白值......

`

public String encrypt(String smsbody) {
    try {

        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");

        kpg.initialize(512);// initialize key pairs to 512 bits ,you can
                            // also take 1024 or 2048 bits

        KeyPair kp = kpg.genKeyPair();

        PublicKey publi = kp.getPublic();
        System.out.println(publi.serialVersionUID);
        Cipher cipher = Cipher.getInstance("RSA");

        cipher.init(Cipher.ENCRYPT_MODE, publi);

        byte[] src = smsbody.getBytes();// converting source data into byte
                                        // array

        byte[] cipherData = cipher.doFinal(src);// use this method to
                                                // finally encrypt data

        String srco = new String(cipherData);// converting byte array into
                                                // string
        // System.out.println();
        // System.out.println("Encrypted data is:-" + srco);

        return srco;
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return "";
}

public PrivateKey Privatekey() {

    try {
        kpg = KeyPairGenerator.getInstance("RSA");

        kpg.initialize(512);// initialize key pairs to 512 bits ,you can
                            // also take 1024 or 2048 bits

        KeyPair kp = kpg.genKeyPair();
        privatei = kp.getPrivate();// Generating private key
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return privatei;
}

public String decrypt(String smsbody, PrivateKey privatei) {
    try {

        Cipher cipheri = Cipher.getInstance("RSA");

        cipheri.init(Cipher.DECRYPT_MODE, privatei);// Setting to
                                                    // decrypt_mode


        System.out.println(smsbody);

        byte[] cipherDat = cipheri.doFinal(smsbody.getBytes());// Finally
                                                                // decrypting
        // data
        System.out.println(cipherDat);
        String decryptdata = new String(cipherDat);


        return decryptdata;
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return "";
} `

我正在尝试以下代码但是当我使用decrypt时它会给出

提前致谢...

0 个答案:

没有答案