在R中加密密码-使用RODBC连接到Oracle数据库
我使用以下代码连接Oracle数据库:在R中加密密码-使用RODBC连接到Oracle数据库,r,oracle,passwords,odbc,R,Oracle,Passwords,Odbc,我使用以下代码连接Oracle数据库: > library(RODBC) > channel <- odbcConnect("R", uid="xxx", pwd="catch@123") > sqlSave(channel ,resultsclassifiedfinal_MC_TC_P1, tablename="table1", rownames=FALSE, append=TRUE, fast = FALSE, nastring = NULL) > odbcC
> library(RODBC)
> channel <- odbcConnect("R", uid="xxx", pwd="catch@123")
> sqlSave(channel ,resultsclassifiedfinal_MC_TC_P1, tablename="table1", rownames=FALSE, append=TRUE, fast = FALSE, nastring = NULL)
> odbcClose(channel)
加密就是这样做的吗?
我应该用这样的东西吗:
channel <- odbcConnect("R", uid="xxx", pwd=rawToChar(y))
channelEDIT:下面的功能现在在我的R软件包中可用。keyringr包还具有访问Gnome Keyring和macOS Keychain的类似功能
---
如果您使用的是Windows,则可以使用PowerShell执行此操作。请看下面我的博客文章
本质上
确保你有
将以下文本保存到名为EncryptPassword.ps1的文件中:
# Create directory user profile if it doesn't already exist.
$passwordDir = "$($env:USERPROFILE)\DPAPI\passwords\$($env:computername)"
New-Item -ItemType Directory -Force -Path $passwordDir
# Prompt for password to encrypt
$account = Read-Host "Please enter a label for the text to encrypt. This will be how you refer to the password in R. eg. MYDB_MYUSER
$SecurePassword = Read-Host -AsSecureString "Enter password" | convertfrom-securestring | out-file "$($passwordDir)\$($account).txt"
# Check output and press any key to exit
Write-Host "Press any key to continue..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
执行上面的脚本(右键单击>使用PowerShell运行),为密码提供有意义的名称,然后键入密码。您现在可以通过检查%USERPROFILE%/DPAPI/passwords/[PC NAME]/[password IDENTIFIER.txt]中的文件来验证密码是否已加密
现在从R中运行以下代码(我将此函数保存在一个R脚本中,在每个脚本的开头我都会使用该脚本)
getEncryptedPassword <- function(credential_label, credential_path) {
# if path not supplied, use %USER_PROFILE%\DPAPI\passwords\computername\credential_label.txt as default
if (missing(credential_path)) {
credential_path <- paste(Sys.getenv("USERPROFILE"), '\\DPAPI\\passwords\\', Sys.info()["nodename"], '\\', credential_label, '.txt', sep="")
}
# construct command
command <- paste('powershell -command "$PlainPassword = Get-Content ', credential_path, '; $SecurePassword = ConvertTo-SecureString $PlainPassword; $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword); $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR); echo $UnsecurePassword"', sep='')
# execute powershell and return command
return(system(command, intern=TRUE))
}
例如,不运行ROracle命令,而是:
dbConnect(driver, "MYUSER", "MY PASSWORD", dbname="MYDB")
您可以改为运行它(我在步骤3中提供的标识符是“MYUSER\u MYDB”:
您可以根据需要重复第3步输入任意数量的密码,只需在第5步中使用正确的标识符调用密码即可
区别是什么?您将加密密码存储在本地,因此任何想要它的人都可以加载并发送它。这对您没有帮助。我怀疑您实际需要的是加密通道(例如SSH)。因此,这是我的问题,到目前为止-我能够做到-但不确定SSH
getEncryptedPassword("[PASSWORD IDENTIFIER]")
dbConnect(driver, "MYUSER", "MY PASSWORD", dbname="MYDB")
dbConnect(driver, "MYUSER", getEncryptedPassword("MYUSER_MYDB"), dbname="MYDB")