RSA在java和go之间加密和解密

时间:2015-05-31 03:52:01

标签: java encryption go rsa

在java中我通过RSA加密字符串:“你好,我是明文字符串!@sina.com” 然后得到:

kkkHf5QSXx8aDadk66AOysmV8LOi4vWUANal+7KV6va/5ZR7PSWGRS5bzbK4vMyK9FA5CLQolr2N B6ouPNWpgq3Af7Pn/f45+pDtKRsBLX8+q/Mw7TOYR525e7nVePDBLM2wLQZ4Gh5QMQzEI3Me3Zc3 030jRg0gEG13N/1EzMo=

但我尝试了很多方法,无法解决它。 有什么问题?

任何帮助将不胜感激,谢谢。

这是我的代码:

的java:

public static void main(String[] args) throws Exception {  
String pubKey_from_go="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZsfv1qscqYdy4vY+P4e3cAtmv"+
            "ppXQcRvrF1cB4drkv0haU24Y7m5qYtT52Kr539RdbKKdLAM6s20lWy7+5C0Dgacd"+
            "wYWd/7PeCELyEipZJL07Vro7Ate8Bfjya+wltGK9+XNUIHiumUKULW4KDx21+1NL"+
            "AUeJ6PeW+DAkmJWF6QIDAQAB";

String plainText = "Hello,I am  plaintext string!@sina.com"; 
String encryptString=encByGoPubKey(pubKey_from_go,plainText);
}
public static String encByGoPubKey(String pubkey_from_go,String plainText) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");//Cipher.getInstance("RSA/ECB/PKCS1Padding");  
    byte[] plainTextBytes = plainText.getBytes();  

    PublicKey pubkey_go=getPublicKey(pubkey_from_go);
    cipher.init(Cipher.ENCRYPT_MODE, pubkey_go);  
    byte[] enBytes = cipher.doFinal(plainTextBytes);  
    String encryptString = (new BASE64Encoder()).encode(enBytes);  
    return encryptString;   
}
public static PublicKey getPublicKey(String key) throws Exception {  
  byte[] keyBytes;  
  keyBytes = (new BASE64Decoder()).decodeBuffer(key);  

  X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);  
  KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
  PublicKey publicKey = keyFactory.generatePublic(keySpec);  
  return publicKey;  
}  

转到:

import (
        "crypto/rand"
        "crypto/rsa"
        // "crypto/sha1"
        "crypto/x509"
        "encoding/base64"
        "encoding/pem"
        "errors"
        // "flag"
        "fmt"
        "github.com/astaxie/beego"
    )
var publicKey = []byte(`
    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZsfv1qscqYdy4vY+P4e3cAtmv
    ppXQcRvrF1cB4drkv0haU24Y7m5qYtT52Kr539RdbKKdLAM6s20lWy7+5C0Dgacd
    wYWd/7PeCELyEipZJL07Vro7Ate8Bfjya+wltGK9+XNUIHiumUKULW4KDx21+1NL
    AUeJ6PeW+DAkmJWF6QIDAQAB
    -----END PUBLIC KEY-----
    `)
    var privateKey = []byte(`
    -----BEGIN RSA PRIVATE KEY-----
    MIICXQIBAAKBgQDZsfv1qscqYdy4vY+P4e3cAtmvppXQcRvrF1cB4drkv0haU24Y
    7m5qYtT52Kr539RdbKKdLAM6s20lWy7+5C0DgacdwYWd/7PeCELyEipZJL07Vro7
    Ate8Bfjya+wltGK9+XNUIHiumUKULW4KDx21+1NLAUeJ6PeW+DAkmJWF6QIDAQAB
    AoGBAJlNxenTQj6OfCl9FMR2jlMJjtMrtQT9InQEE7m3m7bLHeC+MCJOhmNVBjaM
    ZpthDORdxIZ6oCuOf6Z2+Dl35lntGFh5J7S34UP2BWzF1IyyQfySCNexGNHKT1G1
    XKQtHmtc2gWWthEg+S6ciIyw2IGrrP2Rke81vYHExPrexf0hAkEA9Izb0MiYsMCB
    /jemLJB0Lb3Y/B8xjGjQFFBQT7bmwBVjvZWZVpnMnXi9sWGdgUpxsCuAIROXjZ40
    IRZ2C9EouwJBAOPjPvV8Sgw4vaseOqlJvSq/C/pIFx6RVznDGlc8bRg7SgTPpjHG
    4G+M3mVgpCX1a/EU1mB+fhiJ2LAZ/pTtY6sCQGaW9NwIWu3DRIVGCSMm0mYh/3X9
    DAcwLSJoctiODQ1Fq9rreDE5QfpJnaJdJfsIJNtX1F+L3YceeBXtW0Ynz2MCQBI8
    9KP274Is5FkWkUFNKnuKUK4WKOuEXEO+LpR+vIhs7k6WQ8nGDd4/mujoJBr5mkrw
    DPwqA3N5TMNDQVGv8gMCQQCaKGJgWYgvo3/milFfImbp+m7/Y3vCptarldXrYQWO
    AQjxwc71ZGBFDITYvdgJM1MTqc8xQek1FXn1vfpy2c6O
    -----END RSA PRIVATE KEY-----
    `
    func RsaEncrypt(origData []byte, pubKey []byte) ([]byte, error) {
        block, _ := pem.Decode(pubKey)
        if block == nil {
            return nil, errors.New("public key error")
        }
        pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
        if err != nil {
            return nil, err
        }
        pub := pubInterface.(*rsa.PublicKey)
        return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
    }

    func RsaDecrypt(ciphertext []byte, privKey []byte) ([]byte, error) {
        block, _ := pem.Decode(privKey)
        if block == nil {
            return nil, errors.New("private key error!")
        }
        priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
        if err != nil {
            return nil, err
        }
        return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
    }
var data64 string = `kkkHf5QSXx8aDadk66AOysmV8LOi4vWUANal+7KV6va/5ZR7PSWGRS5bzbK4vMyK9FA5CLQolr2NB6ouPNWpgq3Af7Pn/f45+pDtKRsBLX8+q/Mw7TOYR525e7nVePDBLM2wLQZ4Gh5QMQzEI3Me3Zc3030jRg0gEG13N/1EzMo=`
    func main() {
        data, _ := base64.StdEncoding.DecodeString(data64)
        origData, _ := RsaDecrypt(data, privateKey)
        fmt.Println("rsa-------" + string(origData))
    }

1 个答案:

答案 0 :(得分:1)

尝试在Java中使用填充进行加密。我什么都不知道"去"但在Java中,您正在加密没有填充的消息。

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

在此代码段更改" RSA"到" RSA / ECB / PKCS1Padding"因为在"去"因为我看到你正在使用一些名为" DecryptPKCS1v15"的函数,PKCS1 v1.5标准本身就意味着要使用的PKCS#1填充。希望它可以帮到你。