base64和byte[]相互转换

   /**
     * 上传图片获取图片base64码,然后解码,然后转成字节数组,以流的形式输出到本地
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadYujhImg1", method = RequestMethod.POST)
    @ResponseBody
    public EntityServiceResponse<Map<String, String>> uploadYujhImg1(String fileImg) throws Exception {
        logger.info("uploadYujh"+fileImg);
        String imgBase64 = fileImg.replaceAll("data:image/png;base64,","");
        BASE64Decoder d = new BASE64Decoder();
        byte[] data = d.decodeBuffer(imgBase64);
        FileOutputStream os = new FileOutputStream("D:\\tupian\\d.jpg");
        os.write(data);
        os.close();
        return null;
    }
///
  
//读取本地图片输入流
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
   public static void  main (String arg[]) throws IOException {
        //读取本地图片输入流
        //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        String imgFile = "D:\\tupian\\a.jpg";//待处理的图片
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        System.out.println(encoder.encode(data));//返回Base64编码过的字节数组字符串
    }


 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将 Base64 字符串和 ECC 加密后的 CipherBlob 相互转换的 Java 代码示例: ```java import java.security.*; import java.security.spec.ECGenParameterSpec; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class CryptoUtils { private static final String ECC_ALGORITHM = "EC"; private static final String AES_ALGORITHM = "AES"; private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5PADDING"; private static final String ECC_CURVE_NAME = "secp256k1"; private static final int AES_KEY_SIZE = 256; public static String encrypt(String input, String publicKeyBase64) throws Exception { // Generate random AES encryption key byte[] aesKey = new byte[AES_KEY_SIZE / 8]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(aesKey); // Use the public key to encrypt the AES key PublicKey publicKey = getPublicKeyFromBase64(publicKeyBase64); Cipher cipher = Cipher.getInstance(ECC_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedAesKey = cipher.doFinal(aesKey); // Encrypt the input with AES-CBC algorithm and the generated key byte[] iv = new byte[16]; secureRandom.nextBytes(iv); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey, AES_ALGORITHM); cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] encryptedInput = cipher.doFinal(input.getBytes()); // Combine the encrypted AES key and encrypted input into a CipherBlob byte[] cipherBlob = new byte[encryptedAesKey.length + iv.length + encryptedInput.length]; System.arraycopy(encryptedAesKey, 0, cipherBlob, 0, encryptedAesKey.length); System.arraycopy(iv, 0, cipherBlob, encryptedAesKey.length, iv.length); System.arraycopy(encryptedInput, 0, cipherBlob, encryptedAesKey.length + iv.length, encryptedInput.length); // Return the Base64-encoded CipherBlob return Base64.getEncoder().encodeToString(cipherBlob); } public static String decrypt(String cipherBlobBase64, String privateKeyBase64) throws Exception { // Decode the Base64-encoded CipherBlob byte[] cipherBlob = Base64.getDecoder().decode(cipherBlobBase64); // Split the CipherBlob into encrypted AES key, IV, and encrypted input byte[] encryptedAesKey = new byte[AES_KEY_SIZE / 8]; byte[] iv = new byte[16]; byte[] encryptedInput = new byte[cipherBlob.length - encryptedAesKey.length - iv.length]; System.arraycopy(cipherBlob, 0, encryptedAesKey, 0, encryptedAesKey.length); System.arraycopy(cipherBlob, encryptedAesKey.length, iv, 0, iv.length); System.arraycopy(cipherBlob, encryptedAesKey.length + iv.length, encryptedInput, 0, encryptedInput.length); // Use the private key to decrypt the AES key PrivateKey privateKey = getPrivateKeyFromBase64(privateKeyBase64); Cipher cipher = Cipher.getInstance(ECC_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] aesKey = cipher.doFinal(encryptedAesKey); // Decrypt the input with AES-CBC algorithm and the decrypted key IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey, AES_ALGORITHM); cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] decryptedInput = cipher.doFinal(encryptedInput); // Return the decrypted input as a string return new String(decryptedInput); } private static PublicKey getPublicKeyFromBase64(String publicKeyBase64) throws Exception { byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyBase64); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance(ECC_ALGORITHM); return keyFactory.generatePublic(keySpec); } private static PrivateKey getPrivateKeyFromBase64(String privateKeyBase64) throws Exception { byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyBase64); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance(ECC_ALGORITHM); return keyFactory.generatePrivate(keySpec); } public static KeyPair generateKeyPair() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ECC_ALGORITHM); ECGenParameterSpec ecSpec = new ECGenParameterSpec(ECC_CURVE_NAME); keyGen.initialize(ecSpec); return keyGen.generateKeyPair(); } } ``` 使用示例: ```java public static void main(String[] args) throws Exception { // Generate a new key pair KeyPair keyPair = CryptoUtils.generateKeyPair(); // Encrypt a message using the public key String message = "Hello, world!"; String cipherBlob = CryptoUtils.encrypt(message, Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded())); System.out.println("CipherBlob: " + cipherBlob); // Decrypt the message using the private key String decryptedMessage = CryptoUtils.decrypt(cipherBlob, Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded())); System.out.println("Decrypted message: " + decryptedMessage); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值