0

我不会用资产文件中的 .cer 文件加密我的字符串值。我找到了 c# 的代码。但是对于 Android,我该怎么做呢?

public string GetEncryptValue(string value)
{

     X509Certificate2 x509_2 = w X509Certificate2(C:/publicCer.cer);//for android asset file

     var encryptedValue = Encrypt(x509_2, value);

     return encryptedValue;
}

public static string Encrypt(X509Certificate2 x509, string stringToEncrypt)
{

     if (x509 == null || string.IsNullOrEmpty(stringToEncrypt))
                throw new Exception("A x509 certificate and string for encryption must be provided");
            RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509.PublicKey.Key;

     byte[] bytestoEncrypt = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt);

     byte[] encryptedBytes = rsa.Encrypt(bytestoEncrypt, false);

     return Convert.ToBase64String(encryptedBytes);
}
4

1 回答 1

2
KeyPairGenerator kpg;

KeyPair kp;

static PublicKey publicKey;

static PrivateKey privateKey;

byte [] encryptedBytes,decryptedBytes;

Cipher cipher;

public String GetEncryptValue(String value) 
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, 
       IllegalBlockSizeException, BadPaddingException 
{
    InputStream is = null;
    javax.security.cert.X509Certificate x5092 = null;
    try {
        is = getAssets().open("publicCer.cer");
         x5092 = javax.security.cert.X509Certificate.getInstance(is);
    } catch (javax.security.cert.CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return RSAEncrypt("Gergova", x5092);
}

public String RSAEncrypt (
    final String plain, javax.security.cert.X509Certificate x5092) 
        throws NoSuchAlgorithmException, NoSuchPaddingException, 
           InvalidKeyException, IllegalBlockSizeException, 
           BadPaddingException 
{
    kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(1024);
    kp = kpg.genKeyPair();
    publicKey = x5092.getPublicKey();
    privateKey = kp.getPrivate();

    cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    encryptedBytes = cipher.doFinal(stringToBytesASCII(plain));
    String str = null;
    try {
        str = new String(encryptedBytes, "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return str;
}

public static byte[] stringToBytesASCII(String str) {

     byte[] b = new byte[str.length()];
     for (int i = 0; i < b.length; i++) {
      b[i] = (byte) str.charAt(i);
     }
     return b;
}
于 2014-05-23T12:17:43.823 回答