如何在Eclipse中导入Android类

huangapple 未分类评论49阅读模式
英文:

how to import android class in eclipse

问题

好的,以下是翻译好的内容:

Good Evening
我正在尝试将名为 "RSA.java" 的 Android 类导入到 Eclipse 中
但在以下部分出现错误:```decode(key, Base64.DEFAULT)encodeToString(key, Base64.DEFAULT)``` 错误为 "DEFAULT 无法解析或不是字段"以及 ```Base64.decode(MODULUS, 0) 和 Base64.decode(EXPONENT, 0)``` 错误为 "decode(String, int) 方法对于 Base64 类型未定义"

以下是您提供的代码片段

```java
public static byte[] decryptBASE64(String key) throws Exception {
    return Base64.decode(key, Base64.DEFAULT);
}

public static String encryptBASE64(byte[] key) throws Exception {
    return Base64.encodeToString(key, Base64.DEFAULT);
}
public static PublicKey getPublicKey(String MODULUS, String EXPONENT) throws Exception {
    byte[] modulusBytes = Base64.decode(MODULUS, 0);
    byte[] exponentBytes = Base64.decode(EXPONENT, 0);

我将您提供的 Android 类导入到了 Eclipse:

package com.questdot.rsaexample;

import java.util.Base64;

import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

public class RSA {
    public static final String KEY_ALGORITHM = "RSA";
    public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";

    private static final String PUBLIC_KEY = "RSAPublicKey";
    private static final String PRIVATE_KEY = "RSAPrivateKey";

    public static byte[] decryptBASE64(String key) throws Exception {
        return Base64.decode(key, Base64.DEFAULT);
    }

    public static String encryptBASE64(byte[] key) throws Exception {
        return Base64.encodeToString(key, Base64.DEFAULT);
    }

    // ... (其余部分未翻译)
}

有什么建议吗?
谢谢。


<details>
<summary>英文:</summary>

Good Evening
I am trying to import the android class by name&quot;RSA.java&quot; into Eclipse
but I have errors in these sections in ```decode(key, Base64.DEFAULT) and encodeToString(key, Base64.DEFAULT) ``` error is &quot;DEFAULT cannot be resolved or is not a field&quot; and ```Base64.decode(MODULUS,0) and Base64.decode(EXPONENT,0) ```
error is &quot;The method decode(String, int) is undefined for the type Base64&quot;

public static byte[] decryptBASE64(String key) throws Exception {
return Base64.decode(key, Base64.DEFAULT);
}

public static String encryptBASE64(byte[] key) throws Exception {
    return Base64.encodeToString(key, Base64.DEFAULT);
}

public static PublicKey getPublicKey(String MODULUS,String EXPONENT) throws Exception{
byte[] modulusBytes = Base64.decode(MODULUS,0);
byte[] exponentBytes = Base64.decode(EXPONENT,0);


My Android class imported into the Eclipse

package com.questdot.rsaexample;

import java.util.Base64;

import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

public class RSA {
public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";

private static final String PUBLIC_KEY = &quot;RSAPublicKey&quot;;
private static final String PRIVATE_KEY = &quot;RSAPrivateKey&quot;;

public static byte[] decryptBASE64(String key) throws Exception {
    return Base64.decode(key, Base64.DEFAULT);
}

public static String encryptBASE64(byte[] key) throws Exception {
    return Base64.encodeToString(key, Base64.DEFAULT);
}


public static String sign(byte[] data, String privateKey) throws Exception {

    byte[] keyBytes = decryptBASE64(privateKey);

    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);

    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initSign(priKey);
    signature.update(data);

    return encryptBASE64(signature.sign());
}


public static boolean verify(byte[] data, String publicKey, String sign)
        throws Exception {

    byte[] keyBytes = decryptBASE64(publicKey);

    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    PublicKey pubKey = keyFactory.generatePublic(keySpec);

    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(pubKey);
    signature.update(data);

    return signature.verify(decryptBASE64(sign));
}


public static byte[] decryptByPrivateKey(byte[] data, String key)
        throws Exception {

    byte[] keyBytes = decryptBASE64(key);


    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    return cipher.doFinal(data);
}


public static byte[] decryptByPublicKey(byte[] data, String key)
        throws Exception {

    byte[] keyBytes = decryptBASE64(key);

    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicKey = keyFactory.generatePublic(x509KeySpec);

    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, publicKey);

    return cipher.doFinal(data);
}


public static byte[] encryptByPublicKey(byte[] data, String key)
        throws Exception {

    byte[] keyBytes = decryptBASE64(key);

    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicKey = keyFactory.generatePublic(x509KeySpec);

    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    return cipher.doFinal(data);
}


public static byte[] encryptByPrivateKey(byte[] data, String key)
        throws Exception {

    byte[] keyBytes = decryptBASE64(key);

    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);

    return cipher.doFinal(data);
}

public static String getPrivateKey(Map&lt;String, Object&gt; keyMap)
        throws Exception {
    Key key = (Key) keyMap.get(PRIVATE_KEY);

    return encryptBASE64(key.getEncoded());
}

public static String getPublicKey(Map&lt;String, Object&gt; keyMap)
        throws Exception {
    Key key = (Key) keyMap.get(PUBLIC_KEY);

    return encryptBASE64(key.getEncoded());
}


public static Map&lt;String, Object&gt; initKey() throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator
            .getInstance(KEY_ALGORITHM);
    keyPairGen.initialize(1024);

    KeyPair keyPair = keyPairGen.generateKeyPair();

    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

    Map&lt;String, Object&gt; keyMap = new HashMap&lt;String, Object&gt;(2);

    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}

public static PublicKey getPublicKey(String MODULUS,String EXPONENT) throws Exception{
    byte[] modulusBytes = Base64.decode(MODULUS,0);
    byte[] exponentBytes = Base64.decode(EXPONENT,0);

    BigInteger modulus = new BigInteger(1, (modulusBytes) );
    BigInteger exponent = new BigInteger(1, (exponentBytes));

    RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
    KeyFactory kf = KeyFactory.getInstance(RSA.KEY_ALGORITHM);
    return kf.generatePublic(spec);
}

public static byte[] encrypt(Key publicKey, String s) throws Exception{
    byte[] byteData = s.getBytes();
    Cipher cipher = Cipher.getInstance(&quot;RSA/None/PKCS1Padding&quot;);
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] encryptedData = cipher.doFinal(byteData);


    return encryptedData;
}

}


Any suggestions?
Thanks.

</details>


huangapple
  • 本文由 发表于 2020年7月26日 16:45:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/63098010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定